mybatis多层嵌套查询

本文详细探讨了在基于权限管理的系统中,如何处理User类的用户与其关联的Role和Module的关系,涉及用户映射、角色权限查找及Mapper文件的SQL设计。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

实体类:

 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
	  		private Integermid;
		    private String mname;
	mapper文件
	<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>
 

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值