Analysis of delayed loading case in mybatis

1、 Delayed loading

Resultmap can realize advanced mapping (one-to-one and one to many mapping using association and collection), and association and collection have the function of delayed loading.

Delayed loading: query from a single table first, and then associate the query from the associated table when necessary, which greatly improves the database performance, because querying a single table is faster than associating multiple tables.

Configure in the mybatis core configuration file:

lazyLoadingEnabled、aggressiveLazyLoading

Occasion:

When only some records need to be associated to query other information, the loading can be delayed as needed. When associated query is needed, SQL can be sent to the database to improve the performance of the database.

When all the associated query information is required, you can directly return all the associated query information without delaying loading. You can use resulttype or resultmap to complete the mapping.

2: Case: (one to many in departments and employees)

Source code introduction:

1.Dept. java

2.Emp. java

3.MybatisUtil. java

4.DeptDao. java

5.DeptDAO. xml

6.mybatis-config. XML (deferred loading configuration here)

7.MyTest. Java (test class)

Test results:

Break point below

Case 1: in mybatis config No configuration in XML

Case 2: in mybatis config Configuration in XML

next step:

F6 next:

F6 next: type the employee's name

Case 3:

F6 next:

F6 next: print out the employee name

The above is the delayed loading in mybatis introduced by Xiaobian. I hope it will be helpful to you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>