Skip to content

Commit

Permalink
refactor: 优化usermapper list的sql
Browse files Browse the repository at this point in the history
  • Loading branch information
damingerdai committed Sep 14, 2024
1 parent 0841a0f commit 69423e2
Showing 1 changed file with 7 additions and 13 deletions.
20 changes: 7 additions & 13 deletions src/main/java/org/daming/hoteler/repository/mapper/UserMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,17 @@ public interface UserMapper {

@Select("""
SELECT
u.id, u.username, u.password, u.password_type,
u.failed_login_attempts, u.account_non_locked, u.lock_time,
COALESCE(ARRAY_TO_JSON(
COALESCE(
ARRAY_AGG(
JSON_BUILD_OBJECT('id', r.id, 'name', r.name, 'description', r.description)
)
FILTER
(WHERE r.id IS NOT NULL)
)
), '[]') AS roles
u.id, u.username, u.password, u.password_type,
u.failed_login_attempts, u.account_non_locked, u.lock_time,
COALESCE(ARRAY_TO_JSON(ARRAY_AGG(
JSON_BUILD_OBJECT('id', r.id, 'name', r.name, 'description', r.description)
) FILTER (WHERE r.id IS NOT NULL)), '[]') AS roles
FROM users u
LEFT JOIN user_roles ur ON ur.user_id = u.id AND ur.deleted_at IS NULL
LEFT JOIN roles r on r.id = ur.role_id and r.deleted_at IS NULL
LEFT JOIN roles r on r.id = ur.role_id AND r.deleted_at IS NULL
WHERE u.deleted_at IS NULL
GROUP BY u.id
""")
""")
@Results(
value = {
@Result(property = "id", column = "id"),
Expand Down

0 comments on commit 69423e2

Please sign in to comment.