Home  >  Article  >  Java  >  MyBatis self-queries and uses recursion to implement N-level linkage

MyBatis self-queries and uses recursion to implement N-level linkage

巴扎黑
巴扎黑Original
2017-07-21 14:38:592005browse

A: First, let’s look at a simple interview question

Fibonacci Sequence

Calculate the array {1,1,2,3,5,8.... ..} The 30th digit value


Rule: 1 1 Starting from the third item, each item is the sum of the previous two items

There are two ways to implement it

The first way:

   TestSelf((n<0   IllegalArgumentException("n不能为负数" (n<=2 1 TestSelf(n-2)+TestSelf(n-1 30

The second way: using array

 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));
   }

Formula: f(n) = f(n-2)+f(n-1) f represents the number of bits represented by method n

B: Use recursion to implement n-level linkage in MyBatis

sql statement: select * from type where pid = 0; Specify the pid value as 0 for the first time, and then use the cid with pid as 0 as the next query pid

        public List<Category> getCategory(Integer pid);   //接口层方法

Mapping file configuration

<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’s javaType and ofType

are all types of specified objects. The difference is that when using reverse query select to retrieve data from another maper file, you must use ofType

. The type of object can be specified for collection and association.

is not required to be written, only ofType is required for reverse selection;

Entity class:

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>();
    @Overridepublic 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 class:

 //测试自连接    @Testpublic void  TestSelf(){
        CateGoryDao dao = MyBatis.getSessionTwo().getMapper(CateGoryDao.class);
        List<Category> list = dao.getCategory(0);for (Category item:list ) {
            System.out.println(item);
        }
    }

Print result:

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

The above is the detailed content of MyBatis self-queries and uses recursion to implement N-level linkage. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn