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,如果你需要通过XML文件来返回一个`List<Student>`的结果集,其`Student`包含`id`和一个`List<Info>`类型的属性`infor`,你需要定义相应的映射文件。以下是一个基本的示例来说明如何实现这一点: 首先,你需要定义`Student`类以及它的属性和`List<Info>`的getter和setter方法。 ```java public class Student { private Integer id; private List<Info> infor; // getter 和 setter 方法 public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public List<Info> getInfor() { return infor; } public void setInfor(List<Info> infor) { this.infor = infor; } } public class Info { // Info类的属性和方法 } ``` 接着,在MyBatis的映射文件,你需要配置一个查询语句和结果映射: ```xml <mapper namespace="com.example.mapper.StudentMapper"> <!-- 结果映射 --> <resultMap id="StudentResultMap" type="Student"> <id property="id" column="student_id" /> <collection property="infor" ofType="Info"> <!-- Info类的属性映射 --> <id property="id" column="info_id" /> <!-- 其他Info类的属性映射 --> </collection> </resultMap> <!-- 查询语句 --> <select id="selectStudents" resultMap="StudentResultMap"> SELECT s.id as student_id, i.id as info_id, ... FROM student s LEFT JOIN info i ON s.id = i.student_id WHERE ... </select> </mapper> ``` 在这个例子,`resultMap`定义了如何将数据库查询结果映射到`Student`对象以及它的`infor`属性。`<collection>`标签用于映射`List`类型的属性,`ofType`指定了集合元素的类型。`<select>`标签定义了查询语句,其的`resultMap`属性指定了结果集应该使用哪个`resultMap`进行映射。 查询语句应该返回所有必要的列,以确保`Student`对象和它包含的`Info`对象能够被正确地填充。`as`关键字用于给结果列起别名,这些别名需要与`resultMap`的`property`和`column`相对应。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值