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<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!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The recursion depth of C++ functions is limited, and exceeding this limit will result in a stack overflow error. The limit value varies between systems and compilers, but is usually between 1,000 and 10,000. Solutions include: 1. Tail recursion optimization; 2. Tail call; 3. Iterative implementation.

Yes, C++ Lambda expressions can support recursion by using std::function: Use std::function to capture a reference to a Lambda expression. With a captured reference, a Lambda expression can call itself recursively.

How to implement dual WeChat login on Huawei mobile phones? With the rise of social media, WeChat has become one of the indispensable communication tools in people's daily lives. However, many people may encounter a problem: logging into multiple WeChat accounts at the same time on the same mobile phone. For Huawei mobile phone users, it is not difficult to achieve dual WeChat login. This article will introduce how to achieve dual WeChat login on Huawei mobile phones. First of all, the EMUI system that comes with Huawei mobile phones provides a very convenient function - dual application opening. Through the application dual opening function, users can simultaneously

How to implement the WeChat clone function on Huawei mobile phones With the popularity of social software and people's increasing emphasis on privacy and security, the WeChat clone function has gradually become the focus of people's attention. The WeChat clone function can help users log in to multiple WeChat accounts on the same mobile phone at the same time, making it easier to manage and use. It is not difficult to implement the WeChat clone function on Huawei mobile phones. You only need to follow the following steps. Step 1: Make sure that the mobile phone system version and WeChat version meet the requirements. First, make sure that your Huawei mobile phone system version has been updated to the latest version, as well as the WeChat App.

The recursive algorithm solves structured problems through function self-calling. The advantage is that it is simple and easy to understand, but the disadvantage is that it is less efficient and may cause stack overflow. The non-recursive algorithm avoids recursion by explicitly managing the stack data structure. The advantage is that it is more efficient and avoids the stack. Overflow, the disadvantage is that the code may be more complex. The choice of recursive or non-recursive depends on the problem and the specific constraints of the implementation.

The programming language PHP is a powerful tool for web development, capable of supporting a variety of different programming logics and algorithms. Among them, implementing the Fibonacci sequence is a common and classic programming problem. In this article, we will introduce how to use the PHP programming language to implement the Fibonacci sequence, and attach specific code examples. The Fibonacci sequence is a mathematical sequence defined as follows: the first and second elements of the sequence are 1, and starting from the third element, the value of each element is equal to the sum of the previous two elements. The first few elements of the sequence

A recursive function is a technique that calls itself repeatedly to solve a problem in string processing. It requires a termination condition to prevent infinite recursion. Recursion is widely used in operations such as string reversal and palindrome checking.

In today's software development field, Golang (Go language), as an efficient, concise and highly concurrency programming language, is increasingly favored by developers. Its rich standard library and efficient concurrency features make it a high-profile choice in the field of game development. This article will explore how to use Golang for game development and demonstrate its powerful possibilities through specific code examples. 1. Golang’s advantages in game development. As a statically typed language, Golang is used in building large-scale game systems.
