一、 jar包引入
<!--可以在编译源码的时候生成对应的方法--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.6</version> <scope>provided</scope> </dependency> <!--mongodb--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency> <!--解决autoconfigure找不到配置文件的bug--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <!--spring boot的自动化配置--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> <version>2.0.3.RELEASE</version> </dependency>
二、在文件中添加两条数据源属性
mongodb: primary: host: localhost port: 27017 database: test secondary: host: localhost port: 27017 database: data
三、在entity中创建一个配置文件实体类
import lombok.Data; import org.springframework.boot.autoconfigure.mongo.MongoProperties; @Data //生成get、set方法等 public class MultipleMongoProperties { private MongoProperties primary = new MongoProperties(); private MongoProperties secondary = new MongoProperties(); }
四、配置第一个数据库
@Configuration @EnableMongoRepositories(basePackages = "com.tedu.huawei.repository.primary", mongoTemplateRef = PrimaryMongoConfig.MONGO_TEMPLATE) //basePackages对应第一个库的repository所在的地址 public class PrimaryMongoConfig { protected static final String MONGO_TEMPLATE = "primaryMongoTemplate"; }
五、配置第二个数据库
@Configuration @EnableMongoRepositories(basePackages = "com.tedu.huawei.repository.secondary", mongoTemplateRef = SecondaryMongoConfig.MONGO_TEMPLATE) public class SecondaryMongoConfig { protected static final String MONGO_TEMPLATE = "secondaryMongoTemplate"; }
六、读取对应的配置信息并且构造对应的MongoTemplate
import com.mongodb.MongoClient; import com.tedu.huawei.entity.MultipleMongoProperties; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.mongo.MongoProperties; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.data.mongodb.MongoDbFactory; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.SimpleMongoDbFactory; @Configuration public class MultipleMongoConfig { @Autowired private MultipleMongoProperties mongoProperties; @Bean @ConfigurationProperties(prefix = "mongodb") public MultipleMongoProperties connectionSettings(){ return new MultipleMongoProperties(); } @Primary @Bean(name = PrimaryMongoConfig.MONGO_TEMPLATE) public MongoTemplate primaryMongoTemplate() throws Exception { return new MongoTemplate(primaryFactory(this.mongoProperties.getPrimary())); } @Bean @Qualifier(SecondaryMongoConfig.MONGO_TEMPLATE) public MongoTemplate secondaryMongoTemplate() throws Exception { return new MongoTemplate(secondaryFactory(this.mongoProperties.getSecondary())); } @Bean @Primary public MongoDbFactory primaryFactory(MongoProperties mongo) throws Exception { return new SimpleMongoDbFactory(new MongoClient(mongo.getHost(), mongo.getPort()), mongo.getDatabase()); } @Bean public MongoDbFactory secondaryFactory(MongoProperties mongo) throws Exception { return new SimpleMongoDbFactory(new MongoClient(mongo.getHost(), mongo.getPort()), mongo.getDatabase()); } }
七、创建两个库分别对应的repository
image
八、测试
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.2.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.2.2</version> </dependency>
添加配置类
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration @EnableSwagger2 public class SwaggerConfiguration { @Bean public Docket createRestApi(){ return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.tedu.huawei.controller")) //基础扫描包路径 .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo(){ return new ApiInfoBuilder() .title("配置swagger") .description("配置swagger") .termsOfServiceUrl("https://javcfox.github.io") //地址 .contact("lwx640993") //负责人 .version("0.1") .build(); } }
配置好后,访问地址:http://localhost:8080/swagger-ui.html
测试结果: