Spring boot + Mongodb

偶尔善良 提交于 2020-12-12 00:45:08

mongodb的增删改查

1、pom包配置

pom包里面添加spring-boot-starter-data-mongodb包引用

<dependencies>
    <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency> </dependencies>

 

2、在application.properties中添加配置

spring.data.mongodb.uri=mongodb://user:pwd@localhost:27017/test

多个IP集群可以采用以下配置:

spring.data.mongodb.uri=mongodb://user:pwd@ip1:port1,ip2:port2/database

 

3、创建数据实体

public class UserEntity implements Serializable { private static final long serialVersionUID = -3258839839160856613L; private Long id; private String userName; private String passWord; //getter、setter省略 }

 

4、创建实体dao的增删改查操作

dao层实现了UserEntity对象的增删改查

@Component
public class UserDaoImpl implements UserDao { @Autowired private MongoTemplate mongoTemplate; /**  * 创建对象  * @param user  */ @Override public void saveUser(UserEntity user) { mongoTemplate.save(user); } /**  * 根据用户名查询对象  * @param userName  * @return  */ @Override public UserEntity findUserByUserName(String userName) { Query query=new Query(Criteria.where("userName").is(userName)); UserEntity user = mongoTemplate.findOne(query , UserEntity.class); return user; } /**  * 更新对象  * @param user  */ @Override public void updateUser(UserEntity user) { Query query=new Query(Criteria.where("id").is(user.getId())); Update update= new Update().set("userName", user.getUserName()).set("passWord", user.getPassWord()); //更新查询返回结果集的第一条 mongoTemplate.updateFirst(query,update,UserEntity.class); //更新查询返回结果集的所有 // mongoTemplate.updateMulti(query,update,UserEntity.class); } /**  * 删除对象  * @param id  */ @Override public void deleteUserById(Long id) { Query query=new Query(Criteria.where("id").is(id)); mongoTemplate.remove(query,UserEntity.class); } }

 

5、开发对应的测试方法

@RunWith(SpringRunner.class)
@SpringBootTest public class UserDaoTest { @Autowired private UserDao userDao; @Test public void testSaveUser() throws Exception { UserEntity user=new UserEntity(); user.setId(2l); user.setUserName("小明"); user.setPassWord("fffooo123"); userDao.saveUser(user); } @Test public void findUserByUserName(){ UserEntity user= userDao.findUserByUserName("小明"); System.out.println("user is "+user); } @Test public void updateUser(){ UserEntity user=new UserEntity(); user.setId(2l); user.setUserName("天空"); user.setPassWord("fffxxxx"); userDao.updateUser(user); } @Test public void deleteUserById(){ userDao.deleteUserById(1l); } }

 

6、查看验证结果

可以使用工具mongoVUE工具来连接后直接图形化展示查看,也可以登录服务器用命令来查看

1.登录mongos

bin/mongo -host localhost -port 20000

2、切换到test库

use test

3、查询userEntity集合数据

db.userEntity.find()

根据3查询的结果来观察测试用例的执行是否正确。

到此springboot对应mongodb的增删改查功能已经全部实现。

 

多数据源mongodb的使用

在多mongodb数据源的情况下,我们换种更优雅的方式来实现

 

1、pom包配置

添加lombok和spring-boot-autoconfigure包引用

<dependency>
    <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> <version>RELEASE</version> </dependency>
  • Lombok - 是一个可以通过简单的注解形式来帮助我们简化消除一些必须有但显得很臃肿的Java代码的工具,通过使用对应的注解,可以在编译源码的时候生成对应的方法。简单试了以下这个工具还挺好玩的,加上注解我们就不用手动写 getter\setter、构建方式类似的代码了。

  • spring-boot-autoconfigure - 就是spring boot的自动化配置

 

2、配置文件使用YAML的形式添加两条数据源,如下:

mongodb:
  primary:
    host: 192.168.9.60  port: 20000  database: test  secondary:  host: 192.168.9.60  port: 20000  database: test1

 

3、配置两个库的数据源

封装读取以mongodb开头的两个配置文件

@Data
@ConfigurationProperties(prefix = "mongodb") public class MultipleMongoProperties { private MongoProperties primary = new MongoProperties(); private MongoProperties secondary = new MongoProperties(); }

配置不同包路径下使用不同的数据源

第一个库的封装

@Configuration
@EnableMongoRepositories(basePackages = "com.neo.model.repository.primary", mongoTemplateRef = PrimaryMongoConfig.MONGO_TEMPLATE) public class PrimaryMongoConfig { protected static final String MONGO_TEMPLATE = "primaryMongoTemplate"; }

第二个库的封装

@Configuration
@EnableMongoRepositories(basePackages = "com.neo.model.repository.secondary", mongoTemplateRef = SecondaryMongoConfig.MONGO_TEMPLATE) public class SecondaryMongoConfig { protected static final String MONGO_TEMPLATE = "secondaryMongoTemplate"; }

读取对应的配置信息并且构造对应的MongoTemplate

@Configuration
public class MultipleMongoConfig { @Autowired private MultipleMongoProperties mongoProperties; @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()); } }

两个库的配置信息已经完成。

4、创建两个库分别对应的对象和Repository

借助lombok来构建对象

@Data
@AllArgsConstructor
@NoArgsConstructor @Document(collection = "first_mongo") public class PrimaryMongoObject { @Id private String id; private String value; @Override public String toString() { return "PrimaryMongoObject{" + "id='" + id + '\'' + ", value='" + value + '\'' + '}'; } }

对应的Repository

public interface PrimaryRepository extends MongoRepository<PrimaryMongoObject, String> { }

继承了 MongoRepository 会默认实现很多基本的增删改查,省了很多自己写dao层的代码

Secondary和上面的代码类似就不贴出来了

 

5、最后测试

@RunWith(SpringRunner.class)
@SpringBootTest public class MuliDatabaseTest { @Autowired private PrimaryRepository primaryRepository; @Autowired private SecondaryRepository secondaryRepository; @Test public void TestSave() { System.out.println("************************************************************"); System.out.println("测试开始"); System.out.println("************************************************************"); this.primaryRepository .save(new PrimaryMongoObject(null, "第一个库的对象")); this.secondaryRepository .save(new SecondaryMongoObject(null, "第二个库的对象")); List<PrimaryMongoObject> primaries = this.primaryRepository.findAll(); for (PrimaryMongoObject primary : primaries) { System.out.println(primary.toString()); } List<SecondaryMongoObject> secondaries = this.secondaryRepository.findAll(); for (SecondaryMongoObject secondary : secondaries) { System.out.println(secondary.toString()); } System.out.println("************************************************************"); System.out.println("测试完成"); System.out.println("************************************************************"); } }
 

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!