整合MongoDB
在pom.xml添加坐标
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
在application文件添加mongodb的配置
spring.data.mongodb.uri=mongodb://106.75.148.160:27017/test
整合完毕,接下来可以实现CRUD功能
添加MyUser实体类
@Data
@Document("MyUser")
public class MyUser {
@Id
private String id;
private String name;
private Integer age;
private String email;
private String createDate;
}
常用方法
mongoTemplate.findAll(MyUser.class): 查询User文档的全部数据
mongoTemplate.findById(<id>, MyUser.class): 查询User文档id为id的数据
mongoTemplate.find(query, MyUser.class);: 根据query内的查询条件查询
mongoTemplate.upsert(query, update, MyUser.class): 修改
mongoTemplate.remove(query, MyUser.class): 删除
mongoTemplate.insert(User): 新增
添加方法
@RequestMapping("testMongoDBAdd")
public String testMongoDBAdd(){
MyUser myUser = new MyUser();
myUser.setAge(22);
myUser.setEmail("123@qq.com");
myUser.setName("李四");
mongoTemplate.insert(myUser);
return "添加成功";
}
查询方法
Query对象
``1、创建一个query对象(用来封装所有条件对象),再创建一个criteria对象(用来构建条件)2、 精准条件:criteria.and(“key”).is(“条件”)模糊条件:criteria.and(“key”).regex(“条件”)3、封装条件:query.addCriteria(criteria)4、大于(创建新的criteria):Criteria gt = Criteria.where(“key”).gt(“条件”)小于(创建新的criteria):Criteria lt = Criteria.where(“key”).lt(“条件”)5、Query.addCriteria(new Criteria().andOperator(gt,lt));6、一个query中只能有一个andOperator()。其参数也可以是Criteria数组。7、排序 :query.with(new Sort(Sort.Direction.ASC, "age"). and(new Sort(Sort.Direction.DESC, "date")))
查询方法
@RequestMapping("testMongoDBfindAll")
public String testMongoDBfindAll(){
List<MTest> mTests = mongoTemplate.findAll( MTest.class);
System.out.println("mTests="+mTests);
return "查询成功";
}
根据ID查询
@RequestMapping("testMongoDBfindById")
public String testMongoDBfindById(){
MyUser myUser = mongoTemplate.findById("", MyUser.class);
System.out.println("myUser="+myUser);
return "查询成功";
}
条件查询
@RequestMapping("testMongoDBCriteria")
public String testMongoDBCriteria(){
Query query = new Query();
Criteria criteria = Criteria.where("name").is("李四");
query.addCriteria(criteria);
List<MyUser> myUsers = mongoTemplate.find(query, MyUser.class);
System.out.println("mTests="+myUsers);
return "查询成功";
}
模糊查询
//模糊查询
@Test
public void findUsersLikeName() {
String name = "est";
String regex = String.format("%s%s%s", "^.*", name, ".*$");
Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
Query query = new Query(Criteria.where("name").regex(pattern));
List<User> userList = mongoTemplate.find(query, User.class);
System.out.println(userList);
}
分页查询
//分页查询
@Test
public void findUsersPage() {
String name = "est";
int pageNo = 1;
int pageSize = 10;
Query query = new Query();
String regex = String.format("%s%s%s", "^.*", name, ".*$");
Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
query.addCriteria(Criteria.where("name").regex(pattern));
int totalCount = (int) mongoTemplate.count(query, User.class);
List<User> userList = mongoTemplate.find(query.skip((pageNo - 1) * pageSize).limit(pageSize), User.class);
Map<String, Object> pageMap = new HashMap<>();
pageMap.put("list", userList);
pageMap.put("totalCount",totalCount);
System.out.println(pageMap);
}
修改方法
//修改
@RequestMapping("updateMyUser")
public String updateMyUser() {
MyUser MyUser = mongoTemplate.findById("5ffbfa2ac290f356edf9b5aa", MyUser.class);
MyUser.setName("test_1");
MyUser.setAge(25);
MyUser.setEmail("12345678@qq.com");
Query query = new Query(Criteria.where("_id").is(MyUser.getId()));
Update update = new Update();
update.set("name", MyUser.getName());
update.set("age", MyUser.getAge());
update.set("email", MyUser.getEmail());
UpdateResult result = mongoTemplate.upsert(query, update, MyUser.class);
long count = result.getModifiedCount();
return "修改用户"+count;
}
删除方法
//删除操作
@RequestMapping("TestMongoDBDelete")
public void TestMongoDBDelete() {
Query query =
new Query(Criteria.where("_id").is("5ff45642ac290f356edf9b5aa"));
DeleteResult result = mongoTemplate.remove(query, MyUser.class);
long count = result.getDeletedCount();
System.out.println(count);
}