一、依赖
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.0.4</version>
</dependency>
- application.yml
spring:
data:
mongodb:
uri: mongodb://<username>:<password>@47.93.35.143:27017/test
二、实现
常用方法
mongoTemplate.findAll(Student.class)
: 查询Student文档的全部数据
mongoTemplate.findById(<id>, Student.class)
: 查询Student文档id为id的数据
mongoTemplate.find(query, Student.class);
: 根据query内的查询条件查询
mongoTemplate.upsert(query, update, Student.class)
: 修改
mongoTemplate.remove(query, Student.class)
: 删除
mongoTemplate.insert(student)
: 新增
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")))
- 实体类
package com.fzy.javastudy.java.day_0906.model;
import lombok.Data;
import org.springframework.data.mongodb.core.mapping.Document;
import java.time.LocalDateTime;
@Data
@Document("student")
public class Student{
private String id;
private String username;
private String password;
private int age;
private String gender;
private LocalDateTime createTime;
}
- 实现
package com.fzy.javastudy.java.day_0906.service;
import com.fzy.javastudy.java.day_0906.model.*;
import com.fzy.javastudy.spring.apimodel.*;
import com.fzy.javastudy.spring.config.ServiceException;
import com.fzy.javastudy.spring.config.SystemException;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Sort;
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;
import javax.annotation.Resource;
import java.util.List;
import java.util.regex.Pattern;
/**
* https://blog.csdn.net/sinat_35821285/article/details/83511203
*/
@Slf4j
@Service
public class MongoService {
@Resource
private MongoTemplate mongoTemplate;
/**
* 查询全部
*
* @return ApiResponse
*/
public ApiResponse findStudents() {
mongoTemplate.dropCollection(Customer.class);
List<Student> students = mongoTemplate.findAll(Student.class);
System.out.println(students);
long count = mongoTemplate.count(new Query().with(new Sort(Sort.Direction.ASC, "username")), Student.class);
return Api.ok(students, String.format("%s%d%s", "查询到", count, "条"));
}
/**
* 根据id查询
*
* @param id _id
* @return ApiResponse
*/
public ApiResponse findStudentByID(String id) {
Student student = mongoTemplate.findById(id, Student.class);
return Api.ok(student);
}
/**
* 准确查询
*
* @param student Student对象
* @return ApiResponse
*/
public ApiResponse findStudentListByMany(Student student) {
Query query = new Query(Criteria
.where("username").is(student.getUsername())
.and("gender").is(student.getGender())
.and("age").gt(student.getAge()));
List<Student> students = mongoTemplate.find(query, Student.class);
return Api.ok(students);
}
/**
* 模糊查询
* 模糊查询以 【^】开始 以【$】结束 【.*】相当于Mysql中的%
*
* @param username 用户名
* @return ApiResponse
*/
public ApiResponse findStudentsLikeName(String username) {
String regex = String.format("%s%s%s", "^.*", username, ".*$");
Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
Query query = new Query(Criteria.where("username").regex(pattern));
List<Student> students = mongoTemplate.find(query, Student.class);
return Api.ok(students);
}
/**
* 分页查询
*
* @param request
* @return
*/
public PageApiResponse findStudentsPage(StudentRequest request) {
try {
Query query = new Query();
if (StringUtils.isNotEmpty(request.getUsername())) {
String regex = String.format("%s%s%s", "^.*", request.getUsername(), ".*$");
Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
query.addCriteria(Criteria.where("username").regex(pattern));
}
int totalCount = (int) mongoTemplate.count(query, Student.class);
List<Student> studentList = mongoTemplate.find(query.skip(request.getOffset()).limit(request.getPageSize()), Student.class);
PageApiResponse response = PageApi.ok(studentList, totalCount, "获取列表成功");
response.setTotalPage(PageCounter.toTalPage(totalCount, request.getPageSize()));
response.handleRequest(request);
return response;
} catch (Exception e) {
throw new SystemException(-1, "获取分页数据出错");
}
}
/**
* 修改
*
* @param student Student
* @return ApiResponse
*/
public ApiResponse updateStudent(Student student) {
try {
Query query = new Query(Criteria.where("_id").is(student.getId()));
Update update = new Update();
update.set("username", student.getUsername());
update.set("password", student.getPassword());
update.set("age", student.getAge());
update.set("gender", student.getGender());
UpdateResult result = mongoTemplate.upsert(query, update, Student.class);
long count = result.getModifiedCount();
if (count > 0) {
return Api.ok(null, "更新成功");
}
throw new ServiceException(-1, "更新失败");
} catch (ServiceException e) {
throw e;
} catch (Exception e) {
e.printStackTrace();
throw new SystemException(-1, "更新出错");
}
}
/**
* 删除
*
* @param id id
* @return ApiResponse
*/
public ApiResponse delete(String id) {
try {
Query query = new Query(Criteria.where("_id").is(id));
DeleteResult result = mongoTemplate.remove(query, Student.class);
long count = result.getDeletedCount();
if (count > 0) {
return Api.ok(count, "删除成功");
}
throw new ServiceException(-1, String.format("【%s】%s", id, "不存在"));
} catch (ServiceException e) {
throw e;
} catch (Exception e) {
throw new SystemException(-1, "删除出错");
}
}
/**
* 新增
*
* @param student
* @return
*/
public ApiResponse createStudent(Student student) {
try {
Student insert = mongoTemplate.insert(student);
return Api.ok(insert);
} catch (Exception e) {
e.printStackTrace();
throw new SystemException(-1, "创建出错");
}
}
}