MyBatis_Cache_hehe.employment.over.32.1

32.1 MyBatis_The concept of lazy loading and immediate loading

Question: In one-to-many, when we have a user, it has 100 accounts.
When querying users, do you want to find out the associated accounts?
When inquiring about an account, do you want to find out the associated users?
When querying a user, the account information under the user should be, when it was used and when it was queried.
When querying an account, the user information of the account should be queried together with the account query.

  • 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)
  • Load now
    • Regardless of whether it is used or not, as long as the method is called, the query is initiated immediately.
  • Among the four corresponding table relationships: one-to-many, many-to-one, one-to-one, and many-to-many
    • One-to-many, many-to-many : usually we useLazy loading
    • Many-to-one, one-to-one : usually we useLoad now

32.2 MyBatis_One-to-one implementation of lazy loading

  • 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.itheima.dao.IAccountDao">

    <!-- 定义封装account和user的resultMap -->
    <resultMap id="accountUserMap" type="account">
        <id property="id" column="id"></id>
        <result property="uid" column="uid"></result>
        <result property="money" column="money"></result>
        <!-- 一对一的关系映射:配置封装user的内容
        select属性指定的内容:查询用户的唯一标识:
        column属性指定的内容:用户根据id查询时,所需要的参数的值
        -->
        <association property="user" column="uid" javaType="user" select="com.itheima.dao.IUserDao.findById"></association>
    </resultMap>

    <!-- 查询所有 -->
    <select id="findAll" resultMap="accountUserMap">
        select * from account
    </select>

</mapper>
  • Add tags to the main configuration file and enable lazy loading
    <!--配置参数-->
    <settings>
        <!--开启Mybatis支持延迟加载-->
        <setting name="lazyLoadingEnabled" value="true"/>
        <setting name="aggressiveLazyLoading" value="false"></setting>
    </settings>

32.3 MyBatis_ one-to-many implementation of lazy loading

  • Modify the IUserDao.xml mapping file
<?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.itheima.dao.IUserDao">

    <!-- 定义User的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>
        <!-- 配置user对象中accounts集合的映射 -->
        <collection property="accounts" ofType="account" select="com.itheima.dao.IAccountDao.findAccountByUid" column="id"></collection>
    </resultMap>

    <!-- 查询所有 -->
    <select id="findAll" resultMap="userAccountMap">
        select * from user
    </select>

    <!-- 根据id查询用户 -->
    <select id="findById" parameterType="INT" resultType="user">
        select * from user where id = #{uid}
    </select>

</mapper>
  • Add a method to query based on Id in IAccountDao
    /**
     * 根据id查询用户信息
     * @param userId
     * @return
     */
    User findById(Integer userId);

32.4 MyBatis_The concept of cache

  • What is caching
    • Temporary data in memory
  • Why use caching
    • Reduce the number of interactions with the database and improve execution efficiency
  • What kind of data can be cached and what kind of data cannot be used
    • Suitable for caching
      • Frequent inquiries and infrequent changes;
      • The correctness of the data has little effect on the final result.
    • Not suitable for caching
      • Data that changes frequently
      • The correctness of the data has a great influence on the final result.
      • For example: commodity inventory, bank exchange rate, stock market price.

32.5 MyBatis_ first level cache

  • It refers to the cache of the SqlSession object in Mybatis.
  • When we execute the query, the results of the query will be stored in the SqlSession area to provide us at the same time.
  • The structure of the area is a Map. When we query the same data again, mybatis will go to sqlsession first.
  • Check if there is any, and if there is, use it directly.
  • When the SqlSession object disappears, the first level cache of mybatis also disappears.
  • The IUserDao.xml mapping file is as follows:
    <!-- 根据id查询用户 -->
    <select id="findById" parameterType="INT" resultType="user">
        select * from user where id = #{uid}
    </select>
  • UserTest.java
    /**
     * 测试一级缓存
     */
    @Test
    public void testFirstLevelCache(){
    
    
        User user1 = userDao.findById(41);
        System.out.println(user1);

//        sqlSession.close();
        //再次获取SqlSession对象
//        sqlSession = factory.openSession();

        sqlSession.clearCache();//此方法也可以清空缓存

   userDao = sqlSession.getMapper(IUserDao.class);

        User user2 = userDao.findById(41);
        System.out.println(user2);

        System.out.println(user1 == user2);
    }

32.6 MyBatis_ secondary cache:

  • It refers to the cache of the SqlSessionFactory object in Mybatis. The SqlSession created by the same SqlSessionFactory object shares its cache.
  • Steps to use the second-level cache:
    • The first step: Let Mybatis framework support secondary cache (configured in SqlMapConfig.xml)
    <settings>
        <setting name="cacheEnabled" value="true"/>
    </settings>
  • Step 2: Let the current mapping file support the second level cache (configured in IUserDao.xml)
<mapper namespace="com.itheima.dao.IUserDao">
    <!--开启user支持二级缓存-->
    <cache/>
  • Step 3: Let the current operation support the secondary cache (configured in the select tag)
    <!-- 根据id查询用户 -->
    <select id="findById" parameterType="INT" resultType="user" useCache="true">
        select * from user where id = #{uid}
    </select>

Guess you like

Origin blog.csdn.net/qq_44686266/article/details/114092133