需求是:递归查询资源

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. Nginx 学习笔记(八)http和https跨域问题解决

    今天在做网易云信的时候,修改了一下源码,使用自己的服务端进行登陆, 注意:这里是使用http域名访问https域名 1.下载源码,配置了IM的域名,im.tinywan.com 没有开启https,具 ...

  2. vue使用字体图标转码问题

    使用iconfont字体之后,出现的是一个方框,而没有出现相应的图标 需要通过String.fromCharCode方法转成指定unicode编码对应的十六进制字符 <div class=&qu ...

  3. mvn项目压缩打包

    通常情况下,maven打包结果为jar或war包.如果需要一并打包配置文件等参数,通过resources配置指定需要打包的文件参数,如下示例: <project> ... <!-- ...

  4. Java SSM框架之MyBatis3(十)MyBatis批量插入数据(MySql)

    插入成功后返回自增主键 <insert id="insertRole" parameterType="role" useGeneratedKeys=&qu ...

  5. Devexpress Winform 使用MVVM

    MVVM在WPF里很早就有了,在Winform里Devexpress最近几个大版本才有的事,上一段代码. 现在对话框上添加三个控件simpleButton1,simpleButton2,textEdi ...

  6. 洛谷P1972 【[SDOI2009]HH的项链】

    这道题想了很久,发题解是为了理解的更深刻一点...(管理放我过好嘛qwq) 步入正题:这道题应该是很多做法,我选择的是离线+树状数组. 首先输入数组.用fisrt数组先记录元素最开始出现的位置,对应的 ...

  7. Java基础_0206:方法的定义与使用

    方法的基本概念 方法的主要功能是封装可以执行的一段代码,这样不仅可以进行重复调用,更可以方便的实现代码的维护,而本次使用的方法定义语法如下所示. public static 返回值类型 方法名称(参数 ...

  8. 浏览器支持webp格式

    使用插件http://www.etherdream.com/WebP/WebP.js

  9. code forces 805B (水)

    #include<stdio.h>#include<iostream>#include<algorithm>#include<string.h>#inc ...

  10. 20165234 《Java程序设计》第五周学习总结

    第五周学习总结 教材学习内容总结 第七章 内部类与异常类 内部类 内部类:在一个类中定义另一个类. 外嵌类:包含内部类的类,称为内部类的外嵌类. 内部类的类体中不能声明类变量和类方法.外嵌类的类体中可 ...