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_categorywhere is_del=0andparent_category_id=#{_parameter,jdbcType=INTEGER}</select>

注意这边的resultMap就是上述定义的resultMap.

如果要递归获取所有的TCategory,我们只要获取所有category_type=1(即根类目),然后从根目录递归下去,注意这边的resultMap必须为TreeMap,才会触发递归。

 

<!-- 递归查询所有 --><select id="selectRecursionAll" resultMap="TreeMap">select*from t_categorywhere is_del=0andcategory_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);}
}

 

转载于:https://www.cnblogs.com/roy-blog/p/7080258.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/269095.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

福禄克DSX2-5000测试仪为CAT 6A认证保驾护航

我们都知道CAT 6A是目前被认为支持&#xff08;10 Gig&#xff09;千兆以太网速度性能最高的双绞线布线&#xff0c;它已经存在了十多年。所以&#xff0c;你可能会惊讶地发现第6类&#xff08;现在19岁了&#xff01;&#xff09;继续主导双绞线铜缆的销量第一名。 如果您查看…

一手指天,一手指地,开!

做了1年多的C#.NET的系统开发&#xff0c;我越来越觉得自个应该去写C逻辑程序。C#的语言更加重视的是如何去处理实现问题&#xff0c;如何去实现一种应用&#xff0c;这让我有点提不起劲&#xff0c;倒不是说高级语言的毛病&#xff0c;个人喜好而已。所以这段时间忙里偷闲&…

java.util.ComparableTimSort中的sort()方法简单分析

TimSort算法是一种起源于归并排序和插入排序的混合排序算法&#xff0c;设计初衷是为了在真实世界中的各种数据中能够有较好的性能。该算法最初是由Tim Peters于2002年在Python语言中提出的。 TimSort 是一个归并排序做了大量优化的版本号。对归并排序排在已经反向排好序的输入…

虚拟化运维工具对金融行业的解决方案

金融行业是一个专业领域&#xff0c;例如如医疗保健&#xff0c;需要以最大化的性能进行无中断运营。任何包括中断或降级在内的性能问题都是完全不可接受的。如果已经中断&#xff0c;后果将会是非常严重的。而明辰智航云安虚拟化网络与虚拟化性能管理系统可以帮助你尽可能的规…

简单说说通讯设备的热设计

从事通讯设备热设计工作8个月了&#xff0c;总体来说对这项工作有了些自己的认识。热设计成为一个专门的岗位在国内还没有多少年的历史&#xff0c;外企中&#xff0c;英特尔、思科、诺基亚、爱立信、山特电子等都有专门的岗位&#xff0c;国内企业&#xff0c;如华为、中兴、联…

使用DSX2-5000 CH测试时选择(+PoE)和(+All)后缀的含义

在测试双绞线布线时&#xff0c;需要在测试仪上设置一些关键的参数来测试永久链路符合行业标准参数&#xff0c;如插入损耗、NEXT、PSNEXT、ACR-N、PSACR-N、ACR-F、PSACR-F和回波损耗。在测试Cat 6A&#xff08;或ISO11801标准的Fa类&#xff09;时&#xff0c;我们还使用PSAN…

bat学习工具

BAT学习工具 最近开始学习批处理&#xff0c;整理了一下以下是3800的教程说明&#xff0c;和一些批处理教程的下载地址&#xff0c;附件是一个bat—exe的好软件VisualBat 1.08的&#xff0c;更新了exe—bat的转换教程到处都是&#xff0c;关键是自己要好好学习。亲密接触批处理…

苹果电脑(Mac)如何进行大小写和中英文的切换

中英文写切换&#xff1a;按一下caps lock键进行切换 大小写切换&#xff1a;长按caps lock键直至灯亮起 终端字体放大&#xff1a;按住command 和

SpringMVC路径匹配规则AntPathMatcher(转)

SpringMVC的路径匹配规则是依照Ant的来的. 实际上不只是SpringMVC,整个Spring框架的路径解析都是按照Ant的风格来的. 在Spring中的具体实现,详情参见 org.springframework.util.AntPathMatcher. 具体规则如下(来自Spring AntPathMatcher源码注释): * {link PathMatcher} implem…

Getting the right Exception Context from a Memory dump Fixed

吃饭回来&#xff0c;看到Share Source CLI团队的rss聚合上面Debug团队的juqiang发了一篇文章&#xff0c;说抓了一个minidump出现了&#xff1a;WARNING: Unable to verify timestamp for mscorwks.dll的错误。上次我在查看一个mini Dump的时候&#xff0c;ntdll.dll好像也出现…

以太网测试工具

以太网是计算机网络的一种局域网技术&#xff0c;多种标准指定了以太网技术的标准&#xff0c;其中包括了物理层、电子信号及基质层协议等多种内容。以太网时目前应用比较广泛的局域网技术。像企业中所用到网络即可成为以太网。那么如果企业中如果网络时长发生延迟、掉帧、断网…

单例模式中饿汉式

写法一&#xff1a; 写法二&#xff1a;

css3 动画的播放、暂停和重新开始

播放 先在keyframes中创建动画&#xff0c;之后把它捆绑到某个选择器&#xff0c;就可以产生动画效果。html <div id"box" class"box"></div> css keyframes mymove {0% {margin-left: 0px;}50% {margin-left: 400px;}100% {margin-left: 0px…

以太网性能测试仪

目前企业办公遇到的首要问题就是网络方面&#xff0c;企业网络中的网络不通、网速慢、丢包、IP地址冲突等各种问题。网络管理人员手里没有趁手的网络故障仪器&#xff0c;解决问题将会非常麻烦。手持式的、口袋型、具备多种测试标准的以太网性能测试仪是先解决企业中网路管理人…

拉斯廷老兄

从前打了一场大仗&#xff0c;大仗结束后&#xff0c;许多士兵被遣散回家。拉斯廷老兄也退役了&#xff0c;他除了一袋干粮和四个金币外一无所有地上路了。圣彼得装成一个可怜的乞丐站在拉斯廷老兄的必经之路上&#xff0c;等他走过来便向他乞讨。拉斯廷老兄回答说&#xff1a;…

极寒极热天气是否可以使用福禄克DSX2-5000网线测试仪工作

当在室外测试网线和光纤时&#xff0c;天气的温度不仅会影响到工作的效率&#xff0c;电缆如果进行拉扯弯折也可能会在安装测试过程中受损。福禄克指定经销商—明辰智航的工程师推荐您了解一下安装测试电缆时的电缆可承受的温度。 在冬天&#xff0c;寒冷的天气致使工作总是不会…

分析uboot中 make xxx_config过程

make xxx_config实质上就是调用了 首先看MKCONFIG&#xff1a; 【注意】SRCTREE源文件下的目录 之后的语句&#xff1a; $(MKCONFIG) $(:_config) arm arm920t EmbedSky NULL s3c2440就相当于执行 #mkconfig xxx arm arm920t EmbedSky NULL s3c2440 #$0 $1 $2 $3 $4 $5 $…

mysql创建索引语句

1:表结构 2:创建索引语句 alter table staffs add index idx_staffs_nameAgePos(NAME,age,pos); 执行后效果