Test mybatis delay Load errors and workarounds

Source: Internet
Author: User

What is deferred loading?

Lazy loading is also called load-on-demand, which means loading the master information first, and then loading the information when needed.

Demand:

Query the order information, when necessary to query the user information

Implementation method:

Write two statement, one statement is to query the order information, one is to query the user information, but query the order Information statement to use the RESULTMAP tag for results mapping.

Mapper Interface:

/**
* Delay loading query When the order needs to query user information
* */
Public list<orderext> lazyloading ();

Mapper Map File: Ordermapper.xml

<resultmap type= "Orderext" id= "lazyloading" >
<!--order Information--
<id column= "id" property= "id"/>
<result column= "number" property= "number"/>
<result column= "user_id" property= "user_id"/>
<!--user Information--
<association property= "User"
select= "Com.heima.mapper.UserMapper.findUserById" column= "user_id"/>
</resultMap>
<!--delayed Load-
<select id= "lazyloading" resultmap= "lazyloading" >
SELECT * FROM Orders
</select>

<select id= "Finduserbyid" parametertype= "int" resulttype= "Com.heima.po.User" >
SELECT * FROM USER WHERE id = #{id}
</select>

MyBatis Global Map File:

<!--lazy Loading

<settings>
   <setting name= "lazyloadingenabled" value= "true"/>
   & Lt;setting name= "aggressivelazyloading" value= "false"/>
  </settings>

test class:

@Test
 public void lazyloading () {
  sqlsession sqlsession = sqlsessionfactory.opensession ( );
   ordermapper mapper = Sqlsession.getmapper (Ordermapper.class);
   List<OrderExt> List = mapper.lazyloading ();
   for (Orderext order:list) {
    system.out.println (order);
  
 }

An error occurred during the test:

  

Workaround:

The Ordermapper mapping file in the association label of the column value is wrong, the following is the correct wording, column this property can not be omitted, or will be an error.

<association property= "User"
select= "Com.heima.mapper.UserMapper.findUserById" column= "user_id"/>

Test mybatis delay Load errors and workarounds

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.