Mybatis delays loading and mybatis delays

Source: Internet
Author: User

Mybatis delays loading and mybatis delays

If you have such a requirement, You need to query all the orders and obtain detailed information about the orders.

If all the required data is requested at one time, the overhead on the server and database will be high. Therefore, you can load the order information first, and request the details data when the order details are used.

Latency loading of mybatis is required.

  • Enable delayed Loading
    Add settings in the mybaits configuration file
<! -- Delayed loading --> <setting name = "lazyLoadingEnabled" value = "true"/> <setting name = "aggressiveLazyLoading" value = "false"/>
  • Configure resultMap
    The original method is to connect two tables in SQL, and then add cascade attributes with collection tags in resultMap. Now you can divide the SQL statement into two parts. The first part only queries the order table, and the second part queries the Order details through the order id.
<select id="findOrderUser" resultMap="orderUserResultMap">   SELECT * FROM orders</select>
<select id="findDetailByOrdreId" resultType="cn.elinzhou.mybatisTest.pojo.OrderDetailCustom">    SELECT * FROM orderdetail WHERE orders_id = #{_parameter}</select>

Then configure orderUserResultMap. in the traditional way, configure cascade attributes in the collection of orderUserResultMap, for example

<! -- Order details list --> <collection property = "orderDetails" ofType = "cn. elinzhou. mybatisTest. pojo. orderDetailCustom "> <id column =" orderdetail_id "property =" id "/> <result column =" orderdetail_orders_id "property =" orders_id "/> <result column =" orderdetail_items_id "property = ""items_id"/> <result column = "orderdetail_items_num" property = "items_num"/> </collection>

If all such data is queried at a time, you can call the previously defined findDetailByOrdreId and load the Order details data by delay.
Change the preceding collection code

<collection property="orderDetails"             column="id"             ofType="cn.elinzhou.mybatisTest.pojo.OrderDetailCustom"            select="cn.elinzhou.mybatisTest.mapper.OrderDetailMapper.findDetailByOrdreId"></collection>

In this way, delayed loading is implemented, and the following code is tested.

@ Testpublic void testFindOrders () throws Exception {OrdersMapper orderMapper = sqlSession. getMapper (OrdersMapper. class); List <OrdersCustrom> list = orderMapper. findOrderUser (); ****************** /System. out. println (list ); ****************** /}

Place a breakpoint before the first comment of the above code, and then run the code line by line to observe the log

Debug, stop at the first breakpoint, and then run List list = orderMapper. findOrderUser () in a single step. You can see that the console outputs similar content.

It indicates that the orders table is queried and the orderdetail table is not queried Based on the id.
Then execute the next sentence. The console output is similar

The code just executed is to print out the content in the list and search the database, which indicates that this is achieved through delayed loading. The orderdetail table is queried only when the orderdetail table is actually used, enabling on-demand distribution. The necessary code is executed only when necessary, improving the efficiency of servers and databases.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.