用户服务用Relations注解查询取代Join查询

This commit is contained in:
数据小王子 2024-02-06 14:48:25 +08:00
parent 254dfe383b
commit 9e4171f4ad
3 changed files with 23 additions and 17 deletions

View File

@ -159,9 +159,7 @@ public class SysUserController extends BaseController {
}
userInfoVo.setUser(sysUser);
List<SysRoleVo> userRoles = roleService.selectUserRolesByUserId(userId);
userInfoVo.setRoleIds(StreamUtils.toList(userRoles, SysRoleVo::getRoleId));
//userInfoVo.setRoleIds(StreamUtils.toList(sysUser.getRoles(), SysRoleVo::getRoleId));
userInfoVo.setRoleIds(StreamUtils.toList(sysUser.getRoles(), SysRoleVo::getRoleId));
userInfoVo.setPostIds(postService.selectPostListByUserId(userId));
}
return R.ok(userInfoVo);

View File

@ -2,8 +2,8 @@ package com.ruoyi.system.domain.vo;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.mybatisflex.annotation.Column;
import com.mybatisflex.annotation.ColumnMask;
import com.mybatisflex.annotation.RelationManyToMany;
import com.mybatisflex.annotation.RelationOneToOne;
import com.mybatisflex.core.mask.Masks;
import com.ruoyi.common.orm.core.domain.BaseEntity;
@ -12,7 +12,6 @@ import io.github.linpeilie.annotations.AutoMapper;
import lombok.Data;
import java.io.Serial;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
@ -122,11 +121,19 @@ public class SysUserVo extends BaseEntity {
/**
* 部门对象
*/
@RelationOneToOne(
selfField = "deptId", joinSelfColumn = "dept_id",
targetField = "deptId", joinTargetColumn = "dept_id",
targetTable = "sys_dept")
private SysDeptVo dept;
/**
* 角色对象
*/
@RelationManyToMany(
selfField = "userId", joinSelfColumn = "user_id",
targetTable = "sys_role", targetField = "roleId",
joinTargetColumn = "role_id", joinTable = "sys_user_role")
private List<SysRoleVo> roles;
/**

View File

@ -285,7 +285,7 @@ public class SysUserServiceImpl extends BaseServiceImpl<SysUserMapper, SysUser>
QueryWrapper queryWrapper = buildOneQueryWrapper()
.where(SYS_USER.TENANT_ID.eq(tenantId))
.and(SYS_USER.USER_NAME.eq(userName));
return this.getOneAs(queryWrapper, SysUserVo.class);
return userMapper.selectOneWithRelationsByQueryAs(queryWrapper, SysUserVo.class);
});
}
@ -302,7 +302,7 @@ public class SysUserServiceImpl extends BaseServiceImpl<SysUserMapper, SysUser>
QueryWrapper queryWrapper = buildOneQueryWrapper()
.where(SYS_USER.TENANT_ID.eq(tenantId))
.and(SYS_USER.PHONENUMBER.eq(phonenumber));
return this.getOneAs(queryWrapper, SysUserVo.class);
return userMapper.selectOneWithRelationsByQueryAs(queryWrapper, SysUserVo.class);
});
}
@ -319,7 +319,7 @@ public class SysUserServiceImpl extends BaseServiceImpl<SysUserMapper, SysUser>
QueryWrapper queryWrapper = buildOneQueryWrapper()
.where(SYS_USER.TENANT_ID.eq(tenantId))
.and(SYS_USER.EMAIL.eq(email));
return this.getOneAs(queryWrapper, SysUserVo.class);
return userMapper.selectOneWithRelationsByQueryAs(queryWrapper, SysUserVo.class);
});
}
@ -336,7 +336,7 @@ public class SysUserServiceImpl extends BaseServiceImpl<SysUserMapper, SysUser>
QueryWrapper queryWrapper = buildOneQueryWrapper()
.where(SYS_USER.TENANT_ID.eq(tenantId))
.and(SYS_USER.USER_ID.eq(userId));
return this.getOneAs(queryWrapper, SysUserVo.class);
return userMapper.selectOneWithRelationsByQueryAs(queryWrapper, SysUserVo.class);
});
}
@ -348,12 +348,12 @@ public class SysUserServiceImpl extends BaseServiceImpl<SysUserMapper, SysUser>
*/
@Override
public SysUserVo selectUserById(Long userId) {
//QueryWrapper queryWrapper = buildOneQueryWrapper();//当角色有多个时如果使用leftjoin角色表后面在提取角色时会出错所以弃用
//QueryWrapper queryWrapper = buildOneQueryWrapper();//当角色有多个时如果使用leftjoin角色表后面通过关联查询在提取角色时会出错所以弃用
QueryWrapper queryWrapper = query();
if (ObjectUtil.isNotNull(userId)) {
queryWrapper.where(SYS_USER.USER_ID.eq(userId));
}
return userMapper.selectOneWithRelationsByQueryAs(queryWrapper, SysUserVo.class);//使用Relation注解从sys_oss中查询头像地址URL
return userMapper.selectOneWithRelationsByQueryAs(queryWrapper, SysUserVo.class);//使用Relation注解获取deptroles从sys_oss中查询头像地址URL
}
/**
@ -366,16 +366,17 @@ public class SysUserServiceImpl extends BaseServiceImpl<SysUserMapper, SysUser>
public SysUserVo selectProfileUserById(Long userId) {
//使用leftjoin取得部门名称
QueryWrapper queryWrapper = QueryWrapper.create()
.select(QueryMethods.distinct(SYS_USER.USER_ID, SYS_USER.TENANT_ID, SYS_USER.DEPT_ID, SYS_USER.NICK_NAME, SYS_USER.USER_NAME, SYS_USER.USER_TYPE, SYS_USER.EMAIL, SYS_USER.AVATAR, SYS_USER.PHONENUMBER, SYS_USER.PASSWORD, SYS_USER.GENDER, SYS_USER.STATUS, SYS_USER.DEL_FLAG, SYS_USER.LOGIN_IP, SYS_USER.LOGIN_DATE, SYS_USER.CREATE_BY, SYS_USER.CREATE_TIME, SYS_USER.REMARK,
SYS_DEPT.DEPT_ID, SYS_DEPT.PARENT_ID, SYS_DEPT.ANCESTORS, SYS_DEPT.DEPT_NAME, SYS_DEPT.ORDER_NUM, SYS_DEPT.LEADER, SYS_DEPT.STATUS.as("dept_status")
.select(QueryMethods.distinct(SYS_USER.USER_ID, SYS_USER.TENANT_ID, SYS_USER.DEPT_ID, SYS_USER.NICK_NAME, SYS_USER.USER_NAME, SYS_USER.USER_TYPE, SYS_USER.EMAIL, SYS_USER.AVATAR, SYS_USER.PHONENUMBER, SYS_USER.PASSWORD, SYS_USER.GENDER, SYS_USER.STATUS, SYS_USER.DEL_FLAG, SYS_USER.LOGIN_IP, SYS_USER.LOGIN_DATE, SYS_USER.CREATE_BY, SYS_USER.CREATE_TIME, SYS_USER.REMARK
//SYS_DEPT.DEPT_ID, SYS_DEPT.PARENT_ID, SYS_DEPT.ANCESTORS, SYS_DEPT.DEPT_NAME, SYS_DEPT.ORDER_NUM, SYS_DEPT.LEADER, SYS_DEPT.STATUS.as("dept_status")
))
.from(SYS_USER.as("u"))
.leftJoin(SYS_DEPT).as("d").on(SYS_DEPT.DEPT_ID.eq(SYS_USER.DEPT_ID))
//.leftJoin(SYS_DEPT).as("d").on(SYS_DEPT.DEPT_ID.eq(SYS_USER.DEPT_ID))
.where(SYS_USER.DEL_FLAG.eq(0));
if (ObjectUtil.isNotNull(userId)) {
queryWrapper.and(SYS_USER.USER_ID.eq(userId));
}
return this.getOneAs(queryWrapper, SysUserVo.class);
//return this.getOneAs(queryWrapper, SysUserVo.class);
return userMapper.selectOneWithRelationsByQueryAs(queryWrapper, SysUserVo.class);
}
/**
@ -390,7 +391,7 @@ public class SysUserServiceImpl extends BaseServiceImpl<SysUserMapper, SysUser>
if (CollectionUtils.isEmpty(list)) {
return StringUtils.EMPTY;
}
return list.stream().map(SysRoleVo::getRoleName).collect(Collectors.joining(","));
return list.stream().map(SysRoleVo::getRoleName).collect(Collectors.joining(""));
}
/**
@ -405,7 +406,7 @@ public class SysUserServiceImpl extends BaseServiceImpl<SysUserMapper, SysUser>
if (CollectionUtils.isEmpty(list)) {
return StringUtils.EMPTY;
}
return list.stream().map(SysPostVo::getPostName).collect(Collectors.joining(","));
return list.stream().map(SysPostVo::getPostName).collect(Collectors.joining(""));
}
/**