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单 ...
随机推荐
- windows配置Java环境变量
打开电脑的高级系统设置-高级-环境变量 新建系统变量,如果已存在则添加到变量尾部,切勿覆盖 变量名:JAVA_HOME 变量值:C:\Program Files\Java\jdk1..0_102 变量 ...
- Good, then we can start
- windows 使用 php 的exif 问题 Call to undefined function exif_imagetype()
保证 extension=php_mbstring.dll 在 extension=php_exif.dll 之前
- chart.js angular组件封装(ng6)、实战配置、插件编写
前言 项目需要使用chart.js插件,由于项目是使用angular开发,那么我第一步就是先把chart.js改造成angular组件来使用. 本项目代码都可以在github上下载:项目git地址 a ...
- XOR 加密
XOR 是一个神奇的运算符, 观察它的真值表, 很容易得到以下结论: 假设现有 a , b 变量, 则 a ^ 0 == a a ^ 0xff == ~a (取反加1等于作为补码的a的真值的相反数的补 ...
- luogu 2014 选课 树上背包
树上背包 #include<bits/stdc++.h> using namespace std; ; const int inf=0x3f3f3f3f; vector<int> ...
- linux C遍历目录下文件
参考链接: http://blog.sina.com.cn/s/blog_626b7339010161tr.html
- java json 转换
1.直接输出: 2.字符串 通过eval转换输出,里面涉及到一个转义问题,还要注意eval的用法里面需要加"("+ + ")" 3.
- imooc-free
前端性能优化-通用的缓存SDK 依赖项 1.在hosts文件中配置 127.0.0.1 http://cc.imooc.com 就可以直接访问 http://cc.imooc.com:3000 ...
- POJ 1458 Common Subsequence 最长公共子序列
题目大意:求两个字符串的最长公共子序列 题目思路:dp[i][j] 表示第一个字符串前i位 和 第二个字符串前j位的最长公共子序列 #include<stdio.h> #include&l ...