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.
<mappernamespace="cn.myp666.dao.IAccountDao"><!-- 建立对应关系 --><resultMaptype="account"id="accountMap"><idcolumn="aid"property="id"/><resultcolumn="uid"property="uid"/><resultcolumn="money"property="money"/><!-- 它是用于指定从表方的引用实体属性的 --><associationproperty="user"javaType="cn.myp666.domain.user"select="cn.myp666.dao.IUserDao.findById"column="uid"></association></resultMap><selectid="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
User's persistence layer interface and mapping file
publicinterfaceIUserDao{/**
* 根据 id 查询
* @param userId
* @return
*/
User findById(Integer userId);}
<mappernamespace="cn.myp666.dao.IUserDao"><!-- 根据 id 查询 --><selectid="findById"resultType="user"parameterType="int">
select * from user where id = #{uid}
</select></mapper>
Turn on Mybatis' lazy loading strategy
Enter the official document of Mybaits and find the description of settings:
We need to add the delayed loading configuration in the Mybatis configuration file SqlMapConfig.xml file.
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.
Write user persistence layer mapping configuration
<resultMaptype="user"id="userMap"><idcolumn="id"property="id"></id><resultcolumn="username"property="username"/><resultcolumn="address"property="address"/><resultcolumn="sex"property="sex"/><resultcolumn="birthday"property="birthday"/><!-- collection 是用于建立一对多中集合属性的对应关系
ofType 用于指定集合元素的数据类型
select 是用于指定查询账户的唯一标识(账户的 dao 全限定类名加上方法名称)
column 是用于指定使用哪个字段的值作为条件查询
--><collectionproperty="accounts"ofType="account"select="cn.myp666.dao.IAccountDao.findByUid"column="id"></collection></resultMap><!-- 配置查询所有操作 --><selectid="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.