1.查询管理员列表
1.1持久层
在根包下创建pojo.vo.AdminListItemVO类,在此类中添加属性:
id, username, nickname, avatar, phone, email, description, enable, last_login_ip, login_count, gmt_last_login
在AdminMapper接口中添加抽象方法:
List<AdminListItemVO> list();
在AdminMapper.xml中配置SQL语句:
<select id="list" resultMap="ListResultMap">
SELECT
<include refid="ListQueryFields"/>
FROM
ams_admin
ORDER BY
id
</select>
<sql id="ListQueryFields">
<if test="true">
id, username, nickname, avatar, phone,
email, description, enable, last_login_ip, login_count,
gmt_last_login
</if>
</sql>
<resultMap id="ListResultMap" type="cn.tedu.csmall.passport.pojo.vo.AdminListItemVO">
<id column="id" property="id"/>
<result column="username" property="username" />
<result column="nickname" property="nickname" />
<result column="avatar" property="avatar" />
<result column="phone" property="phone" />
<result column="email" property="email" />
<result column="description" property="description" />
<result column="enable" property="enable" />
<result column="last_login_ip" property="lastLoginIp" />
<result column="login_count" property="loginCount" />
<result column="gmt_last_login" property="gmtLastLogin" />
</resultMap>
在AdminMapperTests中编写并执行测试:
@Test
void testList() {
List<?> list = mapper.list();
log.debug("查询管理员列表,结果集中的数据的数量:{}", list.size());
for (Object item : list) {
log.debug("{}", item);
}
}
1.2业务逻辑层
在IAdminService接口中添加:
List<AdminListItemVO> list();
在AdminServiceImpl中实现以上方法:
public List<AdminListItemVO> list() {
// 日志
return adminMapper.list();
}
在AdminServiceTests中编写并执行测试:
@Test
void testList() {
List<?> list = service.list();
log.debug("查询管理员列表,结果集中的数据的数量:{}", list.size());
for (Object item : list) {
log.debug("{}", item);
}
}
1.3控制器层
在AdminController中处理请求:
@ApiOperation("查询管理员列表")
@ApiOperationSupport(order = 400)
@PreAuthorize("hasAuthority('/ams/admin/read')")
@GetMapping("")
public JsonResult<List<AdminListItemVO>> list() {
log.debug("准备处理【查询管理员列表】的请求");
List<AdminListItemVO> list = adminService.list();
return JsonResult.ok(list);
}
2.删除管理员
2.1持久层
当需要删除管理员时,需要执行的SQL语句大致是:
delete from ams_admin where id=?
在执行删除之前,还应该检查此管理员的数据是否存在,可以通过“根据id查询管理员数据”来实现,需要执行的SQL语句大致是:
select id, username, nickname …… from ams_admin where id=?
另外,由于各管理员都存在与角色的关联,当确定删除管理员数据时,此管理员与角色的关联数据也应该删除,需要执行的SQL语句大致是:
delete from ams_admin_role where admin_id=?
则需要:
- 在根包下创建
pojo.vo.AdminStandardVO类 - 在
AdminMapper接口中添加抽象方法:AdminStandardVO getStandardById(Long id); - 在
AdminMapper接口中添加抽象方法:int deleteById(Long id); - 在
AdminRoleMapper接口中添加抽象方法:int deleteByAdminId(Long adminId);
2.1.1AdminMapper.java
int deleteById(Long id);
AdminStandardVO getStandardById(Long id);
2.1.2AdminMapper.xml
<!-- AdminStandardVO getStandardById(Long id); -->
<select id="getStandardById" resultMap="StandardResultMap">
SELECT
<include refid="StandardQueryFields"/>
FROM
ams_admin
WHERE
id=#{id}
</select>
<sql id="StandardQueryFields">
<if test="true">
id, username, password, nickname, avatar,
phone, email, description, enable, last_login_ip,
login_count, gmt_last_login
</if>
</sql>
<resultMap id="StandardResultMap" type="cn.tedu.csmall.passport.pojo.vo.AdminStandardVO">
<id column="id" property="id"/>
<result column="username" property="username" />
<result column="password" property="password" />
<result column="nickname" property="nickname" />
<result column="avatar" property="avatar" />
<result column="phone" property="phone" />
<result column="email" property="email" />
<result column="description" property="description" />
<result column="enable" property="enable" />
<result column="last_login_ip" property="lastLoginIp" />
<result column="login_count" property="loginCount" />
<result column="gmt_last_login" property="gmtLastLogin" />
</resultMap>
2.1.3AdminMapperTests.java
@Test
void testDeleteById() {
Long id = 4L;
int rows = mapper.deleteById(id);
log.debug("根据id={}删除管理员成功,受影响的行数={}", id, rows);
}
@Test
void testGetStandardById() {
Long id = 6L;
Object queryResult = mapper.getStandardById(id);
log.debug("根据id={}查询管理员详情,查询结果={}", id, queryResult);
}
2.1.4AdminRoleMapper.java
/**
* 根据管理员id,删除管理员与角色的关联数据
*
* @param adminId 管理员id
* @return 受影响的行数
*/
int deleteByAdminId(Long adminId);
2.1.5AdminRoleMapper.xml
<!-- int deleteByAdminId(Long adminId); -->
<delete id="deleteByAdminId">
DELETE FROM
ams_admin_role
WHERE
admin_id=#{adminId}
</delete>
2.1.6AdminRoleMapperTests.java
@Test
void testDeleteByAdminId() {
Long adminId = 24L;
int rows = mapper.deleteByAdminId(adminId);
log.debug("根据管理员id={}删除管理员与角色的关联数据,受影响的行数={}", adminId, rows);
}
2.2业务逻辑层
在IAdminService接口中添加抽象方法:
void deleteById(Long id);
在AdminServiceImpl中实现以上方法:
public void deleteById(Long id) {
// 调用adminMapper根据参数id执行查询
// 判断查询结果是否为null
// 抛出ServiceException,业务状态码:40400
// 调用adminMapper根据参数id删除管理员的数据,并获取返回值
// 判断返回值是否不为1
// 抛出ServiceException,业务状态码:DELETE对应的常量
// 调用adminRoleMapper根据参数id删除关联数据,并获取返回值
// 判断返回值是否小于1
// 抛出ServiceException,业务状态码:DELETE对应的常量
}
具体实现为:
@Override
public void deleteById(Long id) {
log.debug("开始处理【根据id删除管理员】的业务");
// 调用adminMapper根据参数id执行查询
AdminStandardVO queryResult = adminMapper.getStandardById(id);
// 判断查询结果是否为null
if (queryResult == null) {
// 抛出ServiceException,业务状态码:40400
String message = "删除管理员失败!尝试访问的数据不存在!";
log.warn(message);
throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
}
// 调用adminMapper根据参数id删除管理员的数据,并获取返回值
int rows = adminMapper.deleteById(id);
// 判断返回值是否不为1
if (rows != 1) {
// 抛出ServiceException,业务状态码:DELETE对应的常量
String message = "删除管理员失败!服务器忙,请稍后再次尝试![错误代码:1]";
log.warn(message);
throw new ServiceException(ServiceCode.ERR_DELETE, message);
}
// 调用adminRoleMapper根据参数id删除关联数据,并获取返回值
rows = adminRoleMapper.deleteByAdminId(id);
// 判断返回值是否小于1
if (rows < 1) {
// 抛出ServiceException,业务状态码:DELETE对应的常量
String message = "删除管理员失败!服务器忙,请稍后再次尝试![错误代码:2]";
log.warn(message);
throw new ServiceException(ServiceCode.ERR_DELETE, message);
}
}
提示:以上方法必须是事务性的!
提示:可能因为错误的测试数据,导致以上业务执行失败,属于正常现象,将这些错误的测试数据清除即可,或使用正确的测试数据来进行测试!
最后,在AdminServiceTests中编写并执行测试:
@Test
void testDeleteById() {
Long id = 10L;
try {
service.deleteById(id);
log.debug("删除管理员成功!");
} catch (ServiceException e) {
log.debug(e.getMessage());
}
}
2.3控制器层
在AdminController中添加处理请求的方法:
@ApiOperation("根据id删除管理员")
@ApiOperationSupport(order = 200)
@ApiImplicitParam(name = "id", value = "管理员id", required = true, dataType = "long")
@PreAuthorize("hasAuthority('/ams/admin/delete')")
@PostMapping("/{id:[0-9]+}/delete")
public JsonResult<Void> deleteById(@PathVariable Long id) {
log.debug("准备处理【根据id删除管理员】的请求:id={}", id);
adminService.deleteById(id);
return JsonResult.ok();
}
3修改管理员账号的启用状态(作业)
3.1持久层
通常,在执行“修改数据”之前,应该对数据进行检查,例如数据是否存在等,此项检查可通过此前完成的查询来实现,本次不需要添加新的查询功能。
关于修改数据,应该使用<set>、<if>等相关标签,实现动态SQL的修改。此项修改功能的开发可以参考product项目中BrandMapper中的int update(Brand brand);的实现。
3.2业务逻辑层
在IAdminService中添加抽象方法:
// Admin admin = new Admin(); admin.setId(id); admin.setEnable(1);
int setEnable(Long id);
// Admin admin = new Admin(); admin.setId(id); admin.setEnable(0);
int setDisable(Long id);
在AdminServiceImpl中实现:
public int setEnable(Long id) {
// 根据id查询管理员数据
// 判断查询结果是否为null
// 是:ServiceException:NOT_FOUND
// 判断查询结果中的enable是否为1
// 是:ServiceException:CONFLICT
// Admin admin = new Admin(); admin.setId(id); admin.setEnable(1);
// 执行更新,获取返回值
// 判断返回值是否不为1
// 是:ServiceException:UPDATE
}
本文介绍了一个系统的管理员操作API,包括查询管理员列表、删除管理员及修改管理员账号启用状态等功能的实现细节。
775

被折叠的 条评论
为什么被折叠?



