1.开发环境
IDEA、Maven、SpringBoot2.0.4、Jdk1.8、MongoDB4.0、MongoDB Compass Community、PostMan
2.MongoDB
传统的关系数据库一般由数据库(database)、表(table)、记录(record)三个层次概念组成,MongoDB是由数据库(database)、集合(collection)、文档对象(document)三个层次组成。MongoDB对于关系型数据库里的表,但是集合中没有列、行和关系概念,这体现了模式自由的特点。
3.创建工程
4.配置
无密码下配置
server:
port: 8031
spring:
application:
name: spirng-boot-mongodb
data:
mongodb:
host: localhost #同127.0.0.1
port: 27017
database: test #指定操作的数据库
5.查用API
查询Student文档的全部数据
mongoTemplate.findAll(Student.class)
查询Student文档id为id的数据
mongoTemplate.findById(<id>, Student.class)
根据query内的查询条件查询
mongoTemplate.find(query, Student.class);
修改
mongoTemplate.upsert(query, update, Student.class)
删除
mongoTemplate.remove(query, Student.class)
新增
mongoTemplate.insert(student)
使用方法
Query对象
1 创建一个query对象(用来封装所有条件对象),再创建一个criteria对象(用来构建条件)
2 精准条件:criteria.and(“key”).is(“条件”)
3 模糊条件:criteria.and(“key”).regex(“条件”)
4 封装条件:query.addCriteria(criteria)
5 大于(创建新的criteria):Criteria gt = Criteria.where(“key”).gt(“条件”)
6 小于(创建新的criteria):Criteria lt = Criteria.where(“key”).lt(“条件”)
7 Query.addCriteria(new Criteria().andOperator(gt,lt));
8 一个query中只能有一个andOperator()。其参数也可以是Criteria数组。
9 排序 :query.with(new Sort(Sort.Direction.ASC, "age"). and(newSort(Sort.Direction.DESC, "date")))
实体
@Data
@Document("student")
public class Student{
private String id;
private String username;
private String password;
private int age;
private String gender;
private LocalDateTime createTime;
}
实现
@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, "创建出错");
}
}
}
实体2
@Data
public class Book {
@Id
private String id;
//价格
private Integer price;
//书名
private String name;
//简介
private String info;
//出版社
private String publish;
//创建时间
private Date createTime;
//修改时间
private Date updateTime;
}
实现2
@Service
public class MongoDbService {
private static final Logger logger = LoggerFactory.getLogger(MongoDbService.class);
@Autowired
private MongoTemplate mongoTemplate;
/**
* 保存对象
*
* @param book
* @return
*/
public String saveObj(Book book) {
logger.info("--------------------->[MongoDB save start]");
book.setCreateTime(new Date());
book.setUpdateTime(new Date());
mongoTemplate.save(book);
return "添加成功";
}
/**
* 查询所有
*
* @return
*/
public List<Book> findAll() {
logger.info("--------------------->[MongoDB find start]");
return mongoTemplate.findAll(Book.class);
}
/***
* 根据id查询
* @param id
* @return
*/
public Book getBookById(String id) {
logger.info("--------------------->[MongoDB find start]");
Query query = new Query(Criteria.where("_id").is(id));
return mongoTemplate.findOne(query, Book.class);
}
/**
* 根据名称查询
*
* @param name
* @return
*/
public Book getBookByName(String name) {
logger.info("--------------------->[MongoDB find start]");
Query query = new Query(Criteria.where("name").is(name));
return mongoTemplate.findOne(query, Book.class);
}
/**
* 更新对象
*
* @param book
* @return
*/
public String updateBook(Book book) {
logger.info("--------------------->[MongoDB update start]");
Query query = new Query(Criteria.where("_id").is(book.getId()));
Update update = new Update().set("publish", book.getPublish())
.set("info", book.getInfo())
.set("updateTime", new Date());
//updateFirst 更新查询返回结果集的第一条
mongoTemplate.updateFirst(query, update, Book.class);
//updateMulti 更新查询返回结果集的全部
// mongoTemplate.updateMulti(query,update,Book.class);
//upsert 更新对象不存在则去添加
// mongoTemplate.upsert(query,update,Book.class);
return "success";
}
/***
* 删除对象
* @param book
* @return
*/
public String deleteBook(Book book) {
logger.info("--------------------->[MongoDB delete start]");
mongoTemplate.remove(book);
return "success";
}
/**
* 根据id删除
*
* @param id
* @return
*/
public String deleteBookById(String id) {
logger.info("--------------------->[MongoDB delete start]");
//findOne
Book book = getBookById(id);
//delete
deleteBook(book);
return "success";
}
}
优化使用
完成以上配置,我们springBoot集成MongoDB环境基本已经搭建好了。
但是在使用中会发现一个问题,假如要对数据库操作多个对象,那岂不是每一个对象Service都需要写一套增删查改的方法。
public abstract class MongoDbDao<T> {
protected Logger logger = LoggerFactory.getLogger(MongoDbDao.class);
/**
* 反射获取泛型类型
*
* @return
*/
protected abstract Class<T> getEntityClass();
@Autowired
private MongoTemplate mongoTemplate;
/***
* 保存一个对象
* @param t
*/
public void save(T t) {
logger.info("-------------->MongoDB save start");
this.mongoTemplate.save(t);
}
/***
* 根据id从几何中查询对象
* @param id
* @return
*/
public T queryById(Integer id) {
Query query = new Query(Criteria.where("_id").is(id));
logger.info("-------------->MongoDB find start");
return this.mongoTemplate.findOne(query, this.getEntityClass());
}
/**
* 根据条件查询集合
*
* @param object
* @return
*/
public List<T> queryList(T object) {
Query query = getQueryByObject(object);
logger.info("-------------->MongoDB find start");
return mongoTemplate.find(query, this.getEntityClass());
}
/**
* 根据条件查询只返回一个文档
*
* @param object
* @return
*/
public T queryOne(T object) {
Query query = getQueryByObject(object);
logger.info("-------------->MongoDB find start");
return mongoTemplate.findOne(query, this.getEntityClass());
}
/***
* 根据条件分页查询
* @param object
* @param start 查询起始值
* @param size 查询大小
* @return
*/
public List<T> getPage(T object, int start, int size) {
Query query = getQueryByObject(object);
query.skip(start);
query.limit(size);
logger.info("-------------->MongoDB queryPage start");
return this.mongoTemplate.find(query, this.getEntityClass());
}
/***
* 根据条件查询库中符合条件的记录数量
* @param object
* @return
*/
public Long getCount(T object) {
Query query = getQueryByObject(object);
logger.info("-------------->MongoDB Count start");
return this.mongoTemplate.count(query, this.getEntityClass());
}
/***
* 删除对象
* @param t
* @return
*/
public int delete(T t) {
logger.info("-------------->MongoDB delete start");
return (int) this.mongoTemplate.remove(t).getDeletedCount();
}
/**
* 根据id删除
*
* @param id
*/
public void deleteById(Integer id) {
Criteria criteria = Criteria.where("_id").is(id);
if (null != criteria) {
Query query = new Query(criteria);
T obj = this.mongoTemplate.findOne(query, this.getEntityClass());
logger.info("-------------->MongoDB deleteById start");
if (obj != null) {
this.delete(obj);
}
}
}
/*MongoDB中更新操作分为三种
* 1:updateFirst 修改第一条
* 2:updateMulti 修改所有匹配的记录
* 3:upsert 修改时如果不存在则进行添加操作
* */
/**
* 修改匹配到的第一条记录
* @param srcObj
* @param targetObj
*/
public void updateFirst(T srcObj, T targetObj){
Query query = getQueryByObject(srcObj);
Update update = getUpdateByObject(targetObj);
logger.info("-------------->MongoDB updateFirst start");
this.mongoTemplate.updateFirst(query,update,this.getEntityClass());
}
/***
* 修改匹配到的所有记录
* @param srcObj
* @param targetObj
*/
public void updateMulti(T srcObj, T targetObj){
Query query = getQueryByObject(srcObj);
Update update = getUpdateByObject(targetObj);
logger.info("-------------->MongoDB updateFirst start");
this.mongoTemplate.updateMulti(query,update,this.getEntityClass());
}
/***
* 修改匹配到的记录,若不存在该记录则进行添加
* @param srcObj
* @param targetObj
*/
public void updateInsert(T srcObj, T targetObj){
Query query = getQueryByObject(srcObj);
Update update = getUpdateByObject(targetObj);
logger.info("-------------->MongoDB updateInsert start");
this.mongoTemplate.upsert(query,update,this.getEntityClass());
}
/**
* 将查询条件对象转换为query
*
* @param object
* @return
* @author Jason
*/
private Query getQueryByObject(T object) {
Query query = new Query();
String[] fileds = getFiledName(object);
Criteria criteria = new Criteria();
for (int i = 0; i < fileds.length; i++) {
String filedName = (String) fileds[i];
Object filedValue = getFieldValueByName(filedName, object);
if (filedValue != null) {
criteria.and(filedName).is(filedValue);
}
}
query.addCriteria(criteria);
return query;
}
/**
* 将查询条件对象转换为update
*
* @param object
* @return
* @author Jason
*/
private Update getUpdateByObject(T object) {
Update update = new Update();
String[] fileds = getFiledName(object);
for (int i = 0; i < fileds.length; i++) {
String filedName = (String) fileds[i];
Object filedValue =getFieldValueByName(filedName, object);
if (filedValue != null) {
update.set(filedName, filedValue);
}
}
return update;
}
/***
* 获取对象属性返回字符串数组
* @param o
* @return
*/
private static String[] getFiledName(Object o) {
Field[] fields = o.getClass().getDeclaredFields();
String[] fieldNames = new String[fields.length];
for (int i = 0; i < fields.length; ++i) {
fieldNames[i] = fields[i].getName();
}
return fieldNames;
}
/***
* 根据属性获取对象属性值
* @param fieldName
* @param o
* @return
*/
private static Object getFieldValueByName(String fieldName, Object o) {
try {
String e = fieldName.substring(0, 1).toUpperCase();
String getter = "get" + e + fieldName.substring(1);
Method method = o.getClass().getMethod(getter, new Class[0]);
return method.invoke(o, new Object[0]);
} catch (Exception var6) {
return null;
}
}
}
我们将mongoDB常用的CURD操作封装为通用的父类,然后在不同的业务场景下继承该类,通过泛型和反射获取到正在操作的实体类。
比如我们可以将之前的Book实体类的CURD类进行改造
创建BookMongoDbDao类继承MongoDbDao
@Repository
public class BookMongoDbDao extends MongoDbDao<Book> {
@Override
protected Class<Book> getEntityClass() {
return Book.class;
}
}
接下来我们可以改造Book的Service类
原始版本:
@Service
public class BookMongoDbService {
private static final Logger logger = LoggerFactory.getLogger(BookMongoDbService.class);
@Autowired
private MongoTemplate mongoTemplate;
/**
* 保存对象
*
* @param book
* @return
*/
public String saveObj(Book book) {
logger.info("--------------------->[MongoDB save start]");
book.setCreateTime(new Date());
book.setUpdateTime(new Date());
mongoTemplate.save(book);
return "添加成功";
}
//其他操作方法......
}
改造版本
@Service
public class BookMongoDbService {
private static final Logger logger = LoggerFactory.getLogger(BookMongoDbService.class);
//注入新的CURD操作类
@Autowired
private BookMongoDbDao bookMongoDbDao;
/**
* 保存对象
*
* @param book
* @return
*/
public String saveObj2(Book book) {
book.setCreateTime(new Date());
book.setUpdateTime(new Date());
//调用bookMongoDbDao父类中的添加方法
bookMongoDbDao.save(book);
return "添加成功";
}
}
改造后的saveObj2方法的效果与以前的一致,但是大大的提高了开发效率。不需要重复的编写CURD的方法。
总结
通过以上的配置已经完成springboot与mongoDB集成环境的初步搭建,当然了MongoDB在springboot中的使用不仅于此,还有更多的功能和更优雅的使用方式等待着我们去发掘。