mybatis:递归查询,关联查询传入多个参数
需求是:递归查询资源
1.资源类 EntityBaseResource:
public final class EntityBaseResource {
private Long resID = 0l;
private String resName = "";
private String urlPath = "";
private String parentResID = "";
private String iconClass = "fa fa-circle-o";
private short level = 1;
private String resType = "";
private String permission = "";
private String belongTo = "";
private String isPublished = "";
private short seq = 1;
private String isValid = "1";
// 子资源
private List<EntityBaseResource> children;
public final Long getResID() {
return resID;
}
public final void setResID(Long resID) {
this.resID = resID;
}
public final String getResName() {
return resName;
}
public final void setResName(String resName) {
this.resName = resName == null ? Constant.BLANK : resName.trim();
}
public final String getUrlPath() {
return urlPath;
}
public final void setUrlPath(String urlPath) {
this.urlPath = urlPath == null ? Constant.BLANK : urlPath.trim();
}
public final String getParentResID() {
return parentResID;
}
public final void setParentResID(String parentResID) {
this.parentResID = parentResID == null ? Constant.BLANK : parentResID.trim();
}
public final String getIconClass() {
return iconClass;
}
public final void setIconClass(String iconClass) {
this.iconClass = iconClass == null ? Constant.BLANK : iconClass.trim();
}
//省略get/set方法
2.DAO
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.csget.entity.base.EntityBaseResource;
public interface DaoBaseResource {
int deleteByPrimaryKey(Long resID);
int insert(EntityBaseResource record);
EntityBaseResource selectByPrimaryKey(Long resID);
List<EntityBaseResource> selectRecursionRes(@Param("resID") Long resID, @Param("belongTo") String belongTo);
int updateByPrimaryKey(EntityBaseResource record);
}
<?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="com.csget.dao.base.DaoBaseResource"> <!-- 基础属性 -->
<resultMap id="BaseResultMap" type="com.csget.entity.base.EntityBaseResource">
<id column="resID" jdbcType="BIGINT" property="resID" />
<result column="resName" jdbcType="VARCHAR" property="resName" />
<result column="urlPath" jdbcType="VARCHAR" property="urlPath" />
<result column="parentResID" jdbcType="VARCHAR"
property="parentResID" />
<result column="iconClass" jdbcType="VARCHAR"
property="iconClass" />
<result column="level" jdbcType="TINYINT" property="level" />
<result column="resType" jdbcType="VARCHAR" property="resType" />
<result column="permission" jdbcType="VARCHAR"
property="permission" />
<result column="seq" jdbcType="TINYINT" property="seq" />
<result column="isValid" jdbcType="CHAR" property="isValid" />
</resultMap>
<!-- 递归资源,继承基础属性 -->
<resultMap id="resAll" extends="BaseResultMap" type="com.csget.entity.base.EntityBaseResource">
<collection property="children" ofType="resAll"
column="{resID=resID,belongTo=belongTo}" select="selectRecursionRes" />
</resultMap> <!-- 递归资源 -->
<select id="selectRecursionRes" resultMap="resAll">
SELECT resID, resName, urlPath, parentResID, iconClass, belongTo FROM t_base_resource
WHERE parentResID=#{resID} and belongTo=#{belongTo} and isValid='1' ORDER BY seq ASC
</select>
</mapper>

DAO在调用时,通过注解传入参数 
递归查询时多个参数参数,会自动调用查询结果中的字段值

同时递归查询,resultMap采用的继承属性 ,这样可以避免一些不必要的查询,例如如果你只需要查询一条记录,不需要查询它下面的子集。

mybatis:递归查询,关联查询传入多个参数的更多相关文章
- SpringBoot+Mybatis实现关联查询
SpringBoot+Mybatis实现关联查询 今天学习了下Mybatis的动态查询,然后接着上次的Demo改造了下实现表的关联查询. 话不多说,开始今天的小Demo 首先接着上次的项目 https ...
- JavaWeb_(Mybatis框架)关联查询_六
系列博文: JavaWeb_(Mybatis框架)JDBC操作数据库和Mybatis框架操作数据库区别_一 传送门 JavaWeb_(Mybatis框架)使用Mybatis对表进行增.删.改.查操作_ ...
- Mybatis之关联查询
一.一对一关联 1.1.提出需求 根据班级id查询班级信息(带老师的信息) 1.2.创建表和数据 创建一张教师表和班级表,这里我们假设一个老师只负责教一个班,那么老师和班级之间的关系就是一种一对一的关 ...
- Mybatis之关联查询及动态SQL
前言 实际开发项目中,很少是针对单表操作,基本都会联查多表进行操作,尤其是出一些报表的内容.此时,就可以使用Mybatis的关联查询还有动态SQL.前几篇文章已经介绍过了怎么调用及相关内容,因此这里只 ...
- mybatis一对一关联查询——(八)
1.需求 查询所有订单信息,关联查询下单用户信息. 注意: 因为一个订单信息只会是一个人下的订单,所以从查询订单信息出发关联查询用户信息为一对一查询.如果从用户信息出发查询用户下的订单信息则为一对多查 ...
- mybatis中sql语句传入多个参数方法
1 使用map <select id="selectRole" parameterType="map" resultType="RoleMap& ...
- Mybatis一对一关联查询
有两张表,老师表teacher和班级表class,一个class班级对应一个teacher,一个teacher对应一个class 需求是根据班级id查询班级信息(带老师的信息) 创建teacher和c ...
- MyBatis学习(四)MyBatis一对一关联查询
一对一关联查询即.两张表通过外键进行关联.从而达到查询外键直接获得两张表的信息.本文基于业务拓展类的方式实现. 项目骨架 配置文件conf.xml和db.properties前几节讲过.这里就不细说了 ...
- SSM-MyBatis-15:Mybatis中关联查询(多表操作)
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 先简单提及一下关联查询的分类 1.一对多 1.1单条SQL操作的 1.2多条SQL操作的 2.多对一 2.1单 ...
随机推荐
- Java秒杀系统方案优化 高性能高并发实战(1)
首先先把 springboot +thymeleaf 搞起来 ,参考 springboot 官方文档 本次学习 使用 springboot + thymeleaf+mybatis+redis+Rabb ...
- 解析ArcGis的标注(一)——先看看分数式、假分数式标注是怎样实现的
该“标注”系列博文的标注引擎使用“标准标注引擎(standard label engine)”,这个概念如不知道,可不理会,ArcGis默认标注引擎就是它. ArcGis的标注表达式支持VBScrip ...
- Kettle系列:使用Kudu API插入数据到Kudu中
本文详细介绍了在Kettle中使用 Kudu API将数据写入Kudu中, 从本文可以学习到:1. 如何编写一个简单的 Kettle 的 Used defined Java class.2. 如何读取 ...
- js 操作对象 记录
js 对象记录一下: let obj_1 = { name : 'james', age : '22', sex: '1' } for ( i in obj_1 ) { console.log(i) ...
- ASP.NET Web API 2 媒体类型格式化程序
Ø 简介 在之前的ASP.NET Web API 2 消息处理管道文章中有提到,在 Web API 的生命周期中,还包含比较中要的一部分,就是媒体类型格式化程序,该程序主要用于处理 Web API ...
- html5 缓存实例
html5 有两种缓存 1.localStorage:浏览器关闭后,数据库还存在. 2.sessionStorage:session缓存,浏览器关闭后,数据已经不存在. 实例一:localStorag ...
- PS与AI快捷键小结
[文档整理系列] PS与AI快捷键小结PS快捷键 填充前景色 alt+del填充背景色 crel+del前景色与背景色互换: x[英文状态] 切换打开的文件:ctrl + tab关闭当前文件: ctr ...
- Java SE之正则表达式一:概述
正则表达式 概念 定义:符合一定规则的表达式 作用:用于专门操作字符串 特点:用于一些特定的符号表示代码的操作,这样就简化了长篇的程序代码 好处:可以简化对字符串的复杂操作 弊端:符号定义越多,正则越 ...
- iTOP-6818开发板支持AXP228电源管理[官方推荐最佳匹配]_支持动态调频
iTOP-6818开发板与4418开发板共兼容同一底板: 核心板:::::: 尺寸 50mm*60mm 高度 核心板连接器组合高度1.5mm PCB层数 6层PCB沉金设计 4418 CPU ARM ...
- 合肥学院第二届卓越IT-程序设计大赛E+J
E链接 小飞有nn的魔法纸片(可以变出糖果), 现在有m个人来找小飞玩游戏, 小飞希望用魔法纸片来使朋友们开心,纸片可以被随便裁剪,nn的魔法纸片可以裁剪成任意大小的小魔法纸片,小飞通过pubgoso ...