美文网首页
微服务实战(二)连接mysql以及mongodb

微服务实战(二)连接mysql以及mongodb

作者: 负二贷 | 来源:发表于2018-01-11 13:28 被阅读0次

    在实战一种我们完成了hello world,接下来我们就完成连接数据库以及mongodb,首先我们在配置文件application.properties中添加如下配置:

    #connect mysql
    spring.datasource.url=jdbc:mysql://localhost:3306/test
    spring.datasource.username=root
    spring.datasource.password=1988131zjp
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    spring.datasource.max-idle=10
    spring.datasource.max-wait=10000
    spring.datasource.min-idle=5
    spring.datasource.initial-size=5
    spring.datasource.jdbc-interceptors=ConnectionState;SlowQueryReport(threshold=0)
    #rabbitmq
    spring.rabbitmq.host=192.168.20.102
    spring.rabbitmq.port=5672
    spring.rabbitmq.username=admin
    spring.rabbitmq.password=admin
    #mongodb mongodb 3.x 只支持这种方式
    #spring.data.mongodb.uri=mongodb://192.168.20.102/local
    spring.data.mongodb.host=192.168.20.102
    spring.data.mongodb.port=27017
     
    server.port=8080
    server.session.timeout=10
    server.tomcat.uri-encoding=UTF-8
    

    然后再pom.xml加入下面的配置,配置文件如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
     
        <groupId>com.datababy.springcloud</groupId>
        <artifactId>cloud</artifactId>
        <version>1.0-SNAPSHOT</version>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.0.M7</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
     
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
        </properties>
     
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-jdbc</artifactId>
            </dependency>
            <dependency>
                <groupId>org.mongodb</groupId>
                <artifactId>mongodb-driver</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-mongodb</artifactId>
            </dependency>
            <!--<dependency>
                <groupId>de.flapdoodle.embed</groupId>
                <artifactId>de.flapdoodle.embed.mongo</artifactId>
            </dependency>-->
            <dependency>
                <groupId>org.springframework.data</groupId>
                <artifactId>spring-data-mongodb</artifactId>
                <exclusions>
                    <exclusion>
                        <groupId>org.mongodb</groupId>
                        <artifactId>mongo-java-driver</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
        </dependencies>
     
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
     
        <repositories>
            <repository>
                <id>spring-snapshots</id>
                <name>Spring Snapshots</name>
                <url>https://repo.spring.io/snapshot</url>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </repository>
            <repository>
                <id>spring-milestones</id>
                <name>Spring Milestones</name>
                <url>https://repo.spring.io/milestone</url>
                <snapshots>
                    <enabled>false</enabled>
                </snapshots>
            </repository>
            <repository>
                <id>spring-snapshots</id>
                <name>Spring Snapshots</name>
                <url>https://repo.spring.io/libs-snapshot</url>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </repository>
        </repositories>
        <pluginRepositories>
            <pluginRepository>
                <id>spring-snapshots</id>
                <name>Spring Snapshots</name>
                <url>https://repo.spring.io/snapshot</url>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </pluginRepository>
            <pluginRepository>
                <id>spring-milestones</id>
                <name>Spring Milestones</name>
                <url>https://repo.spring.io/milestone</url>
                <snapshots>
                    <enabled>false</enabled>
                </snapshots>
            </pluginRepository>
        </pluginRepositories>
     
    </project>
    

    添加完成以后我们在SpringBootTest.java中加入如下的代码最后代码如下:

    import com.datababy.enity.Customer;
    import com.datababy.enity.Test;
    import com.datababy.service.CustomerRepository;
    import com.datababy.service.TestService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    import org.springframework.boot.autoconfigure.domain.EntityScan;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
    import org.springframework.web.bind.annotation.*;
     
    import java.util.List;
    /**
     * Created by yongma on 2017/1/23.
     */
    @RestController
    @ComponentScan({"com.datababy.*"})
    @EnableMongoRepositories
    @EnableAutoConfiguration
    public class SpringBootTest {
     
        @Autowired
        private TestService services;
        @Autowired
        private CustomerRepository customerRepository;
        @ResponseBody
        @RequestMapping(value = "/")
        public String location() {
            return "北京";
        }
     
        @ResponseBody
        @RequestMapping(value="/get")
        public List<Test> getTest(){
           return services.getTest();
        }
     
     
         @RequestMapping(value = "/addCustomer", method = RequestMethod.GET)
         public Customer addCustomer(){
            return customerRepository.save(new Customer("test1","test1"));
         }
     
     
     
     
        public static void main(String[] args) {
     
            SpringApplication.run(SpringBootTest.class, args);
     
        }
     
     
    }
    

    最后项目结构结果如下图:

    image.png

    完整代码下载地址:https://github.com/majiajue/cloud.git

    相关文章

      网友评论

          本文标题:微服务实战(二)连接mysql以及mongodb

          本文链接:https://www.haomeiwen.com/subject/eyiznxtx.html