MyBatis 三表外关联查询(用户、角色、权限)

一、数据库结构

MyBatis 三表外关联查询(用户、角色、权限)_第1张图片

二、查询所有数据记录

MyBatis 三表外关联查询(用户、角色、权限)_第2张图片
SQL语句:

SELECT u.*, r.*, a.* FROM
(
    (
        ( user u INNER JOIN user_role ur ON ur.user_id = u.user_id )
        INNER JOIN role r ON r.role_id = ur.role_id
    )
    INNER JOIN role_authority ra ON ra.role_id = r.role_id
)
INNER JOIN authority a ON ra.authority_id = a.authority_id

三、详细代码

1、实体类entity

package cn.lemon.demo.entity;

import lombok.Data;

import java.io.Serializable;

@Data
public class AuthorityEntity implements Serializable {
    private Integer authorityId;
    private String authorityName;
    private String authorityDescription;
}
package cn.lemon.demo.entity;

import lombok.Data;

import java.io.Serializable;

@Data
public class RoleEntity implements Serializable {
    private Integer roleId;
    private String roleName;
    private String roleDescription;
}
package cn.lemon.demo.entity;

import lombok.Data;

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

@Data
public class UserEntity implements Serializable {
    private Integer userId;
    private String userName;
    private String userSex;
    private Date userBirthday;
    private String userAddress;

    private List<RoleEntity> roleEntityList;
    private List<AuthorityEntity> authorityEntityList;
}

2、数据访问层dao、Mapper

package cn.lemon.demo.dao;

import cn.lemon.demo.entity.UserEntity;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface IUserDao {
    /**
     * 查询所有关联的数据
     *
     * @return
     */
    List<UserEntity> selectAllUserRoleAuthority();
}


<mapper namespace="cn.lemon.demo.dao.IUserDao">
    <select id="selectAllUserRoleAuthority" resultMap="userMap">
        SELECT u.*, r.*, a.* FROM
        (
            (
                ( user u INNER JOIN user_role ur ON ur.user_id = u.user_id )
                INNER JOIN role r ON r.role_id = ur.role_id
            )
            INNER JOIN role_authority ra ON ra.role_id = r.role_id
        )
        INNER JOIN authority a ON ra.authority_id = a.authority_id
    select>
    
    <resultMap id="userMap" type="cn.lemon.demo.entity.UserEntity">
        <id property="userId" column="user_id"/>
        <result property="userName" column="user_name"/>
        <result property="userSex" column="user_sex"/>
        <result property="userBirthday" column="user_birthday"/>
        <result property="userAddress" column="user_address"/>
        <collection property="roleEntityList" ofType="cn.lemon.demo.entity.RoleEntity" resultMap="roleMap"/>
        <collection property="authorityEntityList" ofType="cn.lemon.demo.entity.AuthorityEntity" resultMap="authorityMap"/>
    resultMap>
    <resultMap id="roleMap" type="cn.lemon.demo.entity.RoleEntity">
        <id property="roleId" column="role_id"/>
        <result property="roleName" column="role_name"/>
        <result property="roleDescription" column="role_description"/>
    resultMap>
    <resultMap id="authorityMap" type="cn.lemon.demo.entity.AuthorityEntity">
        <id property="authorityId" column="authority_id"/>
        <result property="authorityName" column="authority_name"/>
        <result property="authorityDescription" column="authority_description"/>
    resultMap>
mapper>

3、业务层service

package cn.lemon.demo.service;

import cn.lemon.demo.entity.UserEntity;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public interface IUserService {
    List<UserEntity> selectAllUserRoleAuthority();
}
package cn.lemon.demo.service.impl;

import cn.lemon.demo.dao.IUserDao;
import cn.lemon.demo.entity.UserEntity;
import cn.lemon.demo.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private IUserDao userDao;

    @Override
    public List<UserEntity> selectAllUserRoleAuthority() {
        return userDao.selectAllUserRoleAuthority();
    }
}

4、测试类

package cn.lemon.demo.service.impl;

import cn.lemon.demo.entity.UserEntity;
import cn.lemon.demo.service.IUserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

@SpringBootTest
@RunWith(SpringRunner.class)
public class UserServiceImplTest {
    @Autowired
    private IUserService userService;

    @Test
    public void selectAllUserRoleAuthority() {
        List<UserEntity> userEntities = userService.selectAllUserRoleAuthority();
        for (UserEntity userEntity : userEntities) {
            System.out.println(
                    "用户姓名:" + userEntity.getUserName() +
                    "用户地址:" + userEntity.getUserAddress() +
                    "权限列表:" + userEntity.getAuthorityEntityList() +
                    "角色列表:" + userEntity.getRoleEntityList());
            System.out.println("--------------------------------------");
        }
    }
}

代码下载

你可能感兴趣的:(Spring,Boot,MyBatis,mysql,java)