MyBatis之自查詢使用遞歸實現(xiàn) N級聯(lián)動效果(兩種實現(xiàn)方式)
A:首先先看下一個簡單的面試題
斐波那契數(shù)列
計算數(shù)組{1,1,2,3,5,8.......} 第30位值
規(guī)律:1 1 從第三項開始,每一項都是前兩項之和
有兩種實現(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){ //遞歸前兩個數(shù) 不管n是多少 為一
return 1;
}
int[] nums = new int[n+1]; //30位從零開始
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中利用遞歸實現(xiàn)n級聯(lián)動

sql語句: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從另一個maper文件中取出數(shù)據(jù)時必須用ofType
<!--查到的cid作為下次的pid-->
</resultMap>
<select id="getCategory" resultMap="getSelf" >
select * from category where pid=#{pid}
</select>
</mapper>
mybatis的javaType和ofType
都是指定對象的類型 不同的是當(dāng)使用反向查詢select從另一個maper文件中取出數(shù)據(jù)時必須用ofType
都可以為collection和association是指定對象的類型,
都不是必須寫的, 只有反向select時需要ofType;
實體類:
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;
}
}
測試類:
//測試自連接
@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='圖書', pid=0, categorySet=[Category{cid=5, cName='期刊報紙', pid=1, categorySet=[]}, Category{cid=3, cName='青年圖書', pid=1, categorySet=[Category{cid=6, cName='讀者', pid=3, categorySet=[Category{cid=7, cName='12月份', pid=6, categorySet=[]}]}]}, Category{cid=4, cName='少兒圖書', pid=1, categorySet=[]}]}
Category{cid=2, cName='服裝', pid=0, categorySet=[]}
以上所述是小編給大家介紹的MyBatis之自查詢使用遞歸實現(xiàn) N級聯(lián)動效果,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Java Validation Api實現(xiàn)原理解析
這篇文章主要介紹了Java Validation Api實現(xiàn)原理解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-09-09
Java JSONObject與JSONArray對象案例詳解
這篇文章主要介紹了Java JSONObject與JSONArray對象案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09

