Mybatis <collection> 标签使用。

13 篇文章 0 订阅
<?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="SHIRO_SpecSql" >
	
	<!-- 获得这个用户所有的菜单权限 -->
	<select id="searchSingleUserMenuAuthorities" parameterType="java.lang.String" resultMap="OneMenuAuthority">
		select 
		name name,
		ht_authority_id htAuthorityId,
		(select ${uid} from dual ) currentUserId
		from ht_authority 
		where pid = 0
	</select>
	<resultMap type="com.sailod.shiro.dto.HtAuthorityMenuDTO" id="OneMenuAuthority">
		<id property="htAuthorityId" column="htAuthorityId" javaType="java.lang.Long" />
		<result property="name" column="name" javaType="java.lang.String" />
		<result property="currentUserId" column="currentUserId" javaType="java.lang.Long" />
		<collection property="htAuthorityDTO"  ofType="com.sailod.shiro.dto.HtAuthorityDTO"
		 select="selectAuthority" column="{htAuthorityId2 = htAuthorityId ,currentUserId2 = currentUserId}"   >
		 </collection>
	</resultMap>
	<select id="selectAuthority" parameterType="java.util.HashMap" resultType="com.sailod.shiro.dto.HtAuthorityDTO" resultMap="OneAuthority"  >
		select ha.name name,
		ha.url url ,
		ha.ht_authority_id htauthorityid,
		ha.pid pid,
		ha.type type,
		ha.permission permission,
		hua.ht_user_id currUserId
		from ht_authority ha
		left join ht_user_authority hua on hua.ht_authority_id = ha.ht_authority_id 
		where ha.pid = ${htAuthorityId2}
		and ha.type = 'menu' 
		and hua.ht_user_id = ${currentUserId2} 
	</select>
	<resultMap type="com.sailod.shiro.dto.HtAuthorityDTO" id="OneAuthority" >
		<id property="pid" column="pid" javaType="java.lang.Long" />
		<result property="name" column="name"  javaType="java.lang.String"/>
		<result property="url" column="url" javaType="java.lang.String"/>
		<result property="type" column="type" javaType="java.lang.String"/>
		<result property="permission" column="permission" javaType="java.lang.String"/>
		<result property="htAuthorityId" column="htauthorityid" javaType="java.lang.Long"/>
		<result property="currUserId" column="currUserId" javaType="java.lang.Long" />
	</resultMap>
</mapper>


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

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

package com.sailod.shiro.dto;

import java.util.List;

/**
 * 查询菜单用到dto
 * 
 *
 */
public class HtAuthorityMenuDTO {
	

	private String name;
	
	private Long htAuthorityId;
	
	private Long currentUserId;
	
	private List<HtAuthorityDTO> htAuthorityDTO;

	public Long getHtAuthorityId() {
		return htAuthorityId;
	}

	public void setHtAuthorityId(Long htAuthorityId) {
		this.htAuthorityId = htAuthorityId;
	}

	

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public List<HtAuthorityDTO> getHtAuthorityDTO() {
		return htAuthorityDTO;
	}

	public void setHtAuthorityDTO(List<HtAuthorityDTO> htAuthorityDTO) {
		this.htAuthorityDTO = htAuthorityDTO;
	}

	public Long getCurrentUserId() {
		return currentUserId;
	}

	public void setCurrentUserId(Long currentUserId) {
		this.currentUserId = currentUserId;
	}
	
	
}


 

package com.sailod.shiro.dto;

/**
 * 查询菜单用到dto
 * 
 *
 */
public class HtAuthorityDTO {
	
	//这个权限的主键
	private Long htAuthorityId;
	//父菜单的主键
	private Long pid;
	
	private String name;
	
	private String url;
	//类型
	private String type;
	
	private String permission;
	
	private Long currUserId;
	
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public Long getHtAuthorityId() {
		return htAuthorityId;
	}

	public void setHtAuthorityId(Long htAuthorityId) {
		this.htAuthorityId = htAuthorityId;
	}

	public Long getPid() {
		return pid;
	}

	public void setPid(Long pid) {
		this.pid = pid;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getPermission() {
		return permission;
	}

	public void setPermission(String permission) {
		this.permission = permission;
	}

	public Long getCurrUserId() {
		return currUserId;
	}

	public void setCurrUserId(Long currUserId) {
		this.currUserId = currUserId;
	}

	
	
	
}


 

 低效率 collection:

高效率collection,  

1 查询用联合查询

2<collection 里面不写column  

  • 15
    点赞
  • 50
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值