MyBatis self-queries and uses recursion to implement N-level linkage
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
The second way: using array
public int TestSelfTwo(int n){ if(n
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); //接口层方法</category>
Mapping file configuration
<mapper><resultmap><id></id><result></result><collection></collection> //这里可以不用指定oftype 使用反向查询select从另一个maper文件中取出数据时必须用ofType<!--查到的cid作为下次的pid--></resultmap><select>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; } }</category></category></category></category>
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); } }</category>
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Dreamweaver Mac version
Visual web development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.