basic concepts
- Problem: In one-to-many, when we have one user, it has 100 users
- When querying users, do you want to find out the associated accounts?
- When inquiring about the account, do you want to find out the associated users?
- What is lazy loading
- The query is initiated when the data is actually used, and not when it is not in use. On-demand loading (lazy loading)
- What is immediate loading
- Regardless of whether it is used or not, as long as the method is called, the query is initiated immediately
- Corresponding to the four relational tables
- One-to-many, many-to-many: usually we use lazy loading
- Many-to-one, one-to-one: usually we use immediate loading
One-to-one lazy loading
- Add settings in the main configuration file SqlMapConfig.xml
<settings>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
</settings>
- Modify the resultMap of Account
<?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="dao.AccountDao">
<resultMap id="accountUserMap" type="domain.Account">
<id property="id" column="id"></id>
<result property="uid" column="uid"></result>
<result property="money" column="money"></result>
<!--一对一的关系映射,配置封装user的内容-->
<association property="mybatisUser" column="uid" javaType="domain.MybatisUser" select="dao.UserDao.findUserById">
</association>
</resultMap>
<!--查询所有 -->
<select id="findAll" resultMap="accountUserMap">
SELECT * FROM ACCOUNT ;
</select>
</mapper>
- Write SQL statements to query all accounts in AccountDao.xml
<select id="findAll" resultMap="accountUserMap">
SELECT * FROM ACCOUNT ;
</select>
- In UserDao.xml, write the SQL statement to query the user corresponding to the account
<select id="findUserById" parameterType="int" resultType="domain.MybatisUser">
SELECT * FROM mybatisuser WHERE id=#{
uid};
</select>
-
SQL statement executed
- If you only query the account
public void testFindAll(){ List<Account> accounts = accountDao.findAll(); }
- If you need to inquire about the account and the user information that the account belongs to
public void testFindAll(){ List<Account> accounts = accountDao.findAll(); for (Account account : accounts){ System.out.println("账户"+account.getId()+"----------------------------"); System.out.println(account); System.out.println(account.getMybatisUser()); } }
One-to-many lazy loading
- Modify the resultMap of Account
<resultMap id="userAccountMap" type="domain.UserAccount">
<id property="id" column="id"></id>
<result property="username" column="username"></result>
<result property="birthday" column="birthday"></result>
<result property="sex" column="sex"></result>
<result property="address" column="address"></result>
<!--一对多的关系映射,配置UserAccount对象中accounts集合的内容-->
<collection property="accounts" ofType="domain.Account" column="id" select="dao.AccountDao.findAccountByUid"></collection>
</resultMap>
- Write SQL statements to query all users in UseDao.xml
<select id="findAll" resultMap="userAccountMap">
SELECT * FROM mybatisuser;
</select>
- Write the SQL statement to query the account under the user in AccountDao.xml
<select id="findAccountByUid" parameterType="int" resultType="domain.Account">
select * from account where uid=#{
id};
</select>
- Results of the
- Only query all users
public void testFindAll(){ List<UserAccount> users = userDao.findAll(); }
- Query users and get all user account information at the same time
public void testFindAll(){
List<UserAccount> users = userDao.findAll();
for (UserAccount user : users){
System.out.println("用户"+user.getId()+"--------------------");
System.out.println(user);
System.out.println(user.getAccounts());
}
}