赞
踩
某些特定场合,需要分库查询。
如:敏感数据分库存储,日志数据分库存储等。
基于Springboot 1.5.6.RELEASE
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver</artifactId>
<version>3.6.4</version>
</dependency>
server:
port: 8081
spring:
data:
mongodb:
primary:
uri: mongodb://root:123root@127.0.0.1:27017/ltz1
secondary:
uri: mongodb://root2:123root@127.0.0.1:27017/ltz2
import com.mongodb.MongoClientURI; 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; import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; /** * @author litong * @date 2020/1/19 14:40 */ @Configuration @EnableMongoRepositories(basePackages = "com.ltz.demo.primary.dao", mongoTemplateRef = "primaryMongoTemplate") public class PrimaryMongoConfig { @Bean @Primary @ConfigurationProperties(prefix = "spring.data.mongodb.primary") public MongoProperties primaryMongoProperties() { return new MongoProperties(); } @Primary @Bean(name = "primaryMongoTemplate") public MongoTemplate primaryMongoTemplate() throws Exception { return new MongoTemplate(primaryFactory(primaryMongoProperties())); } @Bean @Primary public MongoDbFactory primaryFactory(MongoProperties mongoProperties) throws Exception { return new SimpleMongoDbFactory(new MongoClientURI(primaryMongoProperties().getUri())); } }
import com.mongodb.MongoClientURI; 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; import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; /** * @author litong * @date 2020/1/19 14:40 */ @Configuration @EnableMongoRepositories(basePackages = "com.ltz.demo.secondary.dao", mongoTemplateRef = "secondaryMongoTemplate") public class SecondaryMongoConfig { @Bean @ConfigurationProperties(prefix="spring.data.mongodb.secondary") public MongoProperties secondaryMongoProperties() { return new MongoProperties(); } @Bean(name = "secondaryMongoTemplate") public MongoTemplate secondaryMongoTemplate() throws Exception { return new MongoTemplate(secondaryFactory(secondaryMongoProperties())); } @Bean public MongoDbFactory secondaryFactory(MongoProperties mongoProperties) throws Exception { return new SimpleMongoDbFactory(new MongoClientURI(secondaryMongoProperties().getUri())); } }
primary
和secondary
只是示例,可自行修改名称,但是记得要一并修改Bean的名称。com.ltz.demo.primary.dao
和com.ltz.demo.secondary.dao
两个包名,也可自行修改,然后在包下建立对应的DAO(Repository)
。即可实现在该数据源下的JPA操作。赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。