赞
踩
后台用户信息管理共有六个接口
通过岗位id从岗位信息表中查询岗位
查看接口
代码开发
查看请求和响应的数据结构
请求的数据结构
current
和size
为分页相关参数,分别表示当前所处页面和每个页面的记录数。
SystemUserQueryVo
为房间的查询条件,详细结构如下:
@Data
@Schema(description = "员工查询实体")
public class SystemUserQueryVo {
@Schema(description= "员工姓名")
private String name;
@Schema(description= "手机号码")
private String phone;
}
响应的数据结构
单个系统用户信息的结构可查看web-admin模块下的com.atguigu.lease.web.admin.vo.system.user.SystemUserItemVo
,具体内容如下:
@Data
@Schema(description = "后台管理系统用户基本信息实体")
public class SystemUserItemVo extends SystemUser {
@Schema(description = "岗位名称")
@TableField(value = "post_name")
private String postName;
}
编写Controller层逻辑
在SystemUserController
中增加如下内容
@Operation(summary = "根据条件分页查询后台用户列表")
@GetMapping("page")
public Result<IPage<SystemUserItemVo>> page(@RequestParam long current, @RequestParam long size, SystemUserQueryVo queryVo) {
IPage<SystemUser> page = new Page<>(current, size);
IPage<SystemUserItemVo> systemUserPage = service.pageSystemUserByQuery(page, queryVo);
return Result.ok(systemUserPage);
}
编写Service层逻辑
在SystemUserService
中增加如下内容
IPage<SystemUserItemVo> pageSystemUserByQuery(IPage<SystemUser> page, SystemUserQueryVo queryVo);
在SystemUserServiceImpl
中增加如下内容
@Override
public IPage<SystemUserItemVo> pageSystemUserByQuery(IPage<SystemUser> page, SystemUserQueryVo queryVo) {
return systemUserMapper.pageSystemUserByQuery(page, queryVo);
}
编写Mapper层逻辑
在SystemUserMapper
中增加如下内容
IPage<SystemUserItemVo> pageSystemUserByQuery(IPage<SystemUser> page, SystemUserQueryVo queryVo);
在SystemUserMapper.xml
中增加如下内容
<select id="pageSystemUserByQuery" resultType="com.atguigu.lease.web.admin.vo.system.user.SystemUserItemVo"> select su.id, username, su.name, type, phone, avatar_url, additional_info, post_id, su.status, sp.name post_name from system_user su left join system_post sp on su.post_id = sp.id and sp.is_deleted = 0 <where> su.is_deleted = 0 <if test="queryVo.name != null and queryVo.name != ''"> and su.name like concat('%',#{queryVo.name},'%') </if> <if test="queryVo.phone !=null and queryVo.phone != ''"> and su.phone like concat('%',#{queryVo.phone},'%') </if> </where> </select>
知识点
password
字段不要查询。
查看接口
代码开发
编写Controller层逻辑
在SystemUserController
中增加如下内容
@Operation(summary = "根据ID查询后台用户信息")
@GetMapping("getById")
public Result<SystemUserItemVo> getById(@RequestParam Long id) {
SystemUserItemVo systemUser = service.getSystemUserById(id);
return Result.ok(systemUser);
}
编写Service层逻辑
在SystemUserServcie
中增加如下内容
SystemUserItemVo getSystemUserById(Long id);
在SystemUserServcieImpl
中增加如下内容
@Override
public SystemUserItemVo getSystemUserById(Long id) {
SystemUser systemUser = systemUserMapper.selectById(id);
SystemPost systemPost = systemPostMapper.selectById(systemUser.getPostId());
SystemUserItemVo systemUserItemVo = new SystemUserItemVo();
BeanUtils.copyProperties(systemPost, systemUserItemVo);
systemUserItemVo.setPostName(systemUserItemVo.getPostName());
return systemUserItemVo;
}
知识点
system_user
表中的password
字段不应查询,需要在SystemUser
的password
字段的@TableField
注解中增加select=false
参数。
查看接口
代码开发
编写Controller层逻辑
在SystemUserController
中增加如下内容
@Operation(summary = "保存或更新后台用户信息")
@PostMapping("saveOrUpdate")
public Result saveOrUpdate(@RequestBody SystemUser systemUser) {
if(systemUser.getPassword() != null){
systemUser.setPassword(DigestUtils.md5Hex(systemUser.getPassword()));
}
service.saveOrUpdate(systemUser);
return Result.ok();
}
知识点:
密码处理
用户的密码通常不会直接以明文的形式保存到数据库中,而是会先经过处理,然后将处理之后得到的"密文"保存到数据库,这样能够降低数据库泄漏导致的用户账号安全问题。
密码通常会使用一些单向函数进行处理,如下图所示
常用于处理密码的单向函数(算法)有MD5、SHA-256等,Apache Commons提供了一个工具类DigestUtils
,其中就包含上述算法的实现。
Apache Commons是Apache软件基金会下的一个项目,其致力于提供可重用的开源软件,其中包含了很多易于使用的现成工具。
使用该工具类需引入commons-codec
依赖,在common模块的pom.xml中增加如下内容
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</dependency>
Mybatis-Plus update strategy
使用Mybatis-Plus提供的更新方法时,若实体中的字段为null
,默认情况下,最终生成的update语句中,不会包含该字段。若想改变默认行为,可做以下配置。
全局配置
在application.yml
中配置如下参数
mybatis-plus:
global-config:
db-config:
update-strategy: <strategy>
注:上述<strategy>
可选值有:ignore
、not_null
、not_empty
、never
,默认值为not_null
ignore
:忽略空值判断,不管字段是否为空,都会进行更新
not_null
:进行非空判断,字段非空才会进行判断
not_empty
:进行非空判断,并进行非空串(“”)判断,主要针对字符串类型
never
:从不进行更新,不管该字段为何值,都不更新
局部配置
在实体类中的具体字段通过@TableField
注解进行配置,如下:
@Schema(description = "密码")
@TableField(value = "password", updateStrategy = FieldStrategy.NOT_EMPTY)
private String password;
查看接口
代码开发
在SystemUserController
中增加如下内容
@Operation(summary = "判断后台用户名是否可用")
@GetMapping("isUserNameAvailable")
public Result<Boolean> isUsernameExists(@RequestParam String username) {
LambdaQueryWrapper<SystemUser> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(SystemUser::getUsername, username);
long count = service.count(queryWrapper);
return Result.ok(count == 0);
}
查看接口
代码开发
在SystemUserController
中增加如下内容
@DeleteMapping("deleteById")
@Operation(summary = "根据ID删除后台用户信息")
public Result removeById(@RequestParam Long id) {
service.removeById(id);
return Result.ok();
}
查看接口
代码开发
在SystemUserController
中增加如下内容
@Operation(summary = "根据ID修改后台用户状态")
@PostMapping("updateStatusByUserId")
public Result updateStatusByUserId(@RequestParam Long id, @RequestParam BaseStatus status) {
LambdaUpdateWrapper<SystemUser> updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.eq(SystemUser::getId, id);
updateWrapper.set(SystemUser::getStatus, status);
service.update(updateWrapper);
return Result.ok();
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。