• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

mybatis递归 一对多的实现方法示例

java 搞代码 4年前 (2022-01-05) 128次浏览 已收录 0个评论

这篇文章主要给大家介绍了关于mybatis递归 一对多实现的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

前言

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

由于只有这么两级,可以不用使用递归,直接查询父集,之后foreach查询子集放入对应的list集合。

虽然实现了,感觉毕竟,太low。

有同事跟我说可以使用mybatis的递归实现,就学习了下。

对应的bean里面需要有对应的list 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,'111',NULL,NULL,NULL),(2,'spring',1,'222',NULL,NULL,NULL),(3,'springmvc',1,'333',NULL,NULL,NULL),(4,'struts',1,'444',NULL,NULL,NULL),(5,'jdbc',0,'555',NULL,NULL,NULL),(6,'hibernate',5,'666',NULL,NULL,NULL),(7,'mybatis',5,'777',NULL,NULL,NULL),(8,'jdbctemplate',5,'888',NULL,NULL,NULL),(9,'beanfactory',3,'999',NULL,NULL,NULL),(10,'factorybean',3,'000',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 catelist; get 。。。 set。。。 tostring。。。

dao层

 public interface GoodsMapper { List getCategory(Integer pid); }

mapper.xml

  <!--查到的cid作为下次的pid --> select * from goods_category where parentid=#{pid} ORDER BY displayorder,goodscateid 

之后直接访问对应的方法,即可查询出来

 @RequestMapping("/getGoodsList") @ResponseBody public List getGoodsList(){ // pid指定为0 List list = goodsMapper.getCategory(0); return list; }

结果,可以使用 ,也可以使用这个工具

 [ { "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",<strong style="color:transparent">来源gao@daima#com搞(%代@#码@网</strong> "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 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文件

   <!-- private List emps; column="id"写被集合对象主键,select按照外键键查询,通过deptid查出emp给dept--><sel

以上就是mybatis递归 一对多的实现方法示例的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:mybatis递归 一对多的实现方法示例

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址