Lazy loading of Mybatis_day4_Mybatis

  • One-to-one, one-to-many, and many-to-many relationship configuration and implementation in Mybatis can realize the association query of objects. Many times in the actual development process, we do not always need to load his account information when loading user information. This is what we call lazy loading.


1.1 What is lazy loading?

  • Lazy loading:
    • It is loaded only when the data is needed, and the data is not loaded when the data is not needed. Lazy loading is also called lazy loading.
  • Benefits: Query from a single table first, and then from a related table when needed, greatly improving database performance, because querying a single table is faster than multiple tables related to query
    .
  • Disadvantages: Because the database query will only be performed when the data is needed, so when querying a large amount of data, because the query work also consumes time, it may cause the user to wait longer, resulting in a decline in user experience.

1.2 Implementation requirements

  • demand:
  • Query account information and correlate user information. If the account information can be queried to meet the requirements, then there is no need to query the user information. When we need to query the user information, the user information is queried. To query the user information on demand is lazy loading.
  • When mybatis implemented multi-table operations on the third day, we used resultMapto implement one-to-one, one-to-many, and many-to-many relationship operations. Mainly by association、collectionimplementing one-to-one and one-to-many mapping. association and collection have lazy loading function.

1.3 Using assocation to implement lazy loading

  • Requirements: Query account information and user information.
  1. Account persistence layer DAO interface
public interface IAccountDao {
/**
* 查询所有账户,同时获取账户的所属用户名称以及它的地址信息
* @return
*/
List<Account> findAll();
}

  1. Account persistence layer mapping file
<mapper namespace="cn.myp666.dao.IAccountDao">
<!-- 建立对应关系 -->
	<resultMap type="account" id="accountMap">
		<id column="aid" property="id"/>
		<result column="uid" property="uid"/>
		<result column="money" property="money"/>
		<!-- 它是用于指定从表方的引用实体属性的 -->
		<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 attribute:
    used to specify the sql statement that queries the user list, so fill in the id of the sql map
  • column attribute:
    used to specify the parameter source of the sql statement of the select attribute. The above parameter comes from the uid column of account, so it is written as a field name of uid

  1. User's persistence layer interface and mapping file
public interface IUserDao {
/**
* 根据 id 查询
* @param userId
* @return
*/
User findById(Integer userId);
}
<mapper namespace="cn.myp666.dao.IUserDao">
<!-- 根据 id 查询 -->
	<select id="findById" resultType="user" parameterType="int" >
		select * from user where id = #{uid}
	</select>
</mapper>

  1. Turn on Mybatis' lazy loading strategy
  • Enter the official document of Mybaits and find the description of settings:
    Insert picture description here
  • We need to add the delayed loading configuration in the Mybatis configuration file SqlMapConfig.xml file.
<settings>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
</settings>


Use Collection for lazy loading

  • Similarly, we can also configure the lazy loading strategy in the node of the one-to-many relationship configuration. <collection>There are also select attributes and column attributes in the nodes.
  • Requirement: When the user object is loaded, 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 for writing user and account persistence layer interface
List<User> findAll();
/**
* 根据用户 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 是用于建立一对多中集合属性的对应关系
ofType 用于指定集合元素的数据类型
select 是用于指定查询账户的唯一标识(账户的 dao 全限定类名加上方法名称)
column 是用于指定使用哪个字段的值作为条件查询
-->
	<collection property="accounts" ofType="account"
		select="cn.myp666.dao.IAccountDao.findByUid"
		column="id">
	</collection>
</resultMap>

<!-- 配置查询所有操作 -->
<select id="findAll" resultMap="userMap">
	select * from user
</select>
  • <collection> tag:
    mainly used to load the associated collection object
  • select attribute:
    used to specify the sql statement that queries the account list, so fill in the id of the sql map
  • column attribute:
    used to specify the parameter source of the sql statement of the select attribute. The above parameter comes from the id column of the user, so it is written as the id field name.

  1. Write account persistence layer mapping configuration
<!-- 根据用户 id 查询账户信息 -->
<select id="findByUid" resultType="account" parameterType="int">
	select * from account where uid = #{uid}
</select>
Published 94 original articles · praised 0 · visits 2097

Guess you like

Origin blog.csdn.net/qq_16836791/article/details/104800191