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

相关文章

详解SpringBoot+Ehcache使用示例

《详解SpringBoot+Ehcache使用示例》本文介绍了SpringBoot中配置Ehcache、自定义get/set方式,并实际使用缓存的过程,文中通过示例代码介绍的非常详细,对大家的学习或者... 目录摘要概念内存与磁盘持久化存储:配置灵活性:编码示例引入依赖:配置ehcache.XML文件:配置

Java 虚拟线程的创建与使用深度解析

《Java虚拟线程的创建与使用深度解析》虚拟线程是Java19中以预览特性形式引入,Java21起正式发布的轻量级线程,本文给大家介绍Java虚拟线程的创建与使用,感兴趣的朋友一起看看吧... 目录一、虚拟线程简介1.1 什么是虚拟线程?1.2 为什么需要虚拟线程?二、虚拟线程与平台线程对比代码对比示例:三

k8s按需创建PV和使用PVC详解

《k8s按需创建PV和使用PVC详解》Kubernetes中,PV和PVC用于管理持久存储,StorageClass实现动态PV分配,PVC声明存储需求并绑定PV,通过kubectl验证状态,注意回收... 目录1.按需创建 PV(使用 StorageClass)创建 StorageClass2.创建 PV

Redis 基本数据类型和使用详解

《Redis基本数据类型和使用详解》String是Redis最基本的数据类型,一个键对应一个值,它的功能十分强大,可以存储字符串、整数、浮点数等多种数据格式,本文给大家介绍Redis基本数据类型和... 目录一、Redis 入门介绍二、Redis 的五大基本数据类型2.1 String 类型2.2 Hash

MyBatis延迟加载与多级缓存全解析

《MyBatis延迟加载与多级缓存全解析》文章介绍MyBatis的延迟加载与多级缓存机制,延迟加载按需加载关联数据提升性能,一级缓存会话级默认开启,二级缓存工厂级支持跨会话共享,增删改操作会清空对应缓... 目录MyBATis延迟加载策略一对多示例一对多示例MyBatis框架的缓存一级缓存二级缓存MyBat

Redis中Hash从使用过程到原理说明

《Redis中Hash从使用过程到原理说明》RedisHash结构用于存储字段-值对,适合对象数据,支持HSET、HGET等命令,采用ziplist或hashtable编码,通过渐进式rehash优化... 目录一、开篇:Hash就像超市的货架二、Hash的基本使用1. 常用命令示例2. Java操作示例三

Linux创建服务使用systemctl管理详解

《Linux创建服务使用systemctl管理详解》文章指导在Linux中创建systemd服务,设置文件权限为所有者读写、其他只读,重新加载配置,启动服务并检查状态,确保服务正常运行,关键步骤包括权... 目录创建服务 /usr/lib/systemd/system/设置服务文件权限:所有者读写js,其他

Redis中Set结构使用过程与原理说明

《Redis中Set结构使用过程与原理说明》本文解析了RedisSet数据结构,涵盖其基本操作(如添加、查找)、集合运算(交并差)、底层实现(intset与hashtable自动切换机制)、典型应用场... 目录开篇:从购物车到Redis Set一、Redis Set的基本操作1.1 编程常用命令1.2 集

Redis中的有序集合zset从使用到原理分析

《Redis中的有序集合zset从使用到原理分析》Redis有序集合(zset)是字符串与分值的有序映射,通过跳跃表和哈希表结合实现高效有序性管理,适用于排行榜、延迟队列等场景,其时间复杂度低,内存占... 目录开篇:排行榜背后的秘密一、zset的基本使用1.1 常用命令1.2 Java客户端示例二、zse

mysql8.0.43使用InnoDB Cluster配置主从复制

《mysql8.0.43使用InnoDBCluster配置主从复制》本文主要介绍了mysql8.0.43使用InnoDBCluster配置主从复制,文中通过示例代码介绍的非常详细,对大家的学习或者... 目录1、配置Hosts解析(所有服务器都要执行)2、安装mysql shell(所有服务器都要执行)3、