电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

MyBatis 三表外關聯查詢的實現(用戶、角色、權限)

瀏覽:121日期:2023-10-23 12:20:45

一、數據庫結構

MyBatis 三表外關聯查詢的實現(用戶、角色、權限)

二、查詢所有數據記錄(SQL語句)

MyBatis 三表外關聯查詢的實現(用戶、角色、權限)

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;@Datapublic 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;@Datapublic 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;@Datapublic 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;@Repositorypublic 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 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 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 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 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;@Servicepublic 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;@Servicepublic 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;@Datapublic 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;@Datapublic 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;@Datapublic 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 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;@Servicepublic 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='http://www.hdgsjgj.cn/bcjs/@{/static/js/jquery-1.11.3.min.js}'></script></head><body><div id='head'> <table 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 三表外關聯查詢的實現(用戶、角色、權限)的文章就介紹到這了,更多相關MyBatis 外關聯查詢內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Mybatis 數據庫
相關文章:
主站蜘蛛池模板: 电线电缆厂家|沈阳电缆厂|电线厂|沈阳英联塑力线缆有限公司 | 明渠式紫外线杀菌器-紫外线消毒器厂家-定州市优威环保 | 激光内雕_led玻璃_发光玻璃_内雕玻璃_导光玻璃-石家庄明晨三维科技有限公司 激光内雕-内雕玻璃-发光玻璃 | 气体检测仪-氢气检测仪-可燃气体传感器-恶臭电子鼻-深国安电子 | 骨密度仪-骨密度测定仪-超声骨密度仪-骨龄测定仪-天津开发区圣鸿医疗器械有限公司 | 老城街小面官网_正宗重庆小面加盟技术培训_特色面馆加盟|牛肉拉面|招商加盟代理费用多少钱 | 利浦顿蒸汽发生器厂家-电蒸汽发生器/燃气蒸汽发生器_湖北利浦顿热能科技有限公司官网 | 福州时代广告制作装饰有限公司-福州广告公司广告牌制作,福州展厅文化墙广告设计, | 精密五金冲压件_深圳五金冲压厂_钣金加工厂_五金模具加工-诚瑞丰科技股份有限公司 | 应急灯_消防应急灯_应急照明灯_应急灯厂家-大成智慧官网 | SDI车窗夹力测试仪-KEMKRAFT方向盘测试仪-上海爱泽工业设备有限公司 | 聚氨酯复合板保温板厂家_廊坊华宇创新科技有限公司 | 青岛侦探_青岛侦探事务所_青岛劝退小三_青岛调查出轨取证公司_青岛婚外情取证-青岛探真调查事务所 | led冷热冲击试验箱_LED高低温冲击试验箱_老化试验箱-爱佩百科 | 福州时代广告制作装饰有限公司-福州广告公司广告牌制作,福州展厅文化墙广告设计, | 山东锐智科电检测仪器有限公司_超声波测厚仪,涂层测厚仪,里氏硬度计,电火花检漏仪,地下管线探测仪 | 沥青灌缝机_路面灌缝机_道路灌缝机_沥青灌缝机厂家_济宁萨奥机械有限公司 | 真空泵厂家_真空泵机组_水环泵_旋片泵_罗茨泵_耐腐蚀防爆_中德制泵 | 3A别墅漆/3A环保漆_广东美涂士建材股份有限公司【官网】 | 蔡司三坐标-影像测量机-3D扫描仪-蔡司显微镜-扫描电镜-工业CT-ZEISS授权代理商三本工业测量 | 北京翻译公司-专业合同翻译-医学标书翻译收费标准-慕迪灵 | Boden齿轮油泵-ketai齿轮泵-yuken油研-无锡新立液压有限公司 | 东莞爱加真空科技有限公司-进口真空镀膜机|真空镀膜设备|Polycold维修厂家 | 雪花制冰机(实验室雪花制冰机)百科 | 北京网站建设-企业网站建设-建站公司-做网站-北京良言多米网络公司 | 药品/药物稳定性试验考察箱-埃里森仪器设备(上海)有限公司 | 一体化隔油提升设备-餐饮油水分离器-餐厨垃圾处理设备-隔油池-盐城金球环保产业发展有限公司 | 高柔性拖链电缆-聚氨酯卷筒电缆-柔性屏蔽电缆厂家-玖泰电缆 | 石磨面粉机|石磨面粉机械|石磨面粉机组|石磨面粉成套设备-河南成立粮油机械有限公司 | 上海地磅秤|电子地上衡|防爆地磅_上海地磅秤厂家–越衡称重 | 高压无油空压机_无油水润滑空压机_水润滑无油螺杆空压机_无油空压机厂家-科普柯超滤(广东)节能科技有限公司 | 昆明挖掘机修理厂_挖掘机翻新再制造-昆明聚力工程机械维修有限公司 | 手机存放柜,超市储物柜,电子储物柜,自动寄存柜,行李寄存柜,自动存包柜,条码存包柜-上海天琪实业有限公司 | 南京蜂窝纸箱_南京木托盘_南京纸托盘-南京博恒包装有限公司 | 长城人品牌官网| 无锡网站建设-做网站-建网站-网页设计制作-阿凡达建站公司 | 意大利Frascold/富士豪压缩机_富士豪半封闭压缩机_富士豪活塞压缩机_富士豪螺杆压缩机 | 玉米深加工机械,玉米加工设备,玉米加工机械等玉米深加工设备制造商-河南成立粮油机械有限公司 | 气动绞车,山东气动绞车,气动绞车厂家-烟台博海石油机械有限公司 气动隔膜泵厂家-温州永嘉定远泵阀有限公司 | 贵州科比特-防雷公司厂家提供贵州防雷工程,防雷检测,防雷接地,防雷设备价格,防雷产品报价服务-贵州防雷检测公司 | 上海办公室装修公司_办公室设计_直营办公装修-羚志悦装 |