mybatis多对多查询(三表)

mapper.xml有点坑

  • 1.1entity
    • 1.1.1 User
    • 1.1.2 Role
    • 1.1.3 Perms
  • 2.1 userMapper.xml
  • 3.1 结果
    • 3.1.1 查询功能
    • 3.1.2 测试结果

1.1entity

1.1.1 User

package com.tc.shirospringboot.entity;

import lombok.Data;

import java.io.Serializable;
import java.util.List;

/**
 * (User)实体类
 *
 * @author silence
 * @since 2020-07-22 22:08:11
 */
@Data
public class User implements Serializable {
     

    
    private Integer id;
    
    private String username;
    
    private String password;
    private List<Role> roleList;
    private List<Perms> permsList;



}

1.1.2 Role

package com.tc.shirospringboot.entity;

import lombok.Data;

import java.io.Serializable;
import java.util.List;

/**
 * (Role)实体类
 *
 * @author silence
 * @since 2020-07-23 12:17:31
 */
@Data
public class Role implements Serializable {
     
    private static final long serialVersionUID = -17264268191890864L;
    
    private Integer id;
    
    private String name;


}

1.1.3 Perms


```javascript
package com.tc.shirospringboot.entity;

import lombok.Data;

import java.io.Serializable;
import java.util.List;

/**
 * (Perms)实体类
 *
 * @author silence
 * @since 2020-07-23 12:17:56
 */
@Data

public class Perms implements Serializable {
     
    private static final long serialVersionUID = -79809755308120856L;
    
    private Integer id;
    
    private String name;
    
    private String url;





}

2.1 userMapper.xml



```javascript





    
        
        
        
        
              //因为column不能重名所以必须取别名,对应查询语句的r.id as roleId
            //对应查询语句的r.name as roleName
        
        
            //对应查询语句的p.id as permsId
            //对应查询语句的p.name as permsName
            
            //对应查询语句的url as url
        

    

    



   


3.1 结果

3.1.1 查询功能

根据User的name查询到他拥有的角色和权限

3.1.2 测试结果

mybatis多对多查询(三表)_第1张图片

User(id=1, username=tom, password=123456, roleList=[Role(id=1, name=user), Role(id=2, name=root)], permsList=[Perms(id=null, name=user:add, url=/user/add), Perms(id=null, name=user:add, url=/user/add), Perms(id=null, name=user:update, url=/user/update)])

你可能感兴趣的:(java,mysql,mybatis)