MyBatis之自查詢使用遞歸實(shí)現(xiàn) N級(jí)聯(lián)動(dòng)效果(兩種實(shí)現(xiàn)方式)
A:首先先看下一個(gè)簡(jiǎn)單的面試題
斐波那契數(shù)列
計(jì)算數(shù)組{1,1,2,3,5,8.......} 第30位值
規(guī)律:1 1 從第三項(xiàng)開(kāi)始,每一項(xiàng)都是前兩項(xiàng)之和
有兩種實(shí)現(xiàn)方式
第一種方式:
public class TestOne {
public int TestSelf(int n){
if(n<0){
throw new IllegalArgumentException("n不能為負(fù)數(shù)");
}else if(n<=2){
return 1;
}else{
return TestSelf(n-2)+TestSelf(n-1);
}
}
@Test
public void Test(){
System.out.println(TestSelf(30));
}
}
打印結(jié)果832040
第二種方式:利用數(shù)組
public int TestSelfTwo(int n){
if(n<0){
throw new IllegalArgumentException("n不能為負(fù)數(shù)");
}else if(n<=1){ //遞歸前兩個(gè)數(shù) 不管n是多少 為一
return 1;
}
int[] nums = new int[n+1]; //30位從零開(kāi)始
nums[0]=1;
nums[1]=1;
for (int i =2;i<n;i++){
nums[i] = nums[i-2]+nums[i-1];
}
return nums[n-1];
}
@Test
public void Test(){
System.out.println(TestSelfTwo(30));
}
公式:f(n) = f(n-2)+f(n-1) f代表方法 n代表多少 位
B:在MyBatis中利用遞歸實(shí)現(xiàn)n級(jí)聯(lián)動(dòng)

sql語(yǔ)句:select * from type where pid = 0; 首次指定pid值為0,然后下次根據(jù)pid為0的cid 作為下次查詢的pid public List<Category> getCategory(Integer pid); //接口層方法
映射文件配置
<mapper namespace="dao.CateGoryDao">
<resultMap id="getSelf" type="entity.Category">
<id column="cid" property="cid"></id>
<result column="cname" property="cName"></result>
<collection property="categorySet" select="getCategory" column="cid"></collection> //這里可以不用指定oftype 使用反向查詢select從另一個(gè)maper文件中取出數(shù)據(jù)時(shí)必須用ofType
<!--查到的cid作為下次的pid-->
</resultMap>
<select id="getCategory" resultMap="getSelf" >
select * from category where pid=#{pid}
</select>
</mapper>
mybatis的javaType和ofType
都是指定對(duì)象的類型 不同的是當(dāng)使用反向查詢select從另一個(gè)maper文件中取出數(shù)據(jù)時(shí)必須用ofType
都可以為collection和association是指定對(duì)象的類型,
都不是必須寫(xiě)的, 只有反向select時(shí)需要ofType;
實(shí)體類:
package entity;
import java.util.HashSet;
import java.util.Set;
/**
* Created by zhangyu on 2017/7/12.
*/
public class Category {
private Integer cid;
private String cName;
private Integer pid;
private Set<Category> categorySet = new HashSet<Category>();
@Override
public String toString() {
return "Category{" +
"cid=" + cid +
", cName='" + cName + '\'' +
", pid=" + pid +
", categorySet=" + categorySet +
'}';
}
public Integer getCid() {
return cid;
}
public void setCid(Integer cid) {
this.cid = cid;
}
public String getcName() {
return cName;
}
public void setcName(String cName) {
this.cName = cName;
}
public Integer getPid() {
return pid;
}
public void setPid(Integer pid) {
this.pid = pid;
}
public Set<Category> getCategorySet() {
return categorySet;
}
public void setCategorySet(Set<Category> categorySet) {
this.categorySet = categorySet;
}
}
測(cè)試類:
//測(cè)試自連接
@Test
public void TestSelf(){
CateGoryDao dao = MyBatis.getSessionTwo().getMapper(CateGoryDao.class);
List<Category> list = dao.getCategory(0);
for (Category item:list ) {
System.out.println(item);
}
}
打印結(jié)果:
Category{cid=1, cName='圖書(shū)', pid=0, categorySet=[Category{cid=5, cName='期刊報(bào)紙', pid=1, categorySet=[]}, Category{cid=3, cName='青年圖書(shū)', pid=1, categorySet=[Category{cid=6, cName='讀者', pid=3, categorySet=[Category{cid=7, cName='12月份', pid=6, categorySet=[]}]}]}, Category{cid=4, cName='少兒圖書(shū)', pid=1, categorySet=[]}]}
Category{cid=2, cName='服裝', pid=0, categorySet=[]}
以上所述是小編給大家介紹的MyBatis之自查詢使用遞歸實(shí)現(xiàn) N級(jí)聯(lián)動(dòng)效果,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- MyBatis實(shí)現(xiàn)模糊查詢的幾種方式
- Mybatis多表關(guān)聯(lián)查詢的實(shí)現(xiàn)(DEMO)
- mybatis 模糊查詢的實(shí)現(xiàn)方法
- Mybatis 一對(duì)多和多對(duì)一關(guān)聯(lián)查詢問(wèn)題
- mybatis 實(shí)現(xiàn) SQL 查詢攔截修改詳解
- Mybatis查詢記錄條數(shù)的實(shí)例代碼
- MyBatis實(shí)現(xiàn)動(dòng)態(tài)查詢、模糊查詢功能
- Mybatis查詢語(yǔ)句結(jié)果集的總結(jié)大全
- mybatis分頁(yè)及模糊查詢功能實(shí)現(xiàn)
- MyBatis查詢時(shí)屬性名和字段名不一致問(wèn)題的解決方法
相關(guān)文章
SpringBoot 多環(huán)境配置和啟動(dòng)詳解
這篇文章主要為大家介紹了SpringBoot多環(huán)境配置和啟動(dòng)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10
Java Validation Api實(shí)現(xiàn)原理解析
這篇文章主要介紹了Java Validation Api實(shí)現(xiàn)原理解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
Java JSONObject與JSONArray對(duì)象案例詳解
這篇文章主要介紹了Java JSONObject與JSONArray對(duì)象案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09

