Lazy Loading of Mybatis Study Notes (7)

Lazy loading concept:

resultMap can implement advanced mapping (using association and collection to implement one-to-one and one-to-many mapping), and association and collection have the function of lazy loading .
Requirement:
If the order is inquired and the user information is related to the query. If we query the order information first, we can meet the requirements, and then we query the user information when we need to query the user information. The on -demand query for user information is lazy loading.

Lazy loading: Query from a single table first, and then from the associated table to associate the query when needed, which greatly improves database performance, because querying a single table is faster than querying multiple tables by association.


Lazy loading with association

1. mapper.xml :

The statement corresponding to the two mapper methods needs to be defined.

(1). Only query order information

select * from orders;

Use association in the query order statement to lazy load the following statement (associated with user information).

UserChildMapper.xml:


(2). Related query user information

  Use the user_id in the order information queried above to associate and query the user information;

  Use findUserById in UserMapper.xml.



Execute findOrdersUserLazyloading first, then execute findUserById when you need to query users, and configure the lazy loading execution through the definition of resultMap.


2. resultMap



3. UserChildMapper.java:



4. Test class:



5. Test results:



Similarly, collection is similar, use select and column to delay loading! !

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324651067&siteId=291194637