MyBatis Delayed Loading
I. The Meaning of Delayed Loading
Delayed loading in MyBatis, also known as lazy loading, refers to delaying the select ion query of the associated objects according to the set delay rules when querying the tables. For example, in one-to-many queries, only one party is queried. When multi-party data is needed in the program, mybatis sends out sql statements to query, so that sub-delayed loading can reduce the pressure of the database. MyBatis's delayed loading only sets delays for queries of related objects, and directly executes query statements for main loaded objects.
2. Significance of Delayed Loading
Let users wait a little shorter. For example, browsers usually load text first, display it and then load pictures. In this way, the user will not wait until everything is downloaded before it is displayed back to the user. In some areas, such as software development, proper use of delayed loading can avoid CPU and memory peaks.
3. Delayed Loading Using resultMap
1. Importing jar packages
Delayed loading requires importing jar packages for cglib and asm
2. Open Delay Loading Switch
Configure the following tags and contents in SqlMapConfig.xml
<settings> <! - Delayed Loading Switch - > <setting name="lazyLoadingEnabled" value="true"/> <! -- When set to true, lazy objects may be loaded by all of the lazy attributes. Otherwise, each attribute is loaded on demand. > <setting name="aggressiveLazyLoading" value="false"/> </settings>
3. Database tables
(1) Catalogue of Cuisine
(2) Table of dishes
4. Entity class
(1) Substantial Classes of Cuisine
package com.gh.entity; import lombok.Getter; import lombok.Setter; import lombok.ToString; @Getter @Setter @ToString public class Style { private Integer styleId; private String styleName; }
(2) Substantive Categories of Vegetables
package com.gh.entity; import lombok.Getter; import lombok.Setter; import lombok.ToString; @Getter @Setter @ToString public class Food { private Integer foodId; private String foodName; private Double foodPrice; private Integer foodStar; private String foodDescript; private String foodSrc; private Style style; }
5. Create FoodDao interface and declare method
package com.gh.dao; import com.gh.entity.Food; import com.gh.entity.Style; import java.util.List; public interface FoodDao { public List<Food> selectFoodAndStyleByFoodName(String foodName) throws Exception; }
6. Define select sql statement and resultMap
Here we take food and style as examples:
<?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"> <mapper namespace="com.gh.dao.FoodDao"> <!--resultMap--> <resultMap id="FoodAndStyle" type="com.gh.entity.Food"> <result column="foodId" property="foodId"></result> <result column="foodName" property="foodName"></result> <result column="foodPrice" property="foodPrice"></result> <result column="foodStar" property="foodStar"></result> <result column="foodDescript" property="foodDescript"></result> <result column="foodSrc" property="foodSrc"></result> <!--Here you can call methods and pass parameters, where the parameters are foodStyleId,The parameters invoked are full paths--> <!--Here's what delays loading using the calling method--> <association property="style" javaType="com.gh.entity.Style" column="foodStyleId" select="com.gh.dao.FoodDao.selectStyleById"> <result column="styleId" property="styleId"></result> <result column="styleName" property="styleName"></result> </association> </resultMap> <!--Content loaded directly--> <select id="selectFoodAndStyleByFoodName" parameterType="com.gh.entity.Food" resultMap="FoodAndStyle"> select * from food where foodName = #{value}; </select> <!--Delayed loading sql Statement, called content--> <select id="selectStyleById" resultType="com.gh.entity.Style"> select * from Style where styleId = #{value} </select> </mapper>
7. Test class
package com.gh.test; import com.gh.dao.FoodDao; import com.gh.entity.Food; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.Before; import org.junit.Test; import java.io.IOException; import java.io.Reader; import java.util.List; public class MyBatisTest { private SqlSessionFactory factory = null; @Before public void init(){ try { Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml"); factory = new SqlSessionFactoryBuilder().build(reader); } catch (IOException e) { e.printStackTrace(); } } @Test public void selectFoodAndStyleByFoodName(){ SqlSession sqlSession = factory.openSession(); FoodDao mapper = sqlSession.getMapper(FoodDao.class); try { List<Food> foods = mapper.selectFoodAndStyleByFoodName("sea cucumber soup"); System.out.println(foods); } catch (Exception e) { e.printStackTrace(); } } }