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

相关文章

JavaScript中的reduce方法执行过程、使用场景及进阶用法

《JavaScript中的reduce方法执行过程、使用场景及进阶用法》:本文主要介绍JavaScript中的reduce方法执行过程、使用场景及进阶用法的相关资料,reduce是JavaScri... 目录1. 什么是reduce2. reduce语法2.1 语法2.2 参数说明3. reduce执行过程

如何使用Java实现请求deepseek

《如何使用Java实现请求deepseek》这篇文章主要为大家详细介绍了如何使用Java实现请求deepseek功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1.deepseek的api创建2.Java实现请求deepseek2.1 pom文件2.2 json转化文件2.2

mybatis和mybatis-plus设置值为null不起作用问题及解决

《mybatis和mybatis-plus设置值为null不起作用问题及解决》Mybatis-Plus的FieldStrategy主要用于控制新增、更新和查询时对空值的处理策略,通过配置不同的策略类型... 目录MyBATis-plusFieldStrategy作用FieldStrategy类型每种策略的作

python使用fastapi实现多语言国际化的操作指南

《python使用fastapi实现多语言国际化的操作指南》本文介绍了使用Python和FastAPI实现多语言国际化的操作指南,包括多语言架构技术栈、翻译管理、前端本地化、语言切换机制以及常见陷阱和... 目录多语言国际化实现指南项目多语言架构技术栈目录结构翻译工作流1. 翻译数据存储2. 翻译生成脚本

C++ Primer 多维数组的使用

《C++Primer多维数组的使用》本文主要介绍了多维数组在C++语言中的定义、初始化、下标引用以及使用范围for语句处理多维数组的方法,具有一定的参考价值,感兴趣的可以了解一下... 目录多维数组多维数组的初始化多维数组的下标引用使用范围for语句处理多维数组指针和多维数组多维数组严格来说,C++语言没

在 Spring Boot 中使用 @Autowired和 @Bean注解的示例详解

《在SpringBoot中使用@Autowired和@Bean注解的示例详解》本文通过一个示例演示了如何在SpringBoot中使用@Autowired和@Bean注解进行依赖注入和Bean... 目录在 Spring Boot 中使用 @Autowired 和 @Bean 注解示例背景1. 定义 Stud

使用 sql-research-assistant进行 SQL 数据库研究的实战指南(代码实现演示)

《使用sql-research-assistant进行SQL数据库研究的实战指南(代码实现演示)》本文介绍了sql-research-assistant工具,该工具基于LangChain框架,集... 目录技术背景介绍核心原理解析代码实现演示安装和配置项目集成LangSmith 配置(可选)启动服务应用场景

使用Python快速实现链接转word文档

《使用Python快速实现链接转word文档》这篇文章主要为大家详细介绍了如何使用Python快速实现链接转word文档功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 演示代码展示from newspaper import Articlefrom docx import

oracle DBMS_SQL.PARSE的使用方法和示例

《oracleDBMS_SQL.PARSE的使用方法和示例》DBMS_SQL是Oracle数据库中的一个强大包,用于动态构建和执行SQL语句,DBMS_SQL.PARSE过程解析SQL语句或PL/S... 目录语法示例注意事项DBMS_SQL 是 oracle 数据库中的一个强大包,它允许动态地构建和执行

SpringBoot中使用 ThreadLocal 进行多线程上下文管理及注意事项小结

《SpringBoot中使用ThreadLocal进行多线程上下文管理及注意事项小结》本文详细介绍了ThreadLocal的原理、使用场景和示例代码,并在SpringBoot中使用ThreadLo... 目录前言技术积累1.什么是 ThreadLocal2. ThreadLocal 的原理2.1 线程隔离2