Mybatis BASICS (7) ---- delayed loading, mybatis ----

Source: Internet
Author: User

Mybatis BASICS (7) ---- delayed loading, mybatis ----
1. What is delayed loading?

ResultMap can implement advanced ing (one-to-one and one-to-many ing using association and collection). association and collection have the delayed loading function.

Requirements:If you query an order and associate it with the user information. If you want to query the order information first, you can query the user information. Querying user information on demand is a delayed loading.

Delayed Loading: Queries from a single table and associated tables are performed when necessary, which greatly improves database performance because querying a single table is faster than querying multiple associated tables.

Ii. Use association to implement delayed Loading

You need to define the statement corresponding to the two ER er methods.

1. Only query order information

SELECT * FROM orders

Use association in the statement of the query order to delay loading (execution) The satatement (associated query user information)

2. query user information by Association

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

Core code for delayed loading of OrderMapper. xml:

Use select in association to specify the id of the statement for delayed loading to be executed.

<! -- Query the resultMap definition of order association query users whose user information is loaded on demand in a delayed manner --> <resultMap type = "com. mybatis. entity. Orders" id = "ordersUserLazyLoading"> <! -- Ing the order information --> <id column = "id" property = "id"/> <result column = "user_id" property = "userid"/> <result column = "number" property = "number"/> <result column = "createtime" property = "createTime"/> <result column = "note" property = "note"/> <! -- Latency loading of User Information select: Specifies the id of the statement to be executed for delayed loading (the statement for querying user information based on user_id) column: the column associated with user information in order information is the SQL statement used for user_id association query. It is understood as: SELECT orders. *, (SELECT username from user where orders. user_id = user. id) username, (SELECT sex from user where orders. user_id = user. id) sex FROM orders --> <association property = "user" javaType = "com. mybatis. entity. user "select =" findUserById "column =" user_id "/> </resultMap> <! -- Query users by Id for test of delayed loading --> <select id = "findUserById" parameterType = "int" resultType = "com. mybatis. entity. user "> select * from t_user where id =#{ id} </select> <! -- Query the user associated with the order and delay loading of user information --> <select id = "findOrdersUserLazyLoading" resultMap = "ordersUserLazyLoading"> select * from orders </select>

OrderMapper. java code:

Public interface OrdersCustomMapper {/** query the order and associate it with the query user. The user delays loading */public List <Orders> findOrdersUserLazyLoading () on demand (); /** query users by Id (this method should be put in the UserMapper class for testing convenience) */public User findUserById (int id );}

Test ideas and code:

1. Execute the above mapper method (findOrdersUserLazyLoading) and call findOrdersUserLazyLoading in OrdersMapperCustom internally to query only orders information (single table ).

2. traverse the List <Orders> queried in the previous step in the program. When we call the getUser method in Orders, we start to perform delayed loading.

3. Delayed loading: Call findUserbyId to obtain user information.

// Query the user association to query the user, and the user information is delayed loading @ Test public void TestFindOrdersUserLazyLoading () {SqlSession sqlSession = sqlSessionFactory. openSession (); // create the proxy object OrdersCustomMapper oc = sqlSession. getMapper (OrdersCustomMapper. class); // call the mapper method List <Orders> list = oc. findOrdersUserLazyLoading (); for (Orders order: list) {// execute getUser () to query User information. In this case, the user User = order is delayed. getUser (); System. out. println (user);} sqlSession. close ();}
Iii. Delayed loading configuration in sqlMapConfig. xml, mybatis core configuration file

By default, delayed loading is not enabled for mybatis. You must configure setting in SqlMapConfig. xml.

Configure in the mybatis core configuration file:

LazyLoadingEnabled, aggressiveLazyLoading

Settings

Description

Allowed value

Default Value

LazyLoadingEnabled

Global settings are lazy. If it is set to 'false', all associated objects will be initialized and loaded.

True | false

False

AggressiveLazyLoading

When it is set to 'true', the lazy objects may be loaded by all lazy attributes. Otherwise, each attribute is loaded as needed.

True | false

True

<! -- Global parameter configuration --> <settings> <! -- Enable delayed loading --> <setting name = "lazyLoadingEnabled" value = "true"/> <! -- Change active loading to passive loading and On-Demand Loading --> <setting name = "aggressiveLazyLoading" value = "false"/> </settings>

Summary: By using the delayed loading method, you can first query simple SQL statements (preferably single tables or associated queries), and then load other information of associated queries as needed.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.