栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

一、数据库结构

二、查询所有数据记录(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 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 {
  
  List selectAllUserRoleAuthority();
}



  
    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
  

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;
  
  @RequestMapping(value = "index")
  public String index() {
    return "index";
  }

  
  @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 三表外关联查询的实现(用户、角色、权限)的文章就介绍到这了,更多相关MyBatis 外关联查询内容请搜索考高分网以前的文章或继续浏览下面的相关文章希望大家以后多多支持考高分网!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/132745.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号