给User
的实体类增加了一个属性roleNames
,这个属性在数据库是没有对用字段的,也是需要用mysql
语句查出来的,这个时候要怎么将数据库查出来的roleNames
数据映射到User
的roleNames
属性上,今天早上短路了20分钟,竟然不知道怎么弄,脑袋里怕都是浆糊了。
其实很简单,在mysql
语句中将查出来的roleNames
取别名role_names
,然后将这个别名映射到User
实体类中,OK!
映射:
<resultMap type="SysUser" id="SysUserResult">
<id property="userId" column="user_id" />
<result property="roleNames" column="role_names" />
<result property="deptId" column="dept_id" />
<result property="loginName" column="login_name" />
<result property="userName" column="user_name" />
</resultMap>
取别名:
<select id="listSysUserUnderRole" parameterType="long" resultMap="SysUserResult">
select u.*,temp.roleNames as role_names
from sys_user u
left join sys_dept d on u.dept_id = d.dept_id
left join sys_user_role ur on u.user_id = ur.user_id
left join sys_role r on r.role_id = ur.role_id
join
(
select ur.user_id, group_concat(DISTINCT r.role_name separator '/') as roleNames from sys_user_role ur
join sys_role r
on r.role_id=ur.role_id
where r.del_flag='0' group by ur.user_id
)temp
on temp.user_id = u.user_id
where
u.del_flag='0'
and r.role_id=#{roleId}
</select>
网友评论