MyBatis (eight) Resultmap (third) deferred loading (lazy loading)

Source: Internet
Author: User

Example of succession to chapter seventh:
Let's say lazy loading is also called lazily loading.
Lazy Loading: Resultmap data is tree-like structure, when we use branch data to check, and vice versa, do not query, so you can save database resources.

Then I do a little improvement on the basis of the previous demo: 1, first add the settings in Mybatis-config.xml:

<configuration>
    <settings><!--
        <!--lazy Load set to True--
        <setting name= " Lazyloadingenabled "value=" true "/>
        <!--positive load set to False--
        <setting name=" Aggressivelazyloading " Value= "false"/>
    </settings>
2. Modify the Mapper mapping file Orderdao.xml
<?xml version= "1.0" encoding= "UTF-8"?> <! DOCTYPE Mapper Public "-//mybatis.org//dtd mapper 3.0//en" "Http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapp Er namespace= "com.zll.mybatis.dao.OrderDaoLazy" > <resultmap type= "com.zll.mybatis.bean.Orders" id= " Findorderdetailmap "> <id column=" id "property=" id "/> <result column=" createtime "property=" Cr Eatetime "/> <result column=" userid "property=" userid "/> <association property=" user "Javaty Pe= "Com.zll.mybatis.bean.User" column= "id" select= "finduser"/> <collection property= "OrderItems

    "Oftype=" Com.zll.mybatis.bean.OrderItem "column=" id "select=" findordersitem "/> </resultMap>


    <select id= "Findorderdetaillazy" resultmap= "Findorderdetailmap" > select * from Orders </select> <select id= "Finduser" parametertype= "int" resulttype= "Com.zll.mybatis.bean.User" > select* from user where id = #{id} </select> <select id= "Findordersitem" parametertype= "int" Resulttyp E= "Com.zll.mybatis.bean.OrderItem" > select * from orderitem where orderId = #{id} </select> </mapp Er>

Note: Since lazy loading is a separate query, each of our select mappings also needs to be written separately.
The only thing to note here is the column and select Properties in the association and collection tags.
Select: Fill in the ID of the select map we want to invoke
column: Fill in the parameters we want to pass to the select map.

Test code:

/**
 * Unit test for simple App.
 *
/public class Apptest extends TestCase {public
    void Selectorder () throws IOException {
        String resource = "M Ybatis-config.xml ";
        InputStream InputStream = resources.getresourceasstream (Resource);
        Sqlsessionfactory sqlsessionfactory = new Sqlsessionfactorybuilder (). Build (InputStream);
        sqlsession session = Sqlsessionfactory.opensession ();
        Orderdaolazy mapper = Session.getmapper (orderdaolazy.class);

        list<orders> Orders = Mapper.findorderdetaillazy ();

        for (int i=0; i<orders.size (); i++) {
            System.out.println (Orders.get (i). toString ());
        }
        Session.close ();
    }
}

Note: Because the ToString method in our order class reads the user and the OrderItem object and starts lazy loading, you will see that the console prints 3 SELECT statements and queries the database when we call the data. (If you want to improve the test, you can remove the ToString, method of the Order class, and then call the GetUser and Getorderitems methods, to see through the breakpoint way)

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.