mysql+mybatis递归调用
递归调用的应用场景常常出现在多级嵌套的情况,比如树形的菜单。下面通过一个简单的例子来实现mysql+mybatis的递归。
数据模型
private Integer categoryId; private String categoryName; private Integer isRoot; private Integer categoryLevel; private Integer rootCategoryId; private Integer parentCategoryId; private String parentCategoryName;
以上是一个简单的类目的数据实体,主要要注意通过乐parentCategoryId实现了父子的关联。
数据库数据
我们可以很简单的通过父级的id获取其直接子级列表。但是如果我们想要通过某一父级id获取其直接下属和间接下属的(子,孙,曾孙等)列表呢?这就需要用到递归来实现。
实现方法
首先,我们在实体类下面加上这么一个属性。
public List<TCategory>childList=new ArrayList<TCategory>();//子Category列表
然后,我们编写xml文件中的ResultMap,如下。
<!-- 带有chlidList的map -->
<resultMap id="TreeMap" type="com.qgranite.entity.TCategory">
<id column="category_id" property="categoryId" jdbcType="INTEGER" />
<result column="category_name" property="categoryName"
jdbcType="VARCHAR" />
<result column="category_remark" property="categoryRemark"
jdbcType="VARCHAR" />
<result column="category_type" property="categoryType"
jdbcType="INTEGER" />
<result column="is_root" property="isRoot" jdbcType="INTEGER" />
<result column="category_level" property="categoryLevel"
jdbcType="INTEGER" />
<result column="root_category_id" property="rootCategoryId"
jdbcType="INTEGER" />
<result column="parent_category_id" property="parentCategoryId"
jdbcType="INTEGER" />
<result column="parent_category_name" property="parentCategoryName"
jdbcType="VARCHAR" />
<collection property="childList" column="category_id"
ofType="com.qgranite.entity.TCategory" select="selectRecursionByParentCategoryId"></collection>
</resultMap>
最后一句是关键,它说明了递归所需要调用的方法selectRecursionByParentCategoryId。
然后我们来写这个递归方法。
<!-- 根据父键递归查询 -->
<select id="selectRecursionByParentCategoryId" resultMap="TreeMap"
parameterType="java.lang.Integer">
select
*
from t_category
where is_del=0
and
parent_category_id=#{_parameter,jdbcType=INTEGER}
</select>
注意这边的resultMap就是上述定义的resultMap.
如果要递归获取所有的TCategory,我们只要获取所有category_type=1(即根类目),然后从根目录递归下去,注意这边的resultMap必须为TreeMap,才会触发递归。
<!-- 递归查询所有 -->
<select id="selectRecursionAll" resultMap="TreeMap">
select
*
from t_category
where is_del=0
and
category_type=1
</select>
接下来写后台调用方法。
/**
* 根据特定父类递归查询所有子类
*
* @param categoryId
* @return
*/
public List<TCategory> allCategoryRecursion() {
return baseDao
.findTList(
"TCategoryMapper.selectRecursionAll");
}
/**
* 根据特定父类递归查询所有子类
*
* @param categoryId
* @return
*/
public List<TCategory> subCategoryListByParentId(int categoryId) {
return baseDao
.findTListByParam(
"TCategoryMapper.selectRecursionByParentCategoryId",
categoryId);
}
/**
* 根据categoryId获取子孙categoryId的id字符串,用逗号隔开
*
* @param categoryId
* @return
*/
public String subCategoryStrByParentId(Integer categoryId) {
String categoryStr = categoryId.toString();
List<TCategory> categoryList = baseDao
.findTListByParam(
"TCategoryMapper.selectRecursionByParentCategoryId",
categoryId);
int size = categoryList.size();
for (int i = 0; i < size; i++) {
TCategory category = categoryList.get(i);
categoryStr = categoryStr + "," + category.getCategoryId();
if (!category.getChildList().isEmpty()) {
Iterator<TCategory> it = category.getChildList().iterator();
while (it.hasNext()) {
categoryStr = categoryStr + "," + it.next().getCategoryId();
}
}
}
return categoryStr;
}
其中baseDao的代码如下。
package com.qgranite.dao; import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List; import javax.annotation.Resource; import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.stereotype.Repository; /**
* 所有dao基类
*
* @author xdx
*
* @param <T>
* @param <PK>
*/
@Repository("baseDao")
public class BaseDao<T, PK extends Serializable> {
private Class<T> enetityClass;
@Resource(name = "sqlSessionTemplate")
private SqlSessionTemplate sqlSessionTemplate; // 构造方法,根据实例类自动获取实体类型,这边利用java的反射
public BaseDao() {
this.enetityClass = null;
Class c = getClass();
Type t = c.getGenericSuperclass();
if (t instanceof ParameterizedType) {
ParameterizedType p = (ParameterizedType) t;
Type[] type = p.getActualTypeArguments();
this.enetityClass = (Class<T>) type[0];
}
} /**
* 获取实体
*
* @param id
* @return
*/
public T getT(String sql, Object param) {
return sqlSessionTemplate.selectOne(sql, param);
}
/**
* 不带查询参数的列表
* @param str
* @return
* @throws Exception
*/
public List<T> findTList(String sql){
return sqlSessionTemplate.selectList(sql);
} /**
* 带有参数的列表
*
* @param str
* @param param
* @return
* @throws Exception
*/
public List<T> findTListByParam(String sql, Object param) {
return sqlSessionTemplate.selectList(sql, param);
} /**
* 插入一条数据,参数是t
*
* @param sql
* @param t
* @return
*/
public int addT(String sql, T t) {
return sqlSessionTemplate.insert(sql, t);
}
/**
* 修改一条数据,参数是t
* @param sql
* @param t
* @return
*/
public int updateT(String sql,T t){
return sqlSessionTemplate.update(sql, t);
}
/**
* 删除t,参数是主键
* @param sql
* @param t
* @return
*/
public int deleteT(String sql,PK pk){
return sqlSessionTemplate.delete(sql, pk);
}
/**
* 根据param获取一个对象
* @param sql
* @param param
* @return
*/
public Object getObject(String sql,Object param){
return sqlSessionTemplate.selectOne(sql,param);
}
}
mysql+mybatis递归调用的更多相关文章
- Java Spring+Mysql+Mybatis 实现用户登录注册功能
前言: 最近在学习Java的编程,前辈让我写一个包含数据库和前端的用户登录功能,通过看博客等我先是写了一个最基础的servlet+jsp,再到后来开始用maven进行编程,最终的完成版是一个 Spri ...
- mysql中递归树状结构<转>
在Oracle 中我们知道有一个 Hierarchical Queries 通过CONNECT BY 我们可以方便的查了所有当前节点下的所有子节点.但很遗憾,在MySQL的目前版本中还没有对应的功能. ...
- mysql 树结构递归处理
日常开发中我们经常会遇到树形结构数据处理,一般表结构通常会常用id,pid这种设计方案. 之前用oracle.sqlServer数据库,用相应的语法即可获取树形结构数据(oracel:connect ...
- springboot学习笔记:8. springboot+druid+mysql+mybatis+通用mapper+pagehelper+mybatis-generator+freemarker+layui
前言: 开发环境:IDEA+jdk1.8+windows10 目标:使用springboot整合druid数据源+mysql+mybatis+通用mapper插件+pagehelper插件+mybat ...
- springboot学习笔记:10.springboot+atomikos+mysql+mybatis+druid+分布式事务
前言 上一篇文章我们整合了springboot+druid+mybatis+mysql+多数据源: 本篇文章大家主要跟随你们涛兄在上一届基础上配置一下多数据源情况下的分布式事务: 首先,到底啥是分布式 ...
- Python-函数的递归调用
递归调用顾名思义即在函数内部调用函数(自己调用自己),通常用它来计算阶乘,累加等 注意: - 必须有最后的默认结果 if n ==0,(不能一直调用自己,如果没有可能会造成死循环) - 递归参数必 ...
- mybatis动态调用表名和字段名
以后慢慢启用个人博客:http://www.yuanrengu.com/index.php/mybatis1021.html 一直在使用Mybatis这个ORM框架,都是使用mybatis里的一些常用 ...
- 关于C++的递归调用(n的阶乘为例)
C++,是入门编程界的一门初期的语言.今天我们浅谈一下有关C++的递归调用. 在没有继承,多态,封装之前,C++几乎看成是C语言,除了一些简单的输出和头文件. 具体代码实现如下: #include&l ...
- java中父类与子类, 不同的两个类中的因为构造函数由于递归调用导致栈溢出问题
/* 对于类中对成员变量的初始化和代码块中的代码全部都挪到了构造函数中, 并且是按照java源文件的初始化顺序依次对成员变量进行初始化的,而原构造函数中的代码则移到了构造函数的最后执行 */ impo ...
随机推荐
- 圆形border渐变加载
自己遇到的一个需求,圆形的border加载.初听大家一定认为很简单,确实很简单,但是突然又加了一个需求,就是border的颜色要进行渐变,用了很多方法,可以实现渐变,直接的一个css属性是border ...
- div内长串数字或字母不断行处理
比如: <div>1111tryrt645645rt4554111112324353453454364</div> <div>qwewretrytuytuiyiuo ...
- vim如何显示行号
在 vim 里执行(在普通模式下直接按冒号,并输入下面的命令 :set number 回车后就可以显示行号了,但重启 vim 后又会恢复默认的设置. 要想每次进入 vim 都显示行号就需要配置 vim ...
- centos7 jdk8 tomcat8 安装
安装jdk # cd /opt/# wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F ...
- http2.4简单配置
前言: 上一篇博文说到了http的发展以及http完整请求响应的工作流程. 一.开篇: 从最简单的静态服务器开始. 之前说过,http是应用层协议,必定会在用户空间体现出具体的应用程序.常见的http ...
- 微信JS-SDK 选取手机照片并进行上传
项目中遇到需要选取照片上传的需求,因为网页运行在微信的浏览器里面,所以用微信的 js-sdk 提供的选取照片功能,来进行项目开发.实际开发中需要用到微信web开发者工具,详细参考链接:https:// ...
- 关于vue 框架与后台框架的混合使用的尝试
这几天我在研究前台框架和后台框架融合的问题,进行了一些尝试; 我前台选择的是 vue,当然也可以选择 react 等其他 mvvm 框架,不过 vue 对于我来说是最熟悉的; 后台话,我选择的是 ph ...
- 状态压缩 - LeetCode #464 Can I Win
动态规划是一种top-down求解模式,关键在于分解和求解子问题,然后根据子问题的解不断向上递推,得出最终解 因此dp涉及到保存每个计算过的子问题的解,这样当遇到同样的子问题时就不用继续向下求解而直接 ...
- 【复习】VueJS之内部指令
Vuejs 源码:https://github.com/zhuangZhou/vuejs 下载Vue.js 官网:http://vuejs.org live-server使用 live-server是 ...
- php 7.2 一些注意事项.
<?php $b = array(); each($b); // Deprecated: The each() function is deprecated. This message will ...