运行环境
jdk版本:jdk1.8.0_202
springboot版本:2.1.6
mongodb版本:4.0.12
启动MongoDB
命令:
cd /usr/local/mongodb/bin/
命令:
mongod -f mongodb.conf
- 查看防火墙状态
命令:
firewall-cmd --state - 关闭防火墙
命令:
systemctl stop firewalld.service
springboot整合MongoDB
1、在pom.xml中添加依赖包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
2、创建用户信息实体类(SysUser.java)
package com.jeff.entity;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.jeff.utils.DateUtils;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import java.util.Date;
/**
* @author Jeff
* @description 用户管理
* @date 2019/11/6
*/
@Data
@Document(collection = "sys_user")
public class SysUser {
@Id
private String id;//主键id
private String loginName;//登陆名
private String password;//密码
private String salt;//盐
private Integer sex;//性别
private Integer status;//状态
private String name;//真实姓名
private String nickName;//昵称
private String phone;//手机号
private String email;//邮箱
@JsonFormat(pattern = DateUtils.SHORT_TIME_FORMAT, timezone = "GMT+8")
private Date birthday;//生日
private String headimgUrl;//用户头像
private Long roleId;//角色id
@JsonFormat(pattern = DateUtils.LONG_TIME_FORMAT, timezone = "GMT+8")
private Date createTime;//创建时间
private String createName;//创建人
@JsonFormat(pattern = DateUtils.LONG_TIME_FORMAT, timezone = "GMT+8")
private Date modifyTime;//修改时间
private String modifyName;//修改人
}
3、创建用户信息controller类(SysUserController.java)
package com.jeff.controller;
import com.jeff.entity.SysUser;
import com.jeff.entity.request.PageEntity;
import com.jeff.enums.ResponseCodes;
import com.jeff.service.SysUserService;
import com.jeff.utils.Constant;
import com.jeff.utils.ResultsUtils;
import org.apache.commons.lang3.Validate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @author Jeff
* @description 用户管理
* @date 2019/11/6
*/
@RestController
public class SysUserController {
@Autowired
private SysUserService sysUserService;
/**
* @param user, page
* @return java.lang.Object
* @description 获取用户列表(分页)
* @author Jeff
* @date 2019/11/6
*/
@RequestMapping("getUserList")
public Object getUserList(SysUser user, PageEntity page) {
List<SysUser> results = sysUserService.getUserList(user, page);
return ResultsUtils.success(results, page.getTotalCount());
}
/**
* @param user
* @return java.lang.Object
* @description 新增用户
* @author Jeff
* @date 2019/11/6
*/
@PostMapping("add")
public Object add(SysUser user) {
if (sysUserService.isExist(user)) {
return ResultsUtils.error(ResponseCodes.PARAMTER_ERROR, "登录名重复,请重新输入");
}
sysUserService.save(user);
return ResultsUtils.success();
}
/**
* @param user
* @return java.lang.Object
* @description 修改用户
* @author Jeff
* @date 2019/11/6
*/
@PostMapping("update")
public Object update(SysUser user) {
Validate.notNull(user.getId(), Constant.CHECK_MESSAGE, "用户id");
if (sysUserService.isExist(user)) {
return ResultsUtils.error(ResponseCodes.PARAMTER_ERROR, "登录名重复,请重新输入");
}
SysUser u=sysUserService.getUserById(user.getId());
user.setCreateTime(u.getCreateTime());
user.setCreateName(u.getCreateName());
sysUserService.save(user);
return ResultsUtils.success();
}
/**
* @param id
* @return java.lang.Object
* @description 删除用户
* @author Jeff
* @date 2019/11/6
*/
@RequestMapping("delete")
public Object delete(String id) {
Validate.notNull(id, Constant.CHECK_MESSAGE, "用户id");
sysUserService.delete(id);
return ResultsUtils.success();
}
/**
* @param id
* @return java.lang.Object
* @description 按主键id查看用户
* @author Jeff
* @date 2019/11/6
*/
@RequestMapping("getUser")
public Object getUser(String id) {
Validate.notNull(id, Constant.CHECK_MESSAGE, "用户id");
return ResultsUtils.success(sysUserService.getUserById(id));
}
}
4、创建用户信息service类(SysUserService.java)
package com.jeff.service;
import com.jeff.entity.SysUser;
import com.jeff.entity.request.PageEntity;
import com.jeff.utils.PageUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
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.stereotype.Service;
import java.util.Collections;
import java.util.Date;
import java.util.List;
/**
* @author Jeff
* @description 用户管理
* @date 2019/11/6
*/
@Service
public class SysUserService {
@Autowired
private MongoTemplate mongoTemplate;
/**
* @param user, page
* @return java.util.List<com.jeff.entity.SysUser>
* @description 获取用户列表(分页)
* @author Jeff
* @date 2019/11/6
*/
public List<SysUser> getUserList(SysUser user, PageEntity page) {
Page<SysUser> pageData = selectUserList(user, page);
page.setTotalCount(pageData.getTotalElements());
return pageData.getContent();
}
private Page<SysUser> selectUserList(SysUser user, PageEntity page) {
Query query = new Query();
if (StringUtils.isNotBlank(user.getLoginName())) {
query.addCriteria(Criteria.where("loginName").regex(user.getLoginName()));
}
if (user.getSex() != null) {
query.addCriteria(Criteria.where("sex").is(user.getSex()));
}
Pageable pageable = PageUtils.createPageable(page);
query.with(pageable).with(new Sort(Sort.Direction.ASC, "createTime"));
List<SysUser> list = mongoTemplate.find(query, SysUser.class);
long total = mongoTemplate.count(query, SysUser.class);
List<SysUser> content = total > pageable.getOffset() ? list : Collections.<SysUser>emptyList();
return new PageImpl<>(content, pageable, total);
}
/**
* @param user
* @return boolean
* @description 登录名是否已存在
* @author Jeff
* @date 2019/11/6
*/
public boolean isExist(SysUser user) {
Query query = new Query();
query.addCriteria(Criteria.where("loginName").is(user.getLoginName()));
if (user.getId() != null) {
query.addCriteria(Criteria.where("id").ne(user.getId()));
}
return mongoTemplate.findOne(query, SysUser.class) != null;
}
/**
* @param user
* @return void
* @description 保存或更新,根据id是否为null判断更新还是插入
* @author Jeff
* @date 2019/11/6
*/
public void save(SysUser user) {
if (user.getId() == null) {
user.setCreateTime(new Date());
}
user.setModifyTime(new Date());
mongoTemplate.save(user);
}
/**
* @param id
* @return void
* @description 按主键id删除数据
* @author Jeff
* @date 2019/11/6
*/
public void delete(String id) {
Query query=new Query(Criteria.where("id").is(id));
mongoTemplate.remove(query,SysUser.class);
}
/**
* @param id
* @return com.jeff.entity.SysUser
* @description 按主键id查看用户
* @author Jeff
* @date 2019/11/6
*/
public SysUser getUserById(String id) {
return mongoTemplate.findById(id, SysUser.class);
}
}
5、创建全局配置文件(application.properties)
spring.data.mongodb.host=192.168.1.106
spring.data.mongodb.port=27017
spring.data.mongodb.database=jeff
6、启动springboot项目,打开浏览器访问 http://localhost:8080/getUserList?page=1&rows=5