mybatis递归 一对多的实现方法示例
程序员文章站
2024-02-20 13:30:16
前言
今天需要做一个功能,根据专业,有不同的章节,章节下面有对应的习题,
由于只有这么两级,可以不用使用递归,直接查询父集,之后foreach查询子集放入对应的list...
前言
今天需要做一个功能,根据专业,有不同的章节,章节下面有对应的习题,
由于只有这么两级,可以不用使用递归,直接查询父集,之后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,'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<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; }
结果,可以使用 ,也可以使用
[ { "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 (本地下载)
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对的支持。