基础配置

1、引用

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-data-mongodb</artifactId>
  4. </dependency>

2、配置

  1. # 单机、用户名、密码
  2. spring.data.mongodb.uri=mongodb://name:pass@localhost:27017/test
  3. # 集群
  4. spring.data.mongodb.uri=mongodb://user:pwd@ip1:port1,ip2:port2/database
  5. # 认证数据库和基础数据库都要写
  6. spring:
  7. data:
  8. mongodb:
  9. uri: mongodb://dev:kaimen@10.10.20.9:27017
  10. authentication-database: base_data
  11. database: base_data
  12. # 无密码
  13. spring.data.mongodb.uri=mongodb://localhost:27017/test

mongoTemplate

保存

  1. @Autowired
  2. private MongoTemplate mongoTemplate;
  3. mongoTemplate.save(user);

查询

  1. @Override
  2. public User findUserByUserName(String userName) {
  3. Query query=new Query(Criteria.where("userName").is(userName));
  4. User user = mongoTemplate.findOne(query , User.class);
  5. return user;
  6. }

条件更新

  1. @Override
  2. public long updateUser(User user) {
  3. Query query=new Query(Criteria.where("id").is(user.getId()));
  4. Update update= new Update().set("userName", user.getUserName()).set("passWord", user.getPassWord());
  5. //更新查询返回结果集的第一条
  6. UpdateResult result =mongoTemplate.updateFirst(query,update,User.class);
  7. //更新查询返回结果集的所有
  8. // mongoTemplate.updateMulti(query,update,UserEntity.class);
  9. if(result!=null)
  10. return result.getMatchedCount();
  11. else
  12. return 0;
  13. }

删除

  1. @Override
  2. public void deleteUserById(Long id) {
  3. Query query=new Query(Criteria.where("id").is(id));
  4. mongoTemplate.remove(query,User.class);
  5. }

Repository

封装了一些简单操作,复杂操作还是需要使用template

  1. public interface SecondaryRepository extends MongoRepository<User, String> {
  2. }

多数据源

配置

  1. mongodb.primary.uri=mongodb://localhost:27017
  2. mongodb.primary.database=primary
  3. mongodb.secondary.uri=mongodb://localhost:27017
  4. mongodb.secondary.database=secondary

读取配置

  1. @ConfigurationProperties(prefix = "mongodb")
  2. public class MultipleMongoProperties {
  3. private MongoProperties primary = new MongoProperties();
  4. private MongoProperties secondary = new MongoProperties();
  5. …………get set
  6. }
  7. @Configuration
  8. @EnableConfigurationProperties(MultipleMongoProperties.class)
  9. @EnableMongoRepositories(basePackages = "com.neo.repository.primary",
  10. mongoTemplateRef = "primaryMongoTemplate")
  11. public class PrimaryMongoConfig {
  12. }
  13. @Configuration
  14. @EnableConfigurationProperties(MultipleMongoProperties.class)
  15. @EnableMongoRepositories(basePackages = "com.neo.repository.secondary",
  16. mongoTemplateRef = "secondaryMongoTemplate")
  17. public class SecondaryMongoConfig {
  18. }

配置多个Template

  1. @Configuration
  2. public class MultipleMongoConfig {
  3. @Autowired
  4. private MultipleMongoProperties mongoProperties;
  5. @Primary
  6. @Bean(name = "primaryMongoTemplate")
  7. public MongoTemplate primaryMongoTemplate() throws Exception {
  8. return new MongoTemplate(primaryFactory(this.mongoProperties.getPrimary()));
  9. }
  10. @Bean
  11. @Qualifier("secondaryMongoTemplate")
  12. public MongoTemplate secondaryMongoTemplate() throws Exception {
  13. return new MongoTemplate(secondaryFactory(this.mongoProperties.getSecondary()));
  14. }
  15. @Bean
  16. @Primary
  17. public MongoDbFactory primaryFactory(MongoProperties mongo) throws Exception {
  18. MongoClient client = new MongoClient(new MongoClientURI(mongoProperties.getPrimary().getUri()));
  19. return new SimpleMongoDbFactory(client, mongoProperties.getPrimary().getDatabase());
  20. }
  21. @Bean
  22. public MongoDbFactory secondaryFactory(MongoProperties mongo) throws Exception {
  23. MongoClient client = new MongoClient(new MongoClientURI(mongoProperties.getSecondary().getUri()));
  24. return new SimpleMongoDbFactory(client, mongoProperties.getSecondary().getDatabase());
  25. }
  26. }

多个Repository

  1. public interface PrimaryRepository extends MongoRepository<User, String> {
  2. }
  3. public interface SecondaryRepository extends MongoRepository<User, String> {
  4. }