spring boot(十一):Spring boot中mongodb的使用
mongodb简介
传统的关系数据库一般由数据库(database)、表(table)、记录(record)三个层次概念组成,
MongoDB是由数据库(database)、集合(collection)、文档对象(document)三个层次组成。
mongodb使用
1、pom包配置
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency> </dependencies>
2、在application.properties中添加配置
spring.data.mongodb.host=127.0.0.1
spring.data.mongodb.port=27017
spring.data.mongodb.username=root
spring.data.mongodb.password=123456
spring.data.mongodb.database=admin
或者
spring.data.mongodb.uri=mongodb://name:pass@localhost:27017/test
多个IP集群可以采用以下配置:
spring.data.mongodb.uri=mongodb://user:pwd@ip1:port1,ip2:port2/database
3、创建数据实体
package cn.cnki.ref.pojo; import java.io.Serializable; public class User implements Serializable { private int id; private String name; private String username; private int age; private String phone; private String email; //无参构造函数必须有,ORM框架调用的就是无参构造函数 public User() { } public User(int id, String name, String username, int age, String phone, String email) { super(); this.id = id; this.name = name; this.username = username; this.age = age; this.phone = phone; this.email = email; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", username=" + username + ", age=" + age + ", phone=" + phone + ", email=" + email + "]"; } }
4.UserMongodbServiceImpl
package cn.cnki.ref.serviceimpl; import cn.cnki.ref.pojo.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Query; import org.springframework.data.mongodb.core.query.Update; import org.springframework.stereotype.Service; @Service public class UserMongodbServiceImpl { @Autowired private MongoTemplate mongoTemplate; /** * 创建对象 * * @param user */ public void saveUser(User user) { mongoTemplate.save(user); } /** * 根据用户名查询对象 * * @param userName * @return */ public User findUserByUserName(String userName) { Query query = new Query(Criteria.where("name").is(userName)); User user = mongoTemplate.findOne(query, User.class); return user; } /** * 更新对象 * * @param user */ public void updateUser(User user) { Query query = new Query(Criteria.where("id").is(user.getId())); Update update = new Update().set("name", user.getName()).set("username", user.getUsername()); //更新查询返回结果集的第一条 mongoTemplate.updateFirst(query, update, User.class); //更新查询返回结果集的所有 // mongoTemplate.updateMulti(query,update,UserEntity.class); } /** * 删除对象 * * @param id */ public void deleteUserById(Long id) { Query query = new Query(Criteria.where("id").is(id)); mongoTemplate.remove(query, User.class); } }
5.测试
package cn.cnki.ref.serviceimpl; import cn.cnki.ref.pojo.User; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import static org.junit.Assert.*; @RunWith(SpringRunner.class) @SpringBootTest public class UserMongodbServiceImplTest { @Autowired private UserMongodbServiceImpl userMongodbServiceImpl; @Test public void saveUser() { User user = new User(1, "mf", "沐风", 20, "15010298065", "123456@qq.com"); userMongodbServiceImpl.saveUser(user); } @Test public void findUserByUserName() { User user = userMongodbServiceImpl.findUserByUserName("mf"); System.out.println(user); } @Test public void updateUser() { User user = new User(); user.setId(1); user.setName("mufeng"); user.setUsername("沐风"); userMongodbServiceImpl.updateUser(user); } @Test public void deleteUserById() { userMongodbServiceImpl.deleteUserById(1l); } }
MongoTemplate