Delayed loading of mybatis

  • In Mybatis, the configuration and implementation of one-to-one, one to many, many to many relationships can realize the associated query of objects. In the actual development process, we don't always need to load the user's account information when loading the user information. This is what we call delayed loading.


1.1 what is delayed loading?

  • Delayed loading:
    • It is to load data when it is needed, and not to load data when it is not needed. Lazy loading is also called lazy loading
  • Benefit: query from single table first, and then associate query from associated table when necessary, which greatly improves database performance, because single table query is faster than multiple tables associated query
    It must be fast.
  • Disadvantage: only when data is needed, can database query be performed. In mass data query, because query also consumes time, it may cause users to wait longer, resulting in a decline in user experience.

1.2 implementation requirements

  • Demand:
  • Query account information and associated query user information. If we can meet the requirements by querying the account information first, we don't need to query the user information again. When we need to query the user information, we need to query the user information again. To query user information on demand is to delay loading.
  • In the third day of mybatis, we used resultMap to implement one-to-one, one to many, many to many relationships. It mainly realizes one-to-one and one to many mapping through association and collection. Association and collection have the function of delayed loading.

1.3 using allocation to achieve delayed loading

  • Requirement: query account information and user information at the same time.
  1. DAO interface of account persistence layer
public interface IAccountDao {
/**
* Query all accounts, and obtain the user name and address information of the account
* @return
*/
List<Account> findAll();
}
  1. Persistent layer mapping file of account
<mapper namespace="cn.myp666.dao.IAccountDao">
<!-- Establish correspondence -->
	<resultMap type="account" id="accountMap">
		<id column="aid" property="id"/>
		<result column="uid" property="uid"/>
		<result column="money" property="money"/>
		<!-- It is used to specify the reference entity property from the table side -->
		<association property="user" javaType="cn.myp666.domain.user"
			select="cn.myp666.dao.IUserDao.findById"
column="uid">
		</association>
	</resultMap>
	
<select id="findAll" resultMap="accountMap">
	select * from account
</select>
</mapper>
  • select property:
    The sql statement used to specify the query user list, so the id of the sql mapping is filled in
  • column property:
    The parameter source of the sql statement used to specify the select attribute. The above parameter comes from the uid column of account, so it is written as the uid field name
  1. User's persistence layer interface and mapping file
public interface IUserDao {
/**
* Query by id
* @param userId
* @return
*/
User findById(Integer userId);
}
<mapper namespace="cn.myp666.dao.IUserDao">
<!-- according to id query -->
	<select id="findById" resultType="user" parameterType="int" >
		select * from user where id = #{uid}
	</select>
</mapper>
  1. Enable the delayed loading policy of Mybatis
  • Enter the official document of Mybaits and find the settings Description:
  • We need to add the configuration of delayed loading in the configuration file SqlMapConfig.xml of Mybatis.
<settings>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
</settings>

Using Collection to delay loading

  • In the same way, we can also configure the delay loading strategy in the nodes of one to many relationship configuration. The < Collection > node also has the select attribute and column attribute.
  • Requirement: when loading the user object, query the account information owned by the user.
  1. Add list < account > attribute to User entity class
public class User implements Serializable {
private Integer id;
private String username;
private Date birthday;
private String sex;
private String address;
private List<Account> accounts;

public List<Account> getAccounts() {
	return accounts;
}
public void setAccounts(List<Account> accounts) {
	this.accounts = accounts;
  1. Method of writing interface between user and account persistence layer
List<User> findAll();
/**
* Query account information according to user id
* @param uid
* @return
*/
List<Account> findByUid(Integer uid);
  1. Write user persistence layer mapping configuration
<resultMap type="user" id="userMap">
	<id column="id" property="id"></id>
	<result column="username" property="username"/>
	<result column="address" property="address"/>
	<result column="sex" property="sex"/>
	<result column="birthday" property="birthday"/>
	<!-- collection It is used to establish the correspondence of set attributes in one to many
ofType Data type used to specify collection elements
select Is the unique ID used to specify the query account (account's dao Fully qualified class name plus method name)
column Is used to specify which field's value is used as a condition query
-->
	<collection property="accounts" ofType="account"
		select="cn.myp666.dao.IAccountDao.findByUid"
		column="id">
	</collection>
</resultMap>

<!-- Configure query all operations -->
<select id="findAll" resultMap="userMap">
	select * from user
</select>
  • < Collection > label:
    Mainly used to load associated collection objects
  • select property:
    The sql statement used to specify the account list to query, so the id of the sql mapping is filled in
  • column property:
    The parameter source of the sql statement used to specify the select attribute. The above parameter comes from the id column of user, so it is written as the field name id
  1. Write account persistence layer mapping configuration
<!-- According to users id Query account information -->
<select id="findByUid" resultType="account" parameterType="int">
	select * from account where uid = #{uid}
</select>
Published 33 original articles, won praise 0, visited 661
Private letter follow

Tags: SQL Attribute Mybatis Database

Posted on Wed, 11 Mar 2020 03:02:34 -0700 by peterj