用户服务用Relations注解查询取代Join查询
This commit is contained in:
parent
254dfe383b
commit
9e4171f4ad
@ -159,9 +159,7 @@ public class SysUserController extends BaseController {
|
|||||||
}
|
}
|
||||||
userInfoVo.setUser(sysUser);
|
userInfoVo.setUser(sysUser);
|
||||||
|
|
||||||
List<SysRoleVo> userRoles = roleService.selectUserRolesByUserId(userId);
|
userInfoVo.setRoleIds(StreamUtils.toList(sysUser.getRoles(), SysRoleVo::getRoleId));
|
||||||
userInfoVo.setRoleIds(StreamUtils.toList(userRoles, SysRoleVo::getRoleId));
|
|
||||||
//userInfoVo.setRoleIds(StreamUtils.toList(sysUser.getRoles(), SysRoleVo::getRoleId));
|
|
||||||
userInfoVo.setPostIds(postService.selectPostListByUserId(userId));
|
userInfoVo.setPostIds(postService.selectPostListByUserId(userId));
|
||||||
}
|
}
|
||||||
return R.ok(userInfoVo);
|
return R.ok(userInfoVo);
|
||||||
|
@ -2,8 +2,8 @@ package com.ruoyi.system.domain.vo;
|
|||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
import com.mybatisflex.annotation.Column;
|
|
||||||
import com.mybatisflex.annotation.ColumnMask;
|
import com.mybatisflex.annotation.ColumnMask;
|
||||||
|
import com.mybatisflex.annotation.RelationManyToMany;
|
||||||
import com.mybatisflex.annotation.RelationOneToOne;
|
import com.mybatisflex.annotation.RelationOneToOne;
|
||||||
import com.mybatisflex.core.mask.Masks;
|
import com.mybatisflex.core.mask.Masks;
|
||||||
import com.ruoyi.common.orm.core.domain.BaseEntity;
|
import com.ruoyi.common.orm.core.domain.BaseEntity;
|
||||||
@ -12,7 +12,6 @@ import io.github.linpeilie.annotations.AutoMapper;
|
|||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
import java.io.Serial;
|
import java.io.Serial;
|
||||||
import java.io.Serializable;
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
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;
|
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;
|
private List<SysRoleVo> roles;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -285,7 +285,7 @@ public class SysUserServiceImpl extends BaseServiceImpl<SysUserMapper, SysUser>
|
|||||||
QueryWrapper queryWrapper = buildOneQueryWrapper()
|
QueryWrapper queryWrapper = buildOneQueryWrapper()
|
||||||
.where(SYS_USER.TENANT_ID.eq(tenantId))
|
.where(SYS_USER.TENANT_ID.eq(tenantId))
|
||||||
.and(SYS_USER.USER_NAME.eq(userName));
|
.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()
|
QueryWrapper queryWrapper = buildOneQueryWrapper()
|
||||||
.where(SYS_USER.TENANT_ID.eq(tenantId))
|
.where(SYS_USER.TENANT_ID.eq(tenantId))
|
||||||
.and(SYS_USER.PHONENUMBER.eq(phonenumber));
|
.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()
|
QueryWrapper queryWrapper = buildOneQueryWrapper()
|
||||||
.where(SYS_USER.TENANT_ID.eq(tenantId))
|
.where(SYS_USER.TENANT_ID.eq(tenantId))
|
||||||
.and(SYS_USER.EMAIL.eq(email));
|
.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()
|
QueryWrapper queryWrapper = buildOneQueryWrapper()
|
||||||
.where(SYS_USER.TENANT_ID.eq(tenantId))
|
.where(SYS_USER.TENANT_ID.eq(tenantId))
|
||||||
.and(SYS_USER.USER_ID.eq(userId));
|
.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
|
@Override
|
||||||
public SysUserVo selectUserById(Long userId) {
|
public SysUserVo selectUserById(Long userId) {
|
||||||
//QueryWrapper queryWrapper = buildOneQueryWrapper();//当角色有多个时,如果使用leftjoin角色表,后面在提取角色时会出错,所以弃用!
|
//QueryWrapper queryWrapper = buildOneQueryWrapper();//当角色有多个时,如果使用leftjoin角色表,后面通过关联查询在提取角色时会出错,所以弃用!
|
||||||
QueryWrapper queryWrapper = query();
|
QueryWrapper queryWrapper = query();
|
||||||
if (ObjectUtil.isNotNull(userId)) {
|
if (ObjectUtil.isNotNull(userId)) {
|
||||||
queryWrapper.where(SYS_USER.USER_ID.eq(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注解获取dept、roles、从sys_oss中查询头像地址URL
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -366,16 +366,17 @@ public class SysUserServiceImpl extends BaseServiceImpl<SysUserMapper, SysUser>
|
|||||||
public SysUserVo selectProfileUserById(Long userId) {
|
public SysUserVo selectProfileUserById(Long userId) {
|
||||||
//使用leftjoin取得部门名称
|
//使用leftjoin取得部门名称
|
||||||
QueryWrapper queryWrapper = QueryWrapper.create()
|
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,
|
.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")
|
//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"))
|
.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));
|
.where(SYS_USER.DEL_FLAG.eq(0));
|
||||||
if (ObjectUtil.isNotNull(userId)) {
|
if (ObjectUtil.isNotNull(userId)) {
|
||||||
queryWrapper.and(SYS_USER.USER_ID.eq(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)) {
|
if (CollectionUtils.isEmpty(list)) {
|
||||||
return StringUtils.EMPTY;
|
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)) {
|
if (CollectionUtils.isEmpty(list)) {
|
||||||
return StringUtils.EMPTY;
|
return StringUtils.EMPTY;
|
||||||
}
|
}
|
||||||
return list.stream().map(SysPostVo::getPostName).collect(Collectors.joining(","));
|
return list.stream().map(SysPostVo::getPostName).collect(Collectors.joining(","));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user