SpringBoot MongoDB多数据源配置
添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
<version>2.6.4</version>
</dependency>
添加配置yml
spring:
mongodb:
primary:
uri: mongodb://admin:123456@192.168.1.77:27017/admin
#集群配置
#uri: mongodb://admin:123456@192.168.1.77:27017,192.168.2.77:27017,192.168.3.77:27017/admin
secondary:
uri: mongodb://admin:123456@192.168.1.77:27017/admin
配置类
/**
* @Description: mongo配置类 - 父类
* @Author: liang
*/
public abstract class AbstractMongoConfig {
public MongoDatabaseFactory mongoDbFactory(String uri){
return new SimpleMongoClientDatabaseFactory(uri);
}
}
/**
* 主库-配置
*/
@Configuration
@Component
public class PrimaryMongoConfig extends AbstractMongoConfig{
@Value("${spring.mongodb.primary.uri}")
private String uri;
@Primary
@Bean(name = "primaryMongoTemplate")
public MongoTemplate mongoTemplate(){
return new MongoTemplate(mongoDbFactory(uri));
}
}
/**
* 第二个库-配置
*/
@Configuration
@Component
public class SecondaryMongoConfig extends AbstractMongoConfig{
@Value("${spring.mongodb.secondary.uri}")
private String uri;
@Bean(name = "secondaryMongoTemplate")
public MongoTemplate mongoTemplate(){
return new MongoTemplate(mongoDbFactory(uri));
}
}
排除默认配置,springboot自动配置了支持mongodb。在启动springboot时会自动实例化一个mongo实例。所以自己配置的话,需要禁用掉springboot的自动配置。
@SpringBootApplication(exclude = {
MongoAutoConfiguration.class,
MongoDataAutoConfiguration.class})
public class MongoDbdemoApplication
使用
@Autowired
@Qualifier("primaryMongoTemplate")
private MongoTemplate mongoTemplate;
@Autowired
@Qualifier("secondaryMongoTemplate")
private MongoTemplate secondaryMongoTemplate;
@Test
void findById() {
User user = mongoTemplate.findById(1, User.class);
logger.info("查询用户 : [{}]", user.getId());
}
@Test
void findByIdSecondary() {
User user = secondaryMongoTemplate.findById(1, User.class);
logger.info("查询用户 : [{}]", user.getId());
}
四、MongoTemplate 常用方法
#单个插入
@Test
void addUser() {
User user = mongoTemplate.save(new User(3, "张三"));
User user2 = mongoTemplate.save(new User(4, "小王"));
logger.info("创建用户成功 : [{}],[{}]", user, user2);
}
#批量插入
@Test
void batchAddUser() {
User user7 = new User(7, "user7");
User user6 = new User(6, "user6");
List<User> list = new ArrayList<>(2);
list.add(user7);
list.add(user6);
Collection<User> collection = mongoTemplate.insertAll(list);
logger.info("创建用户成功 : [{}]", collection);
}
#单个查询
@Test
void findById() {
User user = mongoTemplate.findById(1, User.class);
logger.info("查询用户 : [{}]", user.getId());
}
#查询所有数据
@Test
void findAll() {
List<User> all = mongoTemplate.findAll(User.class);
logger.info("查询用户 : [{}]", Arrays.toString(all.toArray()));
}
#批量按照条件查询
@Test
void findByCondition() {
Query query = new Query(Criteria.where("id").in(1,2,3));
List<User> all = mongoTemplate.find(query, User.class);
logger.info("查询用户 : [{}]", Arrays.toString(all.toArray()));
}
#批量更新
@Test
void updateMulti() {
Query query = new Query(Criteria.where("id").in(1,2,3));
Update update = new Update().set("name", "updatedName");
mongoTemplate.updateMulti(query,update, User.class);
}
#根据条件删除
@Test
void removeByCondition() {
Query query = new Query(Criteria.where("id").is(1));
mongoTemplate.remove(query, User.class);
}