This is the 24th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

Mybatis lazy loading

What is lazy loading

A lot of the time during development we don’t always need to load the user’s order information when they are loaded. This is what we call lazy loading.

Lazy loading means that data is loaded only when it is needed and not when it is not. Lazy loading is also called lazy loading.

Advantages of lazy loading

Query from a single table first, and then associate the query from the associated table if necessary. This greatly improves database performance because it is faster to query a single table than to associate the query with multiple tables.

Disadvantages of lazy loading

Because only when the data is needed, the database query will be carried out, so in the case of large volume data query, because the query work also consumes time, it may cause users to wait for a long time, resulting in a decline in user experience.

Matters needing attention

  • In multiple tables:

    • One-to-many, many-to-many: Usually lazy load one-to-one
    • (Many-to-one) : Usually load immediately
  • Lazy loading is implemented based on nested queries

Implementation of lazy loading

Local lazy loading implementation

There is a fetchType attribute in both the Association and Collection tags, and you can modify the local load policy by changing its value.

 <! -- Enable one-to-many lazy loading -->
<resultMap id="userMap" type="user">
<id column="id" property="id"></id>
<result column="username" property="username"></result>
<result column="password" property="password"></result>
<result column="birthday" property="birthday"></result>
<! FetchType ="eager" -->
<collection property="orderList" ofType="order" column="id" select="com.mybatis.dao.OrderMapper.findByUid" fetchType="lazy">
</collection>
</resultMap>

<select id="findAll" resultMap="userMap">
SELECT * FROM `user`
</select>
Copy the code

Global lazy load implementation

The global loading policy can be modified using the setting tag in the core configuration file of Mybatis.

<settings>
<! -- Enable global lazy loading -->
<setting name="lazyLoadingEnabled" value="true"/>
</settings>
Copy the code

Matters needing attention

<! -- Disable one-to-one lazy loading -->
<resultMap id="orderMap" type="order">
<id column="id" property="id"></id>
<result column="ordertime" property="ordertime"></result>
<result column="total" property="total"></result>
<! FetchType ="eager" -->
<association
property="user"
column="uid"
javaType="user"
select="com.mybatis.dao.UserMapper.findById"
fetchType="eager">
</association>
</resultMap>
<select id="findAll" resultMap="orderMap">
SELECT * from orders
</select>
Copy the code

The principle of delay loading is implemented

It works by creating a proxy object for the target object using either CGLIB or Javassist(the default). The interceptor method is entered when the getting method of the lazy loading property of the proxy object is called. For example, when a method is called and a method of the interceptor is entered and lazy loading is found, it will separately send the pre-saved SQL query associated with B object, query B up, and then call a.setb (B) method, so as to call a.getName () method. So that’s how lazy loading works and then object B has a value, and then you go down

conclusion

Lazy loading is mainly implemented in the form of dynamic proxy, which intercepts the specified method to perform data loading.