http://xiangai.taobao.com
http://shop148612228.taobao.com

MYBATIS多层嵌套查询

基于权限管理时的用户角色权限处理。

实体类:

  1.User

  public class User{
private Integer uid;
private String username;
private String password;
private Set<Role> roles=new HashSet<>();
 2.Role
  public class Role {
private Integer rid;
private String rname;
private Set<Module> modules=new HashSet<>();
 3.Module
  

Mapper 文件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<resultMap id="userMap2" type="com.example.mapper.entity.User">
       <id property="uid" column="uid"/>
       <result property="username" column="username"/>
       <result property="password" column="password"/>
       <collection property="roles" ofType="roleMap" column="uid" select="findRole"></collection>
   </resultMap>
 
   <resultMap id="roleMap" type="com.example.mapper.entity.Role">
       <id property="rid" column="rid"/>
       <result property="rname" column="rname"/>
       <collection property="modules" ofType="com.example.mapper.entity.Module" column="rid" select="findModule">
       </collection>
   </resultMap>
 
     <select id="findUserByUserName" parameterType="java.lang.String" resultMap="userMap2">
         SELECT  * from USER WHERE username=#{username}
     </select>
 
   <select id="findRole" parameterType="java.lang.Integer" resultMap="roleMap">
       SELECT r.* from Role r LEFT  JOIN  user_role ur on ur.rid =r.rid where ur.uid=#{uid}
   </select>
 
   <select id="findModule" parameterType="java.lang.Integer" resultType="com.example.mapper.entity.Module">
       SELECT m.* from module m LEFT  JOIN  module_role mr on mr.mid =m.mid where mr.rid=#{rid}
   </select>

  测试结果。

User{uid=1, username='hlhdidi', password='123', roles=[Role{rid=1, rname='admin',modules=[Module{mid=2, mname='delete'}, Module{mid=3, mname='query'}, Module{mid=4, mname='update'}, Module{mid=1, mname='add'}]}]}

  
posted @   万事俱备就差个程序员  阅读(292)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
历史上的今天:
2020-09-23 java 双冒号::问我是什么语法

http://xiangai.taobao.com
http://shop148612228.taobao.com
如果您觉得对您有帮助.领个红包吧.谢谢.
支付宝红包
微信打赏 支付宝打赏
点击右上角即可分享
微信分享提示