springboot整合MongoDB常用方法
阿里
阅读:672
2021-04-01 11:25:31
评论:0
1、查询全部
private List<SysUser> selectUserList() {
return mongoTemplate.findAll(SysUser.class);
}
2、保存/修改
public void save(SysUser user) {
mongoTemplate.save(user);
}
3、按主键查询
public SysUser getUserById(String id) {
return mongoTemplate.findById(id, SysUser.class);
}
4、按主键修改(更新所有的值)
public Object update(SysUser user) {
SysUser oldObj = sysUserService.getUserById(user.getId());
user.setCreateTime(oldObj.getCreateTime());
user.setCreateName(oldObj.getCreateName());
user.setModifyTime(new Date());
sysUserService.save(user);
return ResultsUtils.success();
}
5、按条件修改(更新指定字段的值)
public void update(SysUser user) {
Query query = new Query();
query.addCriteria(Criteria.where("_id").is(user.getId()));
Update update = new Update();
update.set("loginName", user.getLoginName());
mongoTemplate.upsert(query, update, "sys_user");
}
6、按主键删除
public void delete(String id) {
Query query=new Query(Criteria.where("id").is(id));
mongoTemplate.remove(query,SysUser.class);
}
7、按条件精确查询
public List<SysUser> getUserList(String loginName) {
Query query = new Query();
if(StringUtils.isNotBlank(loginName)){
query.addCriteria(Criteria.where("loginName").is(loginName));
}
return mongoTemplate.find(query, SysUser.class);
}
8、按条件模糊查询
public List<SysUser> getUserList(String loginName){
Query query = new Query();
query.addCriteria(Criteria.where("loginName").regex(loginName));
return mongoTemplate.find(query, SysUser.class);
}
9、按时间段查询并排序
public List<SysUser> getUserList(String startDate, String endDate) throws Exception{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Query query = new Query();
if (StringUtils.isNotBlank(startDate)&&StringUtils.isNotBlank(endDate)) {
query.addCriteria(Criteria.where("createTime").gte(sdf.parse(startDate)).lte(sdf.parse(endDate)));
}else if(StringUtils.isNotBlank(startDate)){
query.addCriteria(Criteria.where("createTime").gte(sdf.parse(startDate)));
}else if(StringUtils.isNotBlank(endDate)){
query.addCriteria(Criteria.where("createTime").lte(sdf.parse(endDate)));
}
query.with(new Sort(Sort.Direction.DESC, "createTime"));
return mongoTemplate.find(query, SysUser.class);
}
10、保存关联对象
public Object add(SysUser user) {
// SysRole role=sysRoleService.getRoleById(user.getRoleId());
user.setSysRole(new SysRole(user.getRoleId()));
user.setCreateTime(new Date());
user.setCreateName("Jeff");
sysUserService.save(user);
return ResultsUtils.success();
}
11、按关联对象主键查询
public SysUser getUserByRoleId(String roleId) {
Query query = new Query();
query.addCriteria(Criteria.where("sysRole.$id").is(new ObjectId(roleId)));
return mongoTemplate.findOne(query, SysUser.class);
}
12、复杂条件查询
public List<SysUser> getUserList(String loginName,String password) {
Query query = new Query();
Criteria criteria = new Criteria();
Criteria loginNameCri = new Criteria();
Criteria emailCri = new Criteria();
loginNameCri.andOperator(Criteria.where("loginName").is(loginName),Criteria.where("password").is(password));
emailCri.andOperator(Criteria.where("email").is(loginName),Criteria.where("password").is(password));
criteria.orOperator(loginNameCri,emailCri);
query.addCriteria(criteria);
return mongoTemplate.find(query, SysUser.class);
}
声明
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。