MyBatis之自查詢,使用 遞歸實現 N級聯動


A:首先先看下一個簡單的面試題

斐波那契數列

計算數組{1,1,2,3,5,8.......} 第30位值


規律:1 1 從第三項開始,每一項都是前兩項之和

    有兩種實現方式

 第一種方式:

public class TestOne {

   public int TestSelf(int n){

       if(n<0){
           throw  new IllegalArgumentException("n不能為負數");
       }else if(n<=2){
           return 1;
       }else{
           return TestSelf(n-2)+TestSelf(n-1);
       }
   }
   @Test
   public void  Test(){
       System.out.println(TestSelf(30));
   }


}

打印結果832040

 

第二種方式:利用數組

 public int TestSelfTwo(int n){
       if(n<0){
           throw  new IllegalArgumentException("n不能為負數");
       }else if(n<=1){    //遞歸前兩個數  不管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中利用遞歸實現n級聯動

   

 

sql語句:select * from type where pid  = 0;      首次指定pid值為0,然后下次根據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文件中取出數據時必須用ofType
            <!--查到的cid作為下次的pid-->
    </resultMap>

    <select id="getCategory" resultMap="getSelf" >
        select * from category where pid=#{pid}
    </select>

</mapper>

mybatis的javaType和ofType

 

都是指定對象的類型 不同的是當使用反向查詢select從另一個maper文件中取出數據時必須用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);
        }
    }

 

打印結果:

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=[]}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM