首先说一下业务场景
用户表:uid,name等等
user_role表
role表
role_module表
module表
这就是通用的用户——角色——权限系统
现在前端在页面上上要展示如下
不说了上代码,
/**
*query对象主要是分页的,分页采用的是MybatisPlus的分页方法,版本2.x
*
/
public ApiResult pageList(String token,Query query) throws Exception{
Page<User> page=new Page(query.getPageIndex(), query.getPageSize());
page.setRecords(userMapper.select(page,query));
return ResultUtils.buildSucessObject(page);
}
后台的sql
select g.*,h.* from (select e.*,f.module_id from
(select c.*,d.name as role_name,d.role_type from
(select a.uid,a.parent_uid,a.user_name,a.user_status,a.user_tel,a.user_email,a.deleted,a.nick_name,a.shop_id,a.user_headimg, b.role_id from
(select * from qdz_user where shop_id = #{shopId} and deleted=0
<if test="uid != null ">and uid = #{uid}</if>
) a left join qdz_user_role b on a.uid=b.user_id) c
left join qdz_role d on c.role_id=d.id) e left join qdz_role_module f on e.role_id=f.role_id) g left join qdz_module h on g.module_id=h.id
mybatisPlus会自动在后面加上limit
结果
图片有点没对齐,用户和角色目前是一对一就没放出来,角色和权限是一对多,得到的结果映射之后实际上在页面只有一条数据,这是分页就有点尴尬了,对此直接用了一下mysql的group_concat函数,结合groupby效果看一下
select *,
group_concat(i.name separator ';') as module_name from(
select g.*,h.* from (select e.*,f.module_id from
(select c.*,d.name as role_name,d.role_type from
(select a.uid,a.user_name,a.user_status,a.user_tel,a.user_email,a.shop_id, b.role_id from
(select * from qdz_user where shop_id = '1557733645884912056'
) a left join qdz_user_role b on a.uid=b.user_id) c
left join qdz_role d on c.role_id=d.id) e left join qdz_role_module f on e.role_id=f.role_id) g left join qdz_module h on g.module_id=h.id)
i GROUP BY uid
效果
现在就是这种效果,毕竟业务只需要展示就行,一个字符串怼过去。解决!
原创文章,转载请注明出处!