I mean, I haven't written my MyBatis blog for more than three months. I 've been busy all the time. It's really a sin! O (Clerk □clerk) o
After reading the article "two forms of MyBatis association", xunke515 posted a message saying that the official document does not recommend the use of nested select statements. We will discuss this topic today.
This nested select statement is not recommended in official documents because it causes the so-called "N + 1 problem ". This problem occurs in both association and collection elements. This article takes a more typical collection element as an example. In the example scenario used in this series, when you need to query the information of the instructor and the students it directs, one instructor can direct multiple students), we will do this: first use an SQL statement "N + 1 problem" 1) to query the instructor information, that is
Select * from teacher
At this time, multiple records can be queried as N) Instructor records. To further query the information of instructors, an SQL statement is generated for each instructor record, that is
Select * from student where supervisor_id =?
"?" In the preceding SQL statement Represents the id of each instructor. Obviously, such a statement generates N in N + 1 problems ). In this way, N + 1 SQL statement is executed in total, that is, N + 1 database query. Database query is usually the bottleneck of application performance. Generally, we should minimize the number of database queries, which will greatly reduce the system performance.
Two methods can be used to solve this problem. The first method is to use an SQL statement to query the information of the instructors and their students. In the articles "two forms of MyBatis collection" and "two forms of MyBatis association", nested resultMap uses this method.
The second method is to use the delayed Loading Mechanism of MyBatis to download the example source code.) The steps are as follows:
1. MyBatis delay loading mechanism needs to use cglib package, so you should add this package to the project, this article uses cglib-nodep-2.2.3.jar.
2. Add the following configuration to the settings element of the MyBatis core configuration file in configuration. xml in this example:
<settingname="lazyLoadingEnabled"value="true"/>
<settingname="aggressiveLazyLoading"value="false"/>
Now we will test the delayed loading. First, transform the class CollectionDemo. java, let it traverse the found instructors first, and then traverse the information of the Students guided by a certain instructor. This code is modified from the previous blog in this series, "Map mode example of MyBatis multi-parameter transfer". for more information, see this article.) The related code is as follows:
// Query the instructor information by PAGE
List <Teacher> teachers = mapper. findTeacherByPage (params );
If (teachers = null)
{
System. out. println ("related instructor information not found. ");
}
Else
{
Object [] t = teachers. toArray ();
System. out. println ("************************************* *********");
For (int I = 0; I <t. length; I ++)
{
Teacher = (Teacher) t [I];
System. out. println ("instructor name:" + "" + teacher. getName ());
System. out. println ("instructor title:" + "" + teacher. getTitle ());
System. out. println ("************************************* *********");
}
// Traverse the students guided by the current teacher object
For (Student s: teacher. getSupStudents ())
{
System. out. println (s. getName () + "" + s. getGender () + "" + s. getGrade ()
+ "" + S. getMajor ());
}
}
To observe program execution details more clearly, modify the Log4j configuration file. properties, related configuration snippets for the usage of MyBatis Log4j, refer to the blog in this series: Log4j example of MyBatis log ):
# Global log Configuration
Log4j. rootLogger = DEBUG, stdout
Log4j.logger.com. abc. mapper = DEBUG
# For usage of MyBatis Log4j, refer to the author's blog:
# Http://legend2011.blog.51cto.com/3018495/999944
# Log4j.logger.com. abc. mapper. TeacherMapper = DEBUG
Log4j.logger.com. abc. mapper. TeacherMapper. findTeacherByPage = TRACE
# Because the SQL statement getStudents is used for the student information under the query guidance, the following configuration is used:
# Print out the execution details of student queries
Log4j.logger.com. abc. mapper. StudentMapper. getStudents = TRACE
In the command line window, execute the ant run command. This series of blog posts use ant to organize projects. If you are not familiar with ant, refer to the blog in this series: download and install ant, ant Entry Example, and other blog posts in this series). The running results are as follows:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/114Z2CC-0.png "border =" 0 "/>
From the first red box, we can see that, before printing the instructor information, we only searched for the instructor information, and did not load the information of the instructors until we had to traverse the students guided by a certain instructor, to find information about the student, as shown in the second red box.
Let's take a look at the effect of loading without delay. You only need to comment out the setting element named "lazyLoadingEnabled" in the core configuration file configuration. xml of MyBatis to disable the delayed loading function. The running result is as follows:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/114Z223Y-1.png "border =" 0 "/>
The two red boxes show the information of the students whose guidance is also loaded when querying the instructor information. In particular, the student information instructed by the instructor Chen Yi has not been accessed in the program and the student information printed at the end is directed by the instructor Ma Jun), but it is still loaded immediately.
MyBatis Study Notes series I: download and install ant
Prepare for MyBatis Study Notes Series II: ant getting started
MyBatis learning notes: MyBatis getting started
MyBatis Study Notes Series II: Example of adding, deleting, and modifying MyBatis
MyBatis Study Notes Series 3: association examples of MyBatis
MyBatis Study Notes Series 4: two forms of MyBatis association
MyBatis Study Notes Series 5: examples of integration between MyBatis and Spring
MyBatis Study Notes Series 6: examples of integration between MyBatis and Spring
MyBatis study notes 7: MyBatis one-to-multiple bidirectional Association
MyBatis Study Notes: MyBatis MapperScannerConfigurer Configuration
MyBatis Study Notes: two forms of MyBatis collection
MyBatis Study Notes Series 10: Log4j example of MyBatis logs
MyBatis Study Notes: example of how to annotate MyBatis with multiple parameters
MyBatis learning notes: Example of the default naming method for MyBatis multi-parameter transfer
MyBatis Study Notes: Example of Map mode for MyBatis multi-parameter transfer
MyBatis Study Notes: N + 1 in MyBatis
MyBatis Study Notes: a hybrid transfer of multiple parameters in MyBatis
MyBatis Study Notes:Spring declarative Transaction Management example
MyBatis Study Notes: Example of MyBatis multiple-to-multiple storage
This article is from the "Xiao fan's column" blog, please be sure to keep this source http://legend2011.blog.51cto.com/3018495/1131629