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

一、数据库结构

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

二、查询所有数据记录(SQL语句)

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 roleEntityList;
  private List 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 selectAllUserRoleAuthority();
}



  
  
  
    
    
    
    
    
    
    
  
  
    
    
    
  
  
    
    
    
  

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 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 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 userEntities = userService.selectAllUserRoleAuthority();
    for (UserEntity userEntity : userEntities) {
      System.out.println(
          "用户姓名:" + userEntity.getUserName() +
          "用户地址:" + userEntity.getUserAddress() +
          "权限列表:" + userEntity.getAuthorityEntityList() +
          "角色列表:" + userEntity.getRoleEntityList());
      System.out.println("--------------------------------------");
    }
  }
}

四、详细代码(第二中方式)

1、实体类entity (实体类可以省略不写)

package cn.lemon.demo.entity;

import lombok.Data;

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

@Data
public class UserEntity implements Serializable {
  private Long userId;
  private String userName;
  private String userSex;
  private Date userBirthday;
  private String userAddress;
}
package cn.lemon.demo.entity;

import lombok.Data;

import java.io.Serializable;

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

import lombok.Data;

import java.io.Serializable;

@Data
public class AuthorityEntity implements Serializable {
  private Long authorityId;
  private String authorityName;
  private String authorityDescription;
}

2、数据访问层dao、Mapper

package cn.lemon.demo.dao;

import java.util.List;
import java.util.Map;

public interface IUserDao {
  List selectAllUserRoleAuthority();
}



  
  

3、业务层service (接口及实现类)

package cn.lemon.demo.service;

import java.util.List;
import java.util.Map;

public interface IUserService {
  List selectAllUserRoleAuthority();
}
package cn.lemon.demo.service.impl;

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

import java.util.List;
import java.util.Map;

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

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

4、控制层controller

package cn.lemon.demo.controller;

import cn.lemon.demo.service.IUserService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/")
public class SystemController {
  @Autowired
  private IUserService userService;
  /**
   * 跳转页面
   *
   * @return
   */
  @RequestMapping(value = "index")
  public String index() {
    return "index";
  }

  /**
   * 查询所有关联的数据 用户信息,角色信息,权限信息
   * @return
   */
  @RequestMapping(value = "selectAll",method = RequestMethod.POST)
  @ResponseBody
  public String selectAll(){
    List mapList = userService.selectAllUserRoleAuthority();
    JSONObject json = new JSONObject();
    json.put("mapList",mapList);
    System.out.println(json.toJSONString());
    return json.toJSONString();
  }
}

5、前端页面 index.html




  
  首页
  





运行 localhost:8080 显示:

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

到此这篇关于MyBatis 三表外关联查询的实现(用户、角色、权限)的文章就介绍到这了,更多相关MyBatis 外关联查询内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

你可能感兴趣的:(MyBatis 三表外关联查询的实现(用户、角色、权限))