今天需要做一个功能,根据专业,有不同的章节,章节下面有对应的习题,

由于只有这么两级,可以不用使用递归,直接查询父集,之后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递归,一对多代码示例的更多相关文章

  1. mybatis 04: mybatis对象分析 + 测试代码简化 + 配置优化

    MyBatis对象分析 测试代码示例 package com.example.test; import com.example.pojo.Student; import org.apache.ibat ...

  2. Python实现各种排序算法的代码示例总结

    Python实现各种排序算法的代码示例总结 作者:Donald Knuth 字体:[增加 减小] 类型:转载 时间:2015-12-11我要评论 这篇文章主要介绍了Python实现各种排序算法的代码示 ...

  3. Delphi之通过代码示例学习XML解析、StringReplace的用法(异常控制 good)

    *Delphi之通过代码示例学习XML解析.StringReplace的用法 这个程序可以用于解析任何合法的XML字符串. 首先是看一下程序的运行效果: 以解析这样一个XML的字符串为例: <? ...

  4. RSA加密传输代码示例

    RSA加密传输代码示例 涉及敏感数据的传输,双方最好约定使用加密解密.那RSA非对称加密就大有作为了.服务端可以保留自己的私钥,发给客户端对应的公钥.这样就可以互相加解密了.php中rsa加解密实现: ...

  5. mybatis的一对多,多对一,以及多对对的配置和使用

    1.本文章是无意中看见易百教程的Mybatis教程才注意到这个问题,平时都仅仅是在用CRUD,忽略了这方面的问题,真实十分羞愧   2.首先我们开始对mybatis的一对多的探究   根据这个应用场景 ...

  6. 回顾一下MyBatis逆向工程——自动生成代码

    前言 最近做的项目(SSM+Shiro)的数据库表已经创建完成,一共有15张表,如果我们一个个去写pojo/bean的代码以及各种sql语句的话未免太过麻烦而且很容易出错,这个时候我们就需要MyBat ...

  7. (转)MyBatis框架的学习(七)——MyBatis逆向工程自动生成代码

    http://blog.csdn.net/yerenyuan_pku/article/details/71909325 什么是逆向工程 MyBatis的一个主要的特点就是需要程序员自己编写sql,那么 ...

  8. H5+ 分享到微信、朋友圈代码示例

    h5+分享到微信.朋友圈代码示例 在使用分享功能的时候会莫名的分享失败,debug时发现是图片过大的问题. 图片过大时ios平台上返回错误码-8,安卓上返回错误码-3(我测试是这样) 因此如果第一次分 ...

  9. 高级渲染技巧和代码示例 GPU Pro 7

    下载代码示例 移动设备正呈现着像素越来越高,屏幕尺寸越来越小的发展趋势. 由于像素着色的能耗非常大,因此 DPI 的增加以及移动设备固有的功耗受限环境为降低像素着色成本带来了巨大的压力. MSAA 有 ...

随机推荐

  1. nginx 初探 之反向代理

    首先要解释的是什么叫做反向代理? 平时我们浏览网页可以输入网址直接访问,  但如果访问国外的网站,  可能就没那么简单('中国特色'),  这时候我们需要配置一个代理服务器, 然后通过此服务器中转来访 ...

  2. Atcoder #017 agc017 B.Moderate Differences 思维

    LINK 题意:给出最左和最右两个数,要求往中间填n-2个数,使得相邻数间差的绝对值$∈[L,R]$ 思路:其实也是个水题,比赛中大脑宕机似的居然想要模拟构造一个数列,其实我们只要考虑作为结果的数,其 ...

  3. 遍历hashmap

    转]Java中HashMap遍历的两种方式原文地址: http://www.javaweb.cc/language/java/032291.shtml 第一种: Map map = new HashM ...

  4. DevExpress使用教程:GridView经验小结(官方中文文献经典资料技巧)

    下面是笔者自己总结的使用 DevExpress Gridview 的一些经验小结,分享给大家: 1.去除 GridView 头上的 "Drag a column header here to ...

  5. CSS中filter滤镜的学习笔记

    1.CSS静态滤镜样式 (filter)(只有IE4.0以上支持)  CSS静态滤镜样式的使用方法:{ filter : filtername( parameters1, parameters2, . ...

  6. ASP.Net Cache(缓存)—ASP.NET细枝末节(2)

    概述 1.意义 把数据放到Cache中,在指定的时间内,可以直接从Cache中获取,避免对数据库等的压力. 2.做法 设置: HttpRuntime.Cache.Insert(CacheKey, ob ...

  7. Android中注册获取验证码倒计时按钮

    public class CountDownTimerUtils extends CountDownTimer { private TextView mTextView; /** * @param t ...

  8. django框架<二>

    django框架:   Models 1.基本创建 Django提供了一个抽象层("Model")的构建和管理Web应用程序的数据. Django使用一种新的方式,即:关系对象映射 ...

  9. python3-可变和不可变数据类型

    可变:[ ]    { } 不可变:int    str   ( )     应用实例: 把列表l,追加到列表s中,现在网列表l中追加一个5,打印列表s可以看到,列表s中的列表l中也有5. d={&q ...

  10. java-String中的 intern()

    1. 首先String不属于8种基本数据类型,String是一个对象. 因为对象的默认值是null,所以String的默认值也是null:但它又是一种特殊的对象,有其它对象没有的一些特性. 2. ne ...