mybatis遞歸 一對(duì)多的實(shí)現(xiàn)方法示例
前言
今天需要做一個(gè)功能,根據(jù)專業(yè),有不同的章節(jié),章節(jié)下面有對(duì)應(yīng)的習(xí)題,
由于只有這么兩級(jí),可以不用使用遞歸,直接查詢父集,之后foreach查詢子集放入對(duì)應(yīng)的list集合。
雖然實(shí)現(xiàn)了,感覺(jué)畢竟,太low。
有同事跟我說(shuō)可以使用mybatis的遞歸實(shí)現(xiàn),就學(xué)習(xí)了下。
對(duì)應(yīng)的bean里面需要有對(duì)應(yīng)的list<bean> lists的引用。
直接上代碼
對(duì)應(yīng)的sql語(yǔ)句
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ù)雜一些罷了。
代碼目錄

OK?。?!
對(duì)應(yīng)的github地址
https://github.com/chywx/MavenProject6oneToMany (本地下載)
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
- Mybatis 一對(duì)多和多對(duì)一關(guān)聯(lián)查詢問(wèn)題
- mybatis 一對(duì)一、一對(duì)多和多對(duì)多查詢實(shí)例代碼
- 通過(guò)Mybatis實(shí)現(xiàn)單表內(nèi)一對(duì)多的數(shù)據(jù)展示示例代碼
- mybatis高級(jí)映射一對(duì)多查詢實(shí)現(xiàn)代碼
- Mybatis 中的一對(duì)一,一對(duì)多,多對(duì)多的配置原則示例代碼
- mybatis一對(duì)多查詢功能
- MyBatis存儲(chǔ)過(guò)程、MyBatis分頁(yè)、MyBatis一對(duì)多增刪改查操作
- Mybatis中的高級(jí)映射一對(duì)一、一對(duì)多、多對(duì)多
- mybatis關(guān)系映射之一對(duì)多和多對(duì)一
相關(guān)文章
解決SpringBoot使用devtools導(dǎo)致的類型轉(zhuǎn)換異常問(wèn)題
這篇文章主要介紹了解決SpringBoot使用devtools導(dǎo)致的類型轉(zhuǎn)換異常問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。 一起跟隨小編過(guò)來(lái)看看吧2020-08-08
spring boot整合mybatis+mybatis-plus的示例代碼
這篇文章主要介紹了spring boot整合mybatis+mybatis-plus的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
Java兩整數(shù)相除向上取整的方式詳解(Math.ceil())
在調(diào)外部接口獲取列表數(shù)據(jù)時(shí),需要判斷是否已經(jīng)取完了所有的值,因此需要用到向上取整,下面這篇文章主要給大家介紹了關(guān)于Java兩整數(shù)相除向上取整的相關(guān)資料,需要的朋友可以參考下2022-06-06
SpringBoot 動(dòng)態(tài)定時(shí)器的使用方法
這篇文章主要介紹了SpringBoot 動(dòng)態(tài)定時(shí)器的使用方法,非常不錯(cuò),具有一定的參考借鑒借鑒價(jià)值,需要的朋友可以參考下2018-05-05
Java:不支持發(fā)行版本5的超詳細(xì)簡(jiǎn)單解決方案
發(fā)行版本5是Java5,已經(jīng)是十多年前的版本了,現(xiàn)在已經(jīng)不再被支持,如果您使用的是舊版的Java開(kāi)發(fā)工具,可能會(huì)出現(xiàn)這樣的錯(cuò)誤,這篇文章主要給大家介紹了關(guān)于Java:不支持發(fā)行版本5的超詳細(xì)簡(jiǎn)單解決方案,需要的朋友可以參考下2024-01-01
Spring Security OAuth2 實(shí)現(xiàn)登錄互踢的示例代碼
這篇文章主要介紹了Spring Security OAuth2實(shí)現(xiàn)登錄互踢的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
Eclipse設(shè)置斷點(diǎn)調(diào)試的方法
這篇文章主要介紹了Eclipse斷點(diǎn)調(diào)試的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09

