MyBatis第五天(延迟加载 缓存)

Mybatis

    • 一对一延迟加载
      • 1.在SqlMapConfig.xml中配置setting标签
      • 2.在IAccoutDao.xml中配置association标签
      • 3.测试类
      • 4.成功运行
    • 一对多延迟加载
      • 2.在IUserDao.xml中配置collection标签
  • 缓存
    • 一级缓存
    • 二级缓存
      • 1.SqlMaoConfig.xml中
      • 2.在需要使用二级缓存的实体类的mapper中
      • 3.测试类

一对一延迟加载

1.在SqlMapConfig.xml中配置setting标签

详情看中文官网(http://www.mybatis.org/mybatis-3/zh/configuration.html#settings)

<settings>
        
        <setting name="lazyLoadingEnabled" value="true"/>

        <setting name="aggressiveLazyLoading" value="true"/>
    settings>

2.在IAccoutDao.xml中配置association标签


    <resultMap id="accountUserMap" type="Account">
        <id property="id" column="id">id>

        <result property="uid" column="uid">result>
        <result property="money" column="money">result>

        
        <association property="user" column="uid" javaType="User" select="com.daniel.dao.IUserDao.findById">
            <id property="id" column="id">id>
            <result property="username" column="username">result>
            <result property="sex" column="sex">result>
            <result property="address" column="address">result>
            <result property="birthday" column="birthday">result>
        association>
    resultMap>

    <select id="findAll" resultMap="accountUserMap">
        select * from  account
    select>

3.测试类

 @Test
    public void findAll(){
        List<Account> accounts = accoutDao.findAll();
        for (Account account:
             accounts) {
            System.out.println("每一个account的信息");
            System.out.println(account);
            System.out.println(account.getUser());
        }
    }

4.成功运行

MyBatis第五天(延迟加载 缓存)_第1张图片

一对多延迟加载

和一对一没有太多区别

2.在IUserDao.xml中配置collection标签


    <resultMap id="accountUserMap" type="Account">
         <id property="id" column="id">id>
        <result property="username" column="username">result>
        <result property="sex" column="sex">result>
        <result property="address" column="address">result>
        <result property="birthday" column="birthday">result>

        
    	<collection property="accounts" ofType="Account" select="com.daniel.dao.IAccoutDao.findAccountByUid" column="id">
            <id property="id" column="aid">id>
            <result property="uid" column="uid">result>
            <result property="money" column="money">result>
        collection>
    resultMap>

   

缓存

MyBatis第五天(延迟加载 缓存)_第2张图片

一级缓存

其实mybatis中默认就是一级缓存了(平时的测试类就是一级缓存存在SqlSession中)

二级缓存

1.SqlMaoConfig.xml中

<settings>
        <setting name="cacheEnabled" value="true"/>
    settings>

2.在需要使用二级缓存的实体类的mapper中

 
    <cache/>

 
    <select id="findById" parameterType="INT" resultType="user" useCache="true">
        select * from user where id = #{uid}
    select>

3.测试类

public class SecondLevelCacheTest {

    private InputStream in;
    private  SqlSessionFactory factory;

    @Before//用于在测试方法执行之前执行
    public void init()throws Exception{
        //1.读取配置文件,生成字节输入流
        in = Resources.getResourceAsStream("SqlMapConfig.xml");
        //2.获取SqlSessionFactory
        factory = new SqlSessionFactoryBuilder().build(in);

    }

    @After//用于在测试方法执行之后执行
    public void destroy()throws Exception{
        in.close();
    }

    /**
     * 测试二级缓存
     */
    @Test
    public void testFirstLevelCache(){
        SqlSession sqlSession1 = factory.openSession();
        IUserDao dao1 = sqlSession1.getMapper(IUserDao.class);
        User user1 = dao1.findById(41);
        System.out.println(user1);
        sqlSession1.close();//一级缓存消失

        SqlSession sqlSession2 = factory.openSession();
        IUserDao dao2 = sqlSession2.getMapper(IUserDao.class);
        User user2 = dao2.findById(41);
        System.out.println(user2);
        sqlSession2.close();

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


}

MyBatis第五天(延迟加载 缓存)_第3张图片
注意:
从图中可以看出来第二次findbyId根本没有走数据库(数据都是从SqlSessionFactory的二级缓存中拿的 不是对象!)

你可能感兴趣的:(Database)