您好,登錄后才能下訂單哦!
這篇文章給大家介紹使用mybatis遞歸怎么實(shí)現(xiàn)一對(duì)多,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
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);
實(shí)體類
@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>
之后直接訪問(wèn)對(duì)應(yīng)的方法,即可查詢出來(lái)
@RequestMapping("/getGoodsList") @ResponseBody public List<GoodsCategoryVo> getGoodsList(){ // pid指定為0 List<GoodsCategoryVo> list = goodsMapper.getCategory(0); return list; }
結(jié)果,可以使用json在線工具 ,也可以使用這個(gè)工具
[ { "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遞歸就是這么的簡(jiǎn)單。
說(shuō)下mybatis一對(duì)多實(shí)現(xiàn)
對(duì)應(yīng)的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"寫被集合對(duì)象主鍵,select按照外鍵鍵查詢,通過(guò)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>
對(duì)應(yīng)的controller方法
@RequestMapping("/getDeptById") @ResponseBody public Dept getDeptById() { Dept deptById = deptMapper.getDeptById(1); return deptById; }
無(wú)非就是比簡(jiǎn)單查詢復(fù)雜一些罷了。
代碼目錄
關(guān)于使用mybatis遞歸怎么實(shí)現(xiàn)一對(duì)多就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。