需求是:递归查询资源

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:递归查询,关联查询传入多个参数的更多相关文章

  1. SpringBoot+Mybatis实现关联查询

    SpringBoot+Mybatis实现关联查询 今天学习了下Mybatis的动态查询,然后接着上次的Demo改造了下实现表的关联查询. 话不多说,开始今天的小Demo 首先接着上次的项目 https ...

  2. JavaWeb_(Mybatis框架)关联查询_六

    系列博文: JavaWeb_(Mybatis框架)JDBC操作数据库和Mybatis框架操作数据库区别_一 传送门 JavaWeb_(Mybatis框架)使用Mybatis对表进行增.删.改.查操作_ ...

  3. Mybatis之关联查询

    一.一对一关联 1.1.提出需求 根据班级id查询班级信息(带老师的信息) 1.2.创建表和数据 创建一张教师表和班级表,这里我们假设一个老师只负责教一个班,那么老师和班级之间的关系就是一种一对一的关 ...

  4. Mybatis之关联查询及动态SQL

    前言 实际开发项目中,很少是针对单表操作,基本都会联查多表进行操作,尤其是出一些报表的内容.此时,就可以使用Mybatis的关联查询还有动态SQL.前几篇文章已经介绍过了怎么调用及相关内容,因此这里只 ...

  5. mybatis一对一关联查询——(八)

    1.需求 查询所有订单信息,关联查询下单用户信息. 注意: 因为一个订单信息只会是一个人下的订单,所以从查询订单信息出发关联查询用户信息为一对一查询.如果从用户信息出发查询用户下的订单信息则为一对多查 ...

  6. mybatis中sql语句传入多个参数方法

    1 使用map <select id="selectRole" parameterType="map" resultType="RoleMap& ...

  7. Mybatis一对一关联查询

    有两张表,老师表teacher和班级表class,一个class班级对应一个teacher,一个teacher对应一个class 需求是根据班级id查询班级信息(带老师的信息) 创建teacher和c ...

  8. MyBatis学习(四)MyBatis一对一关联查询

    一对一关联查询即.两张表通过外键进行关联.从而达到查询外键直接获得两张表的信息.本文基于业务拓展类的方式实现. 项目骨架 配置文件conf.xml和db.properties前几节讲过.这里就不细说了 ...

  9. SSM-MyBatis-15:Mybatis中关联查询(多表操作)

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 先简单提及一下关联查询的分类 1.一对多 1.1单条SQL操作的 1.2多条SQL操作的 2.多对一 2.1单 ...

随机推荐

  1. GIL 全局解释器

    全局解释器锁 GIL 相当于给python解释器加了一把互斥锁 每一个进程都有一把互斥锁,所有线程必须先拿到解释器,才能执行代码, 同一进程下,所有线程并发 在 Cpython 解释器下,多个进程可以 ...

  2. 016、Dockerfile 常用命令(2019-01-07 周一)

    参考https://www.cnblogs.com/CloudMan6/p/6864000.html   Dokcerfile常见命令   FROM     指定base镜像   MAINTAINER ...

  3. [Windows] [VS] [C] [取得指针所指内存的二进制形式字符]

    // 取得指针所指内存的十六进制形式字符串,size指定字节长度#define Mem_toString(address, size) _Mem_toString((PBYTE)address, si ...

  4. Android弹出窗口

    protected void PopUp() { final PopupWindow popup = new PopupWindow(TestActivity.this); View popView ...

  5. 【bzoj 4756】[Usaco2017 Jan] Promotion Counting

    Description The cows have once again tried to form a startup company, failing to remember from past ...

  6. nginx 相关命令

    验证配置是否正确: nginx -t 查看Nginx的版本号:nginx -V 启动Nginx:start nginx 快速停止或关闭Nginx:nginx -s stop 正常停止或关闭Nginx: ...

  7. promise.all方法合并请求接口的两个值

    function promise1 = new Promise((resolve,reject)=>{ return result1 }) function promise2 = new Pro ...

  8. 4-20模块 序列化模块 hashlib模块

    1,模块,py文件就是模块,py之所以好用就是模块多. 2,模块的分类: 1,内置模块,python 安装时自带的模块 2,扩展模块,别人写好的,需要安装之后,可以直接使用.itchat微信模块, b ...

  9. Python 爬虫六 性能相关

    前面已经讲过了爬虫的两大基础模块: requests模块:用来伪造请求爬取数据 bs4模块:用来整理,提取数据 当我们真正的开始有需求的时候通常都是批量爬取url这样的.那如何批量爬取呢? 按照正常的 ...

  10. Python 中的比较:is 与 ==

    转载: https://www.cnblogs.com/kiko0o0/p/8135184.html 在 Python 中会用到对象之间比较,可以用 ==,也可以用 is .但是它们的区别是什么呢? ...