1.用户管理
1.多条件综合查询
1.1 多条件综合查询接口说明
1)原型效果
2)接口说明
功能描述:多条件综合查询用户分页信息,条件包含:分页信息 用户创建日期范围
服务路径:/api/users
服务方法:Post
请求参数格式:
{
"pageNum":"1",
"pageSize":"20",
"username":"",
"nickName":"",
"startTime":"",
"endTime":""
}
响应数据格式:
{
"code": 1,
"data": {
"totalRows": 12,
"totalPages": 1,
"pageNum": 1,
"pageSize": 20,
"size": 12,
"rows": [
{
"id": 1237361915165020161,
"username": "admin",
"password": "$2a$10$JqoiFCw4LUj184ghgynYp.4kW5BVeAZYjKqu7xEKceTaq7X3o4I4W",
"phone": "13888888888",
"realName": "小池",
"nickName": "超级管理员",
"email": "875267425@qq.com",
"status": 1,
"sex": 1,
"deleted": 1,
"createId": null,
"updateId": "1237361915165020161",
"createWhere": 1,
"createTime": "2019-09-22T11:38:05.000+00:00",
"updateTime": "2020-04-07T10:08:52.000+00:00",
"createUserName": null,
"updateUserName": "admin"
},
//.....
]
}
}
代码实现
1. 表现层
/**
* 多条件综合查询
*/
@ApiOperation(value = "多条件综合查询")
@PostMapping("/users")
public R<UserRespVo> userManager(@RequestBody UserReqVo userReqVo){
return UserService.userManager(userReqVo);
}
2. 服务层
R<UserRespVo> userManager(UserReqVo userReqVo);
@Override
public R<UserRespVo> userManager(UserReqVo userReqVo) {
// 获取分页数据
Integer pageNum = Integer.parseInt(userReqVo.getPageNum());
Integer pageSize = Integer.parseInt(userReqVo.getPageSize());
// 获取查询条件
String username = userReqVo.getUsername();
String nickName = userReqVo.getNickName();
String startTimeString = userReqVo.getStartTime();
String endTimeString = userReqVo.getEndTime();
Date startTime = null;
Date endTime = null;
if (!StringUtils.isBlank(startTimeString) && !StringUtils.isBlank(endTimeString)){
startTime = DateTime.parse(startTimeString, DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss")).toDate();
endTime = DateTime.parse(endTimeString, DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss")).toDate();
}
// 设置分页参数
PageHelper.startPage(pageNum,pageSize);
// 查询获得所有数据
List<SysUser> allUser = sysUserMapper.getSUserByIf(username,nickName,startTime,endTime);
// 分页处理
PageInfo<SysUser> userInfo = new PageInfo<>(allUser);
UserRespVo userRespVo = new UserRespVo(userInfo);
return R.ok(userRespVo);
}
3. Dao层
List<SysUser> getSUserByIf(@Param("username") String username, @Param("nickName") String nickName, @Param("startTime") Date startTime, @Param("endTime") Date endTime);
<select id="getSUserByIf" resultType="com.jixu.stock.pojo.entity.SysUser">
SELECT *
FROM sys_user
WHERE 1=1
<if test="username != null and username != ''">
AND username like concat('%' , #{username} , '%')
</if>
<if test="nickName != null and nickName != ''">
AND nick_name like concat('%' , #{nickName} , '%')
</if>
<if test="startTime != null">
AND created_time >= #{startTime}
</if>
<if test="endTime != null">
AND created_time <= #{endTime}
</if>
</select>
1.2 添加用户接口说明
1)原型效果
2)接口说明
功能描述:添加用户信息
服务路径:/api/user
服务方法:Post
请求参数格式:
{
"username":"mike",
"password":"20",
"phone":"15367945613",
"email":"123@qwe.cn",
"nickName":"jane",
"realName":"kangkang",
"sex":"1",
"createWhere":"1",
"status":"1"
}
响应参数格式:
{
"code": 1,
"msg": "操作成功"
}
代码实现
1. 表现层
/**
* 添加用户
*/
@ApiOperation(value = "添加用户")
@PostMapping("/user")
public R<UserRespVo> addUser(@RequestBody AddUserReqVo addUserReqVo, HttpServletRequest request){
return UserService.addUser(addUserReqVo , request);
}
2. 服务层
R<UserRespVo> addUser(AddUserReqVo addUserReqVo, HttpServletRequest request);
@Override
public R<UserRespVo> addUser(AddUserReqVo addUserReqVo, HttpServletRequest request) {
// 校验用户数据是否正确
if (addUserReqVo == null || StringUtils.isBlank(addUserReqVo.getUsername())
|| StringUtils.isBlank(addUserReqVo.getPassword())|| StringUtils.isBlank(addUserReqVo.getPhone())
|| StringUtils.isBlank(addUserReqVo.getEmail())|| StringUtils.isBlank(addUserReqVo.getNickName())
){
return R.error(ResponseCode.DATA_ERROR.getMessage());
}
// 对密码进行加密
String password = addUserReqVo.getPassword();
String encode = passwordEncoder.encode(password);
// 获取createID
HttpSession session = request.getSession();
Long userId = (Long) session.getAttribute("user_id");
SysUser sysUser = new SysUser();
BeanUtils.copyProperties(addUserReqVo,sysUser);
sysUser.setPassword(encode);
sysUser.setCreateId(userId);
// 获取状态性别
Integer status = Integer.valueOf(addUserReqVo.getStatus());
Integer sex = Integer.valueOf(addUserReqVo.getSex());
sysUser.setStatus(status);
sysUser.setSex(sex);
int insert = sysUserMapper.insert(sysUser);
if (insert > 0){
log.info("用户注册成功");
}else {
log.error("用户注册失败");
return R.error(ResponseCode.ERROR.getMessage());
}
return R.ok("操作成功");
}
3. Dao层
int insert(SysUser record);
<insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.jixu.stock.pojo.entity.SysUser" useGeneratedKeys="true">
insert into sys_user
( id,username,password
,phone,real_name,nick_name
,email,status,sex
,deleted,create_id,update_id
,create_where,create_time,update_time
)
values (#{id,jdbcType=BIGINT},#{username,jdbcType=VARCHAR},#{password,jdbcType=VARCHAR}
,#{phone,jdbcType=VARCHAR},#{realName,jdbcType=VARCHAR},#{nickName,jdbcType=VARCHAR}
,#{email,jdbcType=VARCHAR},#{status,jdbcType=TINYINT},#{sex,jdbcType=TINYINT}
,#{deleted,jdbcType=TINYINT},#{createId,jdbcType=BIGINT},#{updateId,jdbcType=BIGINT}
,#{createWhere,jdbcType=TINYINT},#{createTime,jdbcType=TIMESTAMP},#{updateTime,jdbcType=TIMESTAMP}
)
</insert>
1.3 获取用户具有的角色信息接口说明
1)原型效果
2)接口说明
功能描述:获取用户具有的角色信息,以及所有角色信息
服务路径:/user/roles/{userId}
服务方法:Get
请求参数:String userId
响应参数格式:
{
"code": 1,
"data": {
"ownRoleIds": [
1237258113002901515
],
"allRole": [
{
"id": 1237258113002901512,
"name": "超级管理员",
"description": "我是超级管理员",
"status": 1,
"createTime": "2020-01-06T15:37:45.000+00:00",
"updateTime": "2021-12-09T23:08:02.000+00:00",
"deleted": 1
},
{
"id": 1237258113002901513,
"name": "标记用户角色测试",
"description": "标记用户角色测试",
"status": 1,
"createTime": "2020-01-08T02:53:35.000+00:00",
"updateTime": "2021-12-28T10:16:21.000+00:00",
"deleted": 1
},
//..............
]
}
}
代码实现
1. 表现层
/**
* 获取用户具有的角色信息
*/
@ApiOperation(value = "获取用户具有的角色信息")
@GetMapping("/user/roles/{userId}")
public R<Map<String, Object>> getRole(@PathVariable String userId){
return UserService.getRole(userId);
}
2. 服务层
R<Map<String, Object>> getRole(String userId);
@Override
public R<Map<String, Object>> getRole(String userId) {
Long id = Long.valueOf(userId);
// 查询获得用户角色
List<Long> userRoleList = sysUserRoleMapper.getRole(id);
// 查询获得所有角色信息
ArrayList<SysRole> allRoleList = sysRoleMapper.getAllRole();
// 封装数据
HashMap<String, Object> data = new HashMap<>();
data.put("ownRoleIds" , userRoleList);
data.put("allRole",allRoleList);
return R.ok(data);
}
3. Dao层
List<Long> getRole(@Param("id") Long id);
<select id="getRole" resultType="java.lang.Long">
select role_id from sys_user_role where user_id = #{id};
</select>
ArrayList<SysRole> getAllRole();
<select id="getAllRole" resultType="com.jixu.stock.pojo.entity.SysRole">
select id,name,description
,status,create_time,update_time
,deleted
from sys_role;
</select>