feat(user): 增强用户列表分页查询功能

- 新增 keyword 参数支持用户名和昵称模糊搜索
- 新增 roleId 参数支持按角色过滤用户
- 优化 SQL 查询逻辑,使用 EXISTS 子查询关联用户与角色
- 更新 Mapper 接口以支持新的查询参数
- 调整 Service 和 Controller 层方法签名适配新参数
- 添加请求日志记录便于调试和监控
This commit is contained in:
Chaos
2025-12-01 17:27:20 +08:00
parent 9609f95e5e
commit 50296e8fce
4 changed files with 33 additions and 21 deletions

View File

@@ -91,7 +91,6 @@ public interface UserMapper extends BaseMapper<User> {
""")
List<Role> findRolesByUserId(@Param("userId") Long userId);
@Select("SELECT * FROM t_user WHERE username = #{username} ")
@Results({
@Result(id = true, property = "id", column = "id"),
@@ -103,24 +102,31 @@ public interface UserMapper extends BaseMapper<User> {
many = @Many(select = "findRolesByUserId"))
})
User findUserWithRolesByUsername(String username);
/**
* 分页查询用户,并携带角色信息
* MyBatis-Plus 会自动处理 Page 参数进行物理分页
* @param page 分页对象
* @param keyword 搜索关键词 (可选)
* @return 分页后的用户列表 (包含角色)
* 增加了 roleId 过滤
*/
@Select("""
<script>
SELECT * FROM t_user
<where>
<if test='keyword != null and keyword != ""'>
AND (username LIKE CONCAT('%', #{keyword}, '%') OR nickname LIKE CONCAT('%', #{keyword}, '%'))
</if>
</where>
ORDER BY id ASC
</script>
""")
<script>
SELECT * FROM t_user u
<where>
<if test='keyword != null and keyword != ""'>
AND (u.username LIKE CONCAT('%', #{keyword}, '%') OR u.nickname LIKE CONCAT('%', #{keyword}, '%'))
</if>
<if test='roleId != null'>
AND EXISTS (
SELECT 1
FROM t_user_role ur
WHERE ur.user_id = u.id
AND ur.role_id = #{roleId}
)
</if>
</where>
ORDER BY u.id ASC
</script>
""")
@Results({
@Result(id = true, property = "id", column = "id"),
@Result(property = "username", column = "username"),
@@ -129,5 +135,7 @@ public interface UserMapper extends BaseMapper<User> {
@Result(property = "roles", column = "id",
many = @Many(select = "findRolesByUserId"))
})
IPage<User> selectPageWithRoles(Page<User> page, @Param("keyword") String keyword);
IPage<User> selectPageWithRoles(Page<User> page,
@Param("keyword") String keyword,
@Param("roleId") Long roleId);
}