Questions:
In one-to-many, when we have a user, it has 100 accounts.
When inquiring the user, the account information under the user is when to use and when to query again.
When querying an account, the user information of the account should be queried again as the account is queried.
What is lazy loading
Delayed loading
Queries are initiated only when data is actually used and not when data is not used. On-demand loading (lazy loading)
Immediate loading
Whether you use it or not, just call the method and start the query immediately.
In the corresponding four table relationships, we usually load them in the following way
Table type | Loading mode |
---|---|
One-to-many, one-to-many | Delayed loading |
One-to-one, one-to-one | Immediate loading |
One-to-one Delayed Loading
Demand:
Query account information and user information at the same time, delay loading user information.
The screenshots are as follows:
See mybatisdemo8 for details
Steps:
-
dao interface for writing accounts
public interface IAccountDao { //Query all, and also get the user information of the current account. List<Account> findAll(); }
-
Mapping Profile for Accounts
Select: Fill in the id of the select map we want to call
column: Fill in the parameters we want to pass to the select Map<?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.ben.dao.IAccountDao"> <!-- Define encapsulation account and user Of resultMap --> <resultMap id="accountUserMap" type="account"> <id property="id" column="aid"></id> <result property="uid" column="uid"></result> <result property="money" column="money"></result> <!-- One-to-one relational mapping: configuration encapsulation user Contents select Attribute specified content: Query the unique identity of the user: column Attribute-specified content: Users depend on id When querying, the values of the required parameters --> <association property="user" column="uid" javaType="user" select="com.ben.dao.IUserDao.findUserById"></association> </resultMap> <!-- Query all --> <select id="findAll" resultMap="accountUserMap"> select * from account </select> </mapper>
-
User's Persistence Layer Interface
public interface IUserDao { // Query a user by ID User findUserById(Integer id); }
-
User Mapping Profile
<?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"> <!--namespace: Names used to distinguish different classes --> <mapper namespace="com.ben.dao.IUserDao"> <!-- adopt Id Query a user --> <select id="findUserById" parameterType="Integer" resultType="com.ben.domain.User"> select * from user where id = #{v} </select> </mapper>
-
Delay Loading Strategy of Opening mybatis in SqlMapconfig.xml
<!--configuration parameter--> <settings> <!--open Mybatis Supporting lazy loading--> <setting name="lazyLoadingEnabled" value="true"/> <setting name="aggressiveLazyLoading" value="false"></setting> </settings>
-
Write tests to check only account information and not user information
@Test public void testFindAll(){ List<Account> list = accountDao.findAll(); for (Account account : list) { System.out.println("--------each account Information------------"); System.out.println(account); System.out.println(account.getUser()); } }
Obviously, this time, only the way to query the Account object in List, not involving the User object, so there is no Sql residual query account associated with the User object query.
One-to-many Delayed Loading
-
The Method of Writing Persistent Layer Interface between User and Account
/** * Query all users and get information about all accounts under users at the same time * @return */ List<User> findAll(); /** * Query account information based on user id * @param uid * @return */ List<Account> findAccountByUid(Integer uid);
-
Writing User Persistence Layer Mapping Configuration IUserDao.xml
<?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"> <!--namespace: Names used to distinguish different classes --> <mapper namespace="com.ben.dao.IUserDao"> <!-- Definition User Of resultMap--> <resultMap id="userAccountMap" type="user"> <id property="id" column="id"></id> <result property="username" column="username"></result> <result property="address" column="address"></result> <result property="sex" column="sex"></result> <result property="birthday" column="birthday"></result> <!-- To configure user Object accounts Mapping of Sets --> <collection property="accounts" ofType="account" select="com.ben.dao.IAccountDao.findAccountByUid" column="id"></collection> </resultMap> <!-- Query all --> <select id="findAll" resultMap="userAccountMap"> select * from user </select> <!-- adopt Id Query a user --> <select id="findUserById" parameterType="Integer" resultType="com.ben.domain.User"> select * from user where id = #{v} </select> </mapper>
-
Writing Account Persistence Layer Mapping Configuration IAccountDao.xml
<?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.ben.dao.IAccountDao"> <!-- Define encapsulation account and user Of resultMap --> <resultMap id="accountUserMap" type="account"> <id property="id" column="aid"></id> <result property="uid" column="uid"></result> <result property="money" column="money"></result> <!-- One-to-one relational mapping: configuration encapsulation user Contents select Attribute specified content: Query the unique identity of the user: column Attribute-specified content: Users depend on id When querying, the values of the required parameters --> <association property="user" column="uid" javaType="user" select="com.ben.dao.IUserDao.findUserById"></association> </resultMap> <!-- Query all --> <select id="findAll" resultMap="accountUserMap"> select * from account </select> <!-- According to the user id Query Account List --> <select id="findAccountByUid" resultType="account"> select * from account where uid = #{uid} </select> </mapper>
-
Test only loads user information
//Query all users @Test public void testFindAll(){ List<User> list = userdao.findAll(); // for (User user : list) { // System.out.println("----- information for each user --"); // System.out.println(user); // System.out.println(user.getAccounts()); // } }
Account account information was not loaded;