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

所属分类: 软件编程 / java 阅读数: 64
收藏 0 赞 0 分享

一、数据库结构

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


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();
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<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("--------------------------------------");
    }
  }
}

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

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<Map> selectAllUserRoleAuthority();
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.lemon.demo.dao.IUserDao">
  <!--查询 用户信息,角色信息,权限信息-->
  <select id="selectAllUserRoleAuthority" resultType="java.util.Map">
    SELECT
    u.user_id userId,
    u.user_name userName,
    u.user_sex userSex,
    u.user_birthday userBirthday,
    u.user_address userAddress,
    r.role_name roleName,
    r.role_description roleDescription,
    a.authority_name authorityName,
    a.authority_description authorityDescription
    FROM
      (
        (
          ( USER u INNER JOIN user_role ur ON u.user_id = ur.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 a.authority_id = ra.authority_id
  </select>
</mapper>

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

package cn.lemon.demo.service;

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

public interface IUserService {
  List<Map> 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<Map> 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<Map> mapList = userService.selectAllUserRoleAuthority();
    JSONObject json = new JSONObject();
    json.put("mapList",mapList);
    System.out.println(json.toJSONString());
    return json.toJSONString();
  }
}

5、前端页面 index.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="UTF-8">
  <title>首页</title>
  <script type="text/javascript" th:src="@{/static/js/jquery-1.11.3.min.js}"></script>
</head>
<body>
<div id="head">
  <table width="100%" align="center" border="2px" cellspacing="2px">
    <thead>
    <tr>
      <th>用户编号</th>
      <th>用户姓名</th>
      <th>用户性别</th>
      <th>用户生日</th>
      <th>用户地址</th>
      <th>角色名称</th>
      <th>角色描述</th>
      <th>权限名称</th>
      <th>权限描述</th>
    </tr>
    </thead>
    <tbody id="tbody">

    </tbody>
  </table>
</div>
<script type="text/javascript">
  $(function () {
    $.ajax({
      type: "post",
      url: '/selectAll',
      contentType: "application/json;charset=utf-8",
      dataType: 'json',
      //async: false,/*表示请求为同步方式*/
      success: function (data) {
        //在<tbody>中追加数据
        for (var i = 0; i < data.mapList.length; i++) {
          $("#tbody").append("<tr><td>" + data.mapList[i].userId + "</td>" +
            "<td>" + data.mapList[i].userName + "</td>" +
            "<td>" + data.mapList[i].userSex + "</td>" +
            "<td>" + data.mapList[i].userBirthday + "</td>" +
            "<td>" + data.mapList[i].userAddress + "</td>" +
            "<td>" + data.mapList[i].roleName + "</td>" +
            "<td>" + data.mapList[i].roleDescription + "</td>" +
            "<td>" + data.mapList[i].authorityName + "</td>" +
            "<td>" + data.mapList[i].authorityDescription + "</td>" +
            "</tr>");
        }
      },
      error: function () {
        window.alert("查询失败");
      }
    });
  });
</script>
</body>
</html>

运行 localhost:8080 显示:

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

更多精彩内容其他人还在看

利用MultipartFile实现文件上传功能

这篇文章主要为大家详细介绍了利用MultipartFile实现文件上传功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Java编程实现NBA赛事接口调用实例代码

这篇文章主要介绍了Java编程实现NBA赛事接口调用实例代码,具有一定参考价值,需要的朋友可以了解下。
收藏 0 赞 0 分享

Java编程之双重循环打印图形

这篇文章主要介绍了Java编程之双重循环打印图形,属于Java编程基础练习部分,具有一定参考价值,需要的朋友可以了解下。
收藏 0 赞 0 分享

java基础学习JVM中GC的算法

这篇文章主要介绍了java基础学习JVM中GC的算法,通过图文加深对GC算法思路的理解。
收藏 0 赞 0 分享

Java编程Post数据请求和接收代码详解

这篇文章主要介绍了Java编程Post数据请求和接收代码详解,涉及enctype的三种编码,post与get等相关内容,具有一定参考价值,需要的朋友可以了解下。
收藏 0 赞 0 分享

Retrofit+Rxjava实现文件上传和下载功能

这篇文章主要介绍了Retrofit+Rxjava实现文件上传和下载功能,文中提到了单文件上传和多文件上传及相关参数的请求,需要的朋友参考下吧
收藏 0 赞 0 分享

Retrofit+Rxjava下载文件进度的实现

这篇文章主要介绍了Retrofit+Rxjava下载文件进度的实现,非常不错,具有参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

java检查服务器的连通两种方法代码分享

这篇文章主要介绍了java检查服务器的连通两种方法代码分享,涉及ping的介绍以及检查服务器连通的两种方法代码示例,具有一定参考价值,需要的朋友可以了解下。
收藏 0 赞 0 分享

Java/Android 获取网络重定向文件的真实URL的示例代码

本篇文章主要介绍了Java/Android 获取网络重定向文件的真实URL的示例代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

java并发编程之同步器代码示例

这篇文章主要介绍了java并发编程之同步器代码示例,分享了相关代码,具有一定参考价值,需要的朋友可以了解下。
收藏 0 赞 0 分享
查看更多