mybatis-plus註解版實現多表聯查(sql)

mybatis註解版實現多表聯查

需求:

用戶有角色,角色有權限,須要一次取用戶信息包含角色信息及其對應權限java

實體類:

package cn.zytao.taosir.common.model.user;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import com.baomidou.mybatisplus.annotations.TableField;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

@Data
@ApiModel(value="用戶實體")
public class User implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = -2389902440625641568L;

    @ApiModelProperty(name ="id",value = "ID主鍵")
    private String id;

    @ApiModelProperty(name ="username",value = "用戶名")
    private String username;

    @ApiModelProperty(name ="password",value = "密碼")
    private String password;

    @ApiModelProperty(name ="nickname",value = "暱稱")
    private String nickname;
    
    @ApiModelProperty(name ="isInsiders",value = "是否內部人員")
    private int isInsiders;

    @ApiModelProperty(name ="phone",value = "聯繫電話")
    private String phone;

    @ApiModelProperty(name ="email",value = "郵箱")
    private String email;

    @ApiModelProperty(name ="qq",value = "QQ")
    private String qq;

    @ApiModelProperty(name ="wechat",value = "微信")
    private String wechat;

    @ApiModelProperty(name ="question",value = "找回密碼問題")
    private String question;

    @ApiModelProperty(name ="answer",value = "找回密碼答案")
    private String answer;

    @ApiModelProperty(name ="createTime",value = "建立時間")
    private Date createTime;

    @ApiModelProperty(name ="updateTime",value = "更新時間")
    private Date updateTime;
    
    @TableField(exist = false)
    private Set<Role> roles=new HashSet<>();
}
用戶
package cn.zytao.taosir.common.model.user;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import com.baomidou.mybatisplus.annotations.TableField;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * 
 * @author taosir
 *
 */
@Data
@ApiModel(value="角色實體")
public class Role implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = 4710406435745633366L;

    @ApiModelProperty(name ="id",value = "ID主鍵")
    private String id;
    
    @ApiModelProperty(name ="roleCode",value = "角色代碼")
    private String roleCode;
    
    @ApiModelProperty(name ="roleName",value = "角色名稱")
    private String roleName;
    
    @TableField(exist = false)
    private Set<Permission> permissions=new HashSet<>();
}
角色
package cn.zytao.taosir.common.model.user;


import java.io.Serializable;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

@Data
@ApiModel(value="權限實體")
public class Permission implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = 1545962188193104351L;

    @ApiModelProperty(name ="id",value = "ID主鍵")
    private String id;
    
    @ApiModelProperty(name ="permissionCode",value = "權限代碼")
    private String permissionCode;
    
    @ApiModelProperty(name ="permissionName",value = "權限名稱")
    private String permissionName;
    
    @ApiModelProperty(name="path",value="映射路徑")
    private String path;
}
權限
package cn.zytao.taosir.common.model.user;

import java.io.Serializable;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

@Data
@ApiModel(value="用戶角色關係表")
public class UserRole implements Serializable{
    /**
     * 
     */
    private static final long serialVersionUID = 6163369825048118489L;
    @ApiModelProperty(name ="id",value = "ID主鍵")
    private String id;
    @ApiModelProperty(name ="username",value = "用戶名")
    private String username;
    @ApiModelProperty(name ="roleCode",value = "角色代碼")
    private String roleCode;
}
用戶角色關係
package cn.zytao.taosir.common.model.user;

import java.io.Serializable;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

@Data
@ApiModel(value="角色權限關係表")
public class RolePermission implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = -7522662317811377590L;
    @ApiModelProperty(name ="id",value = "ID主鍵")
    private String id;
    @ApiModelProperty(name ="roleCode",value = "角色代碼")
    private String roleCode;
    @ApiModelProperty(name ="permissionCode",value = "權限代碼")
    private String permissionCode;
}
角色權限關係

註解是swagger2的,無需關注。另外有個 @TableField(exist = false) 該註解標識這個字段非數據庫字段sql

持久層處理:

@Select("select * from  role where role_code in(select role_code from user_role where username = #{username})")
    Set<Role> getUserRoles(String username);
@Select("select * from permission where permission_code in (select permission_code from role_permission where role_code = #{roleCode})")
    Set<Permission> getRolePermissions(String roleCode);

第一個sql爲獲取用戶所擁有角色,第二個sql爲獲取角色所擁有權限數據庫

yml的配置上:

mybatis-plus:
  type-aliases-package: cn.zytao.taosir.common.model.user
  configuration:
    map-underscore-to-camel-case: true

映射實體類,開起對下劃線轉大寫的處理微信

業務處理上:

    public User findByUsername(String username) {
        User user = userMapper.findByUsername(username);
        Set<Role> roles = userMapper.getUserRoles(username);
        for (Role role : roles) {
            role.setPermissions(roleMapper.getRolePermissions(role.getRoleCode()));
        }
        user.setRoles(roles);
        return user;
    }

這樣就能夠很方便的解決多表的聯查~由於本人比較懶,不想寫配置版,也不喜歡在註解上用@Results來標示結果集mybatis

仍在不斷學習中,若有不妥還望指教~app

相關文章
相關標籤/搜索