Lazy loading and immediate loading

basic concepts

  1. Problem: In one-to-many, when we have one user, it has 100 users
    1. When querying users, do you want to find out the associated accounts?
    2. When inquiring about the account, do you want to find out the associated users?
  2. What is lazy loading
    1. The query is initiated when the data is actually used, and not when it is not in use. On-demand loading (lazy loading)
  3. What is immediate loading
    1. Regardless of whether it is used or not, as long as the method is called, the query is initiated immediately
  4. Corresponding to the four relational tables
    1. One-to-many, many-to-many: usually we use lazy loading
    2. Many-to-one, one-to-one: usually we use immediate loading

One-to-one lazy loading

  1. Add settings in the main configuration file SqlMapConfig.xml
<settings>
    <setting name="lazyLoadingEnabled" value="true"/>
    <setting name="aggressiveLazyLoading" value="false"/>
</settings>
  1. 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>
  1. Write SQL statements to query all accounts in AccountDao.xml
<select id="findAll" resultMap="accountUserMap">
    SELECT * FROM ACCOUNT ;
</select>
  1. 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>
  1. SQL statement executed

    1. If you only query the account
    public void testFindAll(){
          
          
        List<Account> accounts = accountDao.findAll();
    }
    

    Insert picture description here

    1. 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());
        }
    }
    

    Insert picture description here

One-to-many lazy loading

  1. 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>
  1. Write SQL statements to query all users in UseDao.xml
<select id="findAll" resultMap="userAccountMap">
    SELECT * FROM mybatisuser;
</select>
  1. 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>
  1. Results of the
    1. Only query all users
     public void testFindAll(){
          
          
         List<UserAccount> users = userDao.findAll();
     }
    

Insert picture description here

  1. 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());
     }
 }

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_40857365/article/details/112849426