mybatis递归,一对多代码示例
今天需要做一个功能,根据专业,有不同的章节,章节下面有对应的习题,
由于只有这么两级,可以不用使用递归,直接查询父集,之后foreach查询子集放入对应的list集合。
虽然实现了,感觉毕竟,太low。
有同事跟我说可以使用mybatis的递归实现,就学习了下。
对应的bean里面需要有对应的list<bean> lists的引用。
直接上代码
对应的sql语句
CREATE TABLE `goods_category` (
`goodscateid` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`parentid` int(11) DEFAULT NULL,
`description` varchar(255) DEFAULT NULL,
`displayorder` int(11) DEFAULT NULL,
`commissionrate` double DEFAULT NULL,
`enabled` int(11) DEFAULT NULL,
PRIMARY KEY (`goodscateid`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8; /*Data for the table `goods_category` */
insert into `goods_category`(`goodscateid`,`name`,`parentid`,`description`,`displayorder`,`commissionrate`,`enabled`) values (1,'java',0,'',NULL,NULL,NULL),(2,'spring',1,'',NULL,NULL,NULL),(3,'springmvc',1,'',NULL,NULL,NULL),(4,'struts',1,'',NULL,NULL,NULL),(5,'jdbc',0,'',NULL,NULL,NULL),(6,'hibernate',5,'',NULL,NULL,NULL),(7,'mybatis',5,'',NULL,NULL,NULL),(8,'jdbctemplate',5,'',NULL,NULL,NULL),(9,'beanfactory',3,'',NULL,NULL,NULL),(10,'factorybean',3,'',NULL,NULL,NULL);
实体类
@JsonIgnoreProperties({"displayorder","commissionrate","enabled"})
public class GoodsCategoryVo {
private Integer goodscateid;
private String name;
private Integer parentid;
private String description;
private Integer displayorder;
private Double commissionrate;
private Integer enabled;
private List<GoodsCategoryVo> catelist;
get 。。。 set。。。 tostring。。。
dao层
public interface GoodsMapper {
List<GoodsCategoryVo> getCategory(Integer pid);
}
mapper.xml
<resultMap id="getSelf" type="com.bscc.beans.GoodsCategoryVo">
<id column="goodscateid" property="goodscateid"></id>
<result column="name" property="name"></result>
<collection property="catelist" select="getCategory"
column="goodscateid"></collection>
<!--查到的cid作为下次的pid -->
</resultMap> <select id="getCategory" resultMap="getSelf">
select * from goods_category where parentid=#{pid}
ORDER BY displayorder,goodscateid
</select>
之后直接访问对应的方法,即可查询出来
@RequestMapping("/getGoodsList")
@ResponseBody
public List<GoodsCategoryVo> getGoodsList(){
// pid指定为0
List<GoodsCategoryVo> list = goodsMapper.getCategory(0);
return list;
}
结果,可以使用json在线工具
[
{
"goodscateid": 1,
"name": "java",
"parentid": 0,
"description": "111",
"catelist": [
{
"goodscateid": 2,
"name": "spring",
"parentid": 1,
"description": "222",
"catelist": []
},
{
"goodscateid": 3,
"name": "springmvc",
"parentid": 1,
"description": "333",
"catelist": [
{
"goodscateid": 9,
"name": "beanfactory",
"parentid": 3,
"description": "999",
"catelist": []
},
{
"goodscateid": 10,
"name": "factorybean",
"parentid": 3,
"description": "000",
"catelist": []
}
]
},
{
"goodscateid": 4,
"name": "struts",
"parentid": 1,
"description": "444",
"catelist": []
}
]
},
{
"goodscateid": 5,
"name": "jdbc",
"parentid": 0,
"description": "555",
"catelist": [
{
"goodscateid": 6,
"name": "hibernate",
"parentid": 5,
"description": "666",
"catelist": []
},
{
"goodscateid": 7,
"name": "mybatis",
"parentid": 5,
"description": "777",
"catelist": []
},
{
"goodscateid": 8,
"name": "jdbctemplate",
"parentid": 5,
"description": "888",
"catelist": []
}
]
}
]
mybatis递归就是这么的简单。
说下mybatis一对多实现
对应的bean
public class Dept {
private Integer id;
private String deptName;
private String locAdd;
private List<Emp> emps
@JsonIgnoreProperties("dept")
public class Emp {
private Integer id;
private String name;
private Dept dept;
dao层
public interface DeptMapper {
public Dept getDeptById(Integer id);
}
public interface EmpMapper {
public Emp getEmpByDeptId(Integer deptId);
}
mapper.xml文件
<mapper namespace="com.bscc.mapper.DeptMapper">
<resultMap id="DeptResultMap" type="com.bscc.beans.Dept">
<id property="id" column="id"/>
<result property="deptName" column="deptName"/>
<result property="locAdd" column="locAdd"/>
<!-- private List<Emp> emps; column="id"写被集合对象主键,select按照外键键查询,通过deptid查出emp给dept-->
<collection property="emps" column="id" ofType="Emp" select="com.bscc.mapper.EmpMapper.getEmpByDeptId"/>
</resultMap>
<select id="getDeptById" parameterType="Integer" resultMap="DeptResultMap">
select * from tbl_dept where id=#{id}
</select>
</mapper>
<mapper namespace="com.bscc.mapper.EmpMapper">
<resultMap id="EmpResultMap" type="com.bscc.beans.Emp">
<id property="id" column="id"/>
<result property="name" column="name"/>
</resultMap>
<select id="getEmpByDeptId" parameterType="Integer" resultMap="EmpResultMap">
select * from tbl_emp where deptId=#{deptId}
</select>
</mapper>
对应的controller方法
@RequestMapping("/getDeptById")
@ResponseBody
public Dept getDeptById() {
Dept deptById = deptMapper.getDeptById(1);
return deptById;
}
无非就是比简单查询复杂一些罢了。
代码目录

OK!!!
对应的github地址
https://github.com/chywx/MavenProject6oneToMany
mybatis递归,一对多代码示例的更多相关文章
- mybatis 04: mybatis对象分析 + 测试代码简化 + 配置优化
MyBatis对象分析 测试代码示例 package com.example.test; import com.example.pojo.Student; import org.apache.ibat ...
- Python实现各种排序算法的代码示例总结
Python实现各种排序算法的代码示例总结 作者:Donald Knuth 字体:[增加 减小] 类型:转载 时间:2015-12-11我要评论 这篇文章主要介绍了Python实现各种排序算法的代码示 ...
- Delphi之通过代码示例学习XML解析、StringReplace的用法(异常控制 good)
*Delphi之通过代码示例学习XML解析.StringReplace的用法 这个程序可以用于解析任何合法的XML字符串. 首先是看一下程序的运行效果: 以解析这样一个XML的字符串为例: <? ...
- RSA加密传输代码示例
RSA加密传输代码示例 涉及敏感数据的传输,双方最好约定使用加密解密.那RSA非对称加密就大有作为了.服务端可以保留自己的私钥,发给客户端对应的公钥.这样就可以互相加解密了.php中rsa加解密实现: ...
- mybatis的一对多,多对一,以及多对对的配置和使用
1.本文章是无意中看见易百教程的Mybatis教程才注意到这个问题,平时都仅仅是在用CRUD,忽略了这方面的问题,真实十分羞愧 2.首先我们开始对mybatis的一对多的探究 根据这个应用场景 ...
- 回顾一下MyBatis逆向工程——自动生成代码
前言 最近做的项目(SSM+Shiro)的数据库表已经创建完成,一共有15张表,如果我们一个个去写pojo/bean的代码以及各种sql语句的话未免太过麻烦而且很容易出错,这个时候我们就需要MyBat ...
- (转)MyBatis框架的学习(七)——MyBatis逆向工程自动生成代码
http://blog.csdn.net/yerenyuan_pku/article/details/71909325 什么是逆向工程 MyBatis的一个主要的特点就是需要程序员自己编写sql,那么 ...
- H5+ 分享到微信、朋友圈代码示例
h5+分享到微信.朋友圈代码示例 在使用分享功能的时候会莫名的分享失败,debug时发现是图片过大的问题. 图片过大时ios平台上返回错误码-8,安卓上返回错误码-3(我测试是这样) 因此如果第一次分 ...
- 高级渲染技巧和代码示例 GPU Pro 7
下载代码示例 移动设备正呈现着像素越来越高,屏幕尺寸越来越小的发展趋势. 由于像素着色的能耗非常大,因此 DPI 的增加以及移动设备固有的功耗受限环境为降低像素着色成本带来了巨大的压力. MSAA 有 ...
随机推荐
- 解决CodeBlocks无法自动补全的问题
在Deepin下安装的CB,输入printf.scanf的时候不会自动补全,这样就很难受. 解决办法是在Setting -> Editor -> Syntax highlighting - ...
- 2017北京国庆刷题Day7 morning
期望得分:100+0+100=200 实际得分:100+20+0=120 离散化搞搞 #include<cstdio> #include<iostream> #include& ...
- vijos 1243 生产产品 DP + 单调队列优化
LINK 题意:有1个产品,m个步骤编号为1~m.步骤要在n个机器人的手中生产完成.其中,第i个步骤在第j个机器人手中的生产时间给定为$T[i][j]$,切换机器人消耗cost.步骤必须按顺序,同一个 ...
- elasticsearch创建索引
1.通过elasticsearch-head 创建 (1)登录localhost:9100 (2)点击复合查询 (3)输入内容 (4)勾选易读,点击验证是否是JSON格式 (5)点击提交请求,返回 { ...
- [hadoop]hadoop学习路线
1.主要学习hadoop中的四大框架:hdfs.mapreduce.hive.hbase.这四大框架是hadoop最最核心的,学习难度最大的,也是应用最广泛的. 2.熟悉了解hadoop基本知识及其所 ...
- Docker 配置国内镜像拉取中心,Configure docker to use faster registries in China.
Networking in China is really bad when it comes to using some cloud based tools like docker, it's us ...
- 51Nod 1256 扩展欧几里得求乘法逆元
给出2个数M和N(M < N),且M与N互质,找出一个数K满足0 < K < N且K * M % N = 1,如果有多个满足条件的,输出最小的. Input 输入2个数M, N中间用 ...
- [POJ 2559]Largest Rectangle in a Histogram 题解(单调栈)
[POJ 2559]Largest Rectangle in a Histogram Description A histogram is a polygon composed of a sequen ...
- 旅游(CSUST省赛选拔赛2+状压dp+最短路)
题目链接:http://csustacm.com:4803/problem/1016 题目: 思路:状压dp+最短路,比赛的时候有想到状压dp,但是最短路部分写挫了,然后就卡死了,对不起出题人~dis ...
- favico.js笔记
1. favicon.js是什么 一个js库可以使用徽标.图像.视频等来设置网页的favicon,即网页标题栏上的小图标. 2. 如何使用 2.1 使用徽标 basic demo: <!DOCT ...