mybatis中的collection标签使用说明

2024-06-10 19:32

本文主要是介绍mybatis中的collection标签使用说明,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >  
  3. <mapper namespace="SHIRO_SpecSql" >  
  4.       
  5.     <!-- 获得这个用户所有的菜单权限 -->  
  6.     <select id="searchSingleUserMenuAuthorities" parameterType="java.lang.String" resultMap="OneMenuAuthority">  
  7.         select   
  8.         name name,  
  9.         ht_authority_id htAuthorityId,  
  10.         (select ${uid} from dual ) currentUserId  
  11.         from ht_authority   
  12.         where pid = 0  
  13.     </select>  
  14.     <resultMap type="com.sailod.shiro.dto.HtAuthorityMenuDTO" id="OneMenuAuthority">  
  15.         <id property="htAuthorityId" column="htAuthorityId" javaType="java.lang.Long" />  
  16.         <result property="name" column="name" javaType="java.lang.String" />  
  17.         <result property="currentUserId" column="currentUserId" javaType="java.lang.Long" />  
  18.         <collection property="htAuthorityDTO"  ofType="com.sailod.shiro.dto.HtAuthorityDTO"  
  19.          select="selectAuthority" column="{htAuthorityId2 = htAuthorityId ,currentUserId2 = currentUserId}"   >  
  20.          </collection>  
  21.     </resultMap>  
  22.     <select id="selectAuthority" parameterType="java.util.HashMap" resultType="com.sailod.shiro.dto.HtAuthorityDTO" resultMap="OneAuthority"  >  
  23.         select ha.name name,  
  24.         ha.url url ,  
  25.         ha.ht_authority_id htauthorityid,  
  26.         ha.pid pid,  
  27.         ha.type type,  
  28.         ha.permission permission,  
  29.         hua.ht_user_id currUserId  
  30.         from ht_authority ha  
  31.         left join ht_user_authority hua on hua.ht_authority_id = ha.ht_authority_id   
  32.         where ha.pid = ${htAuthorityId2}  
  33.         and ha.type = 'menu'   
  34.         and hua.ht_user_id = ${currentUserId2}   
  35.     </select>  
  36.     <resultMap type="com.sailod.shiro.dto.HtAuthorityDTO" id="OneAuthority" >  
  37.         <id property="pid" column="pid" javaType="java.lang.Long" />  
  38.         <result property="name" column="name"  javaType="java.lang.String"/>  
  39.         <result property="url" column="url" javaType="java.lang.String"/>  
  40.         <result property="type" column="type" javaType="java.lang.String"/>  
  41.         <result property="permission" column="permission" javaType="java.lang.String"/>  
  42.         <result property="htAuthorityId" column="htauthorityid" javaType="java.lang.Long"/>  
  43.         <result property="currUserId" column="currUserId" javaType="java.lang.Long" />  
  44.     </resultMap>  
  45. </mapper>  


<resultMap>其实就是返回类型为其引用的id的   标签所定义的。

<collection> 往这个标签定义的 ‘类’ 的 list 属性中设置值, 如何设置值? 还要根据其 select="selectAuthority"  , 把值查询出来。

 

 

 

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.sailod.shiro.dto;  
  2.   
  3. import java.util.List;  
  4.   
  5. /** 
  6.  * 查询菜单用到dto 
  7.  *  
  8.  * 
  9.  */  
  10. public class HtAuthorityMenuDTO {  
  11.       
  12.   
  13.     private String name;  
  14.       
  15.     private Long htAuthorityId;  
  16.       
  17.     private Long currentUserId;  
  18.       
  19.     private List<HtAuthorityDTO> htAuthorityDTO;  
  20.   
  21.     public Long getHtAuthorityId() {  
  22.         return htAuthorityId;  
  23.     }  
  24.   
  25.     public void setHtAuthorityId(Long htAuthorityId) {  
  26.         this.htAuthorityId = htAuthorityId;  
  27.     }  
  28.   
  29.       
  30.   
  31.     public String getName() {  
  32.         return name;  
  33.     }  
  34.   
  35.     public void setName(String name) {  
  36.         this.name = name;  
  37.     }  
  38.   
  39.     public List<HtAuthorityDTO> getHtAuthorityDTO() {  
  40.         return htAuthorityDTO;  
  41.     }  
  42.   
  43.     public void setHtAuthorityDTO(List<HtAuthorityDTO> htAuthorityDTO) {  
  44.         this.htAuthorityDTO = htAuthorityDTO;  
  45.     }  
  46.   
  47.     public Long getCurrentUserId() {  
  48.         return currentUserId;  
  49.     }  
  50.   
  51.     public void setCurrentUserId(Long currentUserId) {  
  52.         this.currentUserId = currentUserId;  
  53.     }  
  54.       
  55.       
  56. }  


 

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.sailod.shiro.dto;  
  2.   
  3. /** 
  4.  * 查询菜单用到dto 
  5.  *  
  6.  * 
  7.  */  
  8. public class HtAuthorityDTO {  
  9.       
  10.     //这个权限的主键  
  11.     private Long htAuthorityId;  
  12.     //父菜单的主键  
  13.     private Long pid;  
  14.       
  15.     private String name;  
  16.       
  17.     private String url;  
  18.     //类型  
  19.     private String type;  
  20.       
  21.     private String permission;  
  22.       
  23.     private Long currUserId;  
  24.       
  25.       
  26.     public String getName() {  
  27.         return name;  
  28.     }  
  29.   
  30.     public void setName(String name) {  
  31.         this.name = name;  
  32.     }  
  33.   
  34.     public String getUrl() {  
  35.         return url;  
  36.     }  
  37.   
  38.     public void setUrl(String url) {  
  39.         this.url = url;  
  40.     }  
  41.   
  42.     public Long getHtAuthorityId() {  
  43.         return htAuthorityId;  
  44.     }  
  45.   
  46.     public void setHtAuthorityId(Long htAuthorityId) {  
  47.         this.htAuthorityId = htAuthorityId;  
  48.     }  
  49.   
  50.     public Long getPid() {  
  51.         return pid;  
  52.     }  
  53.   
  54.     public void setPid(Long pid) {  
  55.         this.pid = pid;  
  56.     }  
  57.   
  58.     public String getType() {  
  59.         return type;  
  60.     }  
  61.   
  62.     public void setType(String type) {  
  63.         this.type = type;  
  64.     }  
  65.   
  66.     public String getPermission() {  
  67.         return permission;  
  68.     }  
  69.   
  70.     public void setPermission(String permission) {  
  71.         this.permission = permission;  
  72.     }  
  73.   
  74.     public Long getCurrUserId() {  
  75.         return currUserId;  
  76.     }  
  77.   
  78.     public void setCurrUserId(Long currUserId) {  
  79.         this.currUserId = currUserId;  
  80.     }  
  81.   
  82.       
  83.       
  84.       
  85. }  


 

 

 

 低效率 collection:

 

 

 

 

高效率collection,  

1 查询用联合查询

2<collection 里面不写column  

这篇关于mybatis中的collection标签使用说明的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/1049057

相关文章

Qt spdlog日志模块的使用详解

《Qtspdlog日志模块的使用详解》在Qt应用程序开发中,良好的日志系统至关重要,本文将介绍如何使用spdlog1.5.0创建满足以下要求的日志系统,感兴趣的朋友一起看看吧... 目录版本摘要例子logmanager.cpp文件main.cpp文件版本spdlog版本:1.5.0采用1.5.0版本主要

Java中使用Hutool进行AES加密解密的方法举例

《Java中使用Hutool进行AES加密解密的方法举例》AES是一种对称加密,所谓对称加密就是加密与解密使用的秘钥是一个,下面:本文主要介绍Java中使用Hutool进行AES加密解密的相关资料... 目录前言一、Hutool简介与引入1.1 Hutool简介1.2 引入Hutool二、AES加密解密基础

使用Python将JSON,XML和YAML数据写入Excel文件

《使用Python将JSON,XML和YAML数据写入Excel文件》JSON、XML和YAML作为主流结构化数据格式,因其层次化表达能力和跨平台兼容性,已成为系统间数据交换的通用载体,本文将介绍如何... 目录如何使用python写入数据到Excel工作表用Python导入jsON数据到Excel工作表用

鸿蒙中@State的原理使用详解(HarmonyOS 5)

《鸿蒙中@State的原理使用详解(HarmonyOS5)》@State是HarmonyOSArkTS框架中用于管理组件状态的核心装饰器,其核心作用是实现数据驱动UI的响应式编程模式,本文给大家介绍... 目录一、@State在鸿蒙中是做什么的?二、@Spythontate的基本原理1. 依赖关系的收集2.

Python基础语法中defaultdict的使用小结

《Python基础语法中defaultdict的使用小结》Python的defaultdict是collections模块中提供的一种特殊的字典类型,它与普通的字典(dict)有着相似的功能,本文主要... 目录示例1示例2python的defaultdict是collections模块中提供的一种特殊的字

C++ Sort函数使用场景分析

《C++Sort函数使用场景分析》sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变,如果某些场景需要保持相同元素间的相对顺序,可使... 目录C++ Sort函数详解一、sort函数调用的两种方式二、sort函数使用场景三、sort函数排序

Spring Boot项目中结合MyBatis实现MySQL的自动主从切换功能

《SpringBoot项目中结合MyBatis实现MySQL的自动主从切换功能》:本文主要介绍SpringBoot项目中结合MyBatis实现MySQL的自动主从切换功能,本文分步骤给大家介绍的... 目录原理解析1. mysql主从复制(Master-Slave Replication)2. 读写分离3.

Java String字符串的常用使用方法

《JavaString字符串的常用使用方法》String是JDK提供的一个类,是引用类型,并不是基本的数据类型,String用于字符串操作,在之前学习c语言的时候,对于一些字符串,会初始化字符数组表... 目录一、什么是String二、如何定义一个String1. 用双引号定义2. 通过构造函数定义三、St

Pydantic中Optional 和Union类型的使用

《Pydantic中Optional和Union类型的使用》本文主要介绍了Pydantic中Optional和Union类型的使用,这两者在处理可选字段和多类型字段时尤为重要,文中通过示例代码介绍的... 目录简介Optional 类型Union 类型Optional 和 Union 的组合总结简介Pyd

Vue3使用router,params传参为空问题

《Vue3使用router,params传参为空问题》:本文主要介绍Vue3使用router,params传参为空问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录vue3使用China编程router,params传参为空1.使用query方式传参2.使用 Histo