默认情况下,MyBatis只启用了本地的会话缓存,它仅仅对一个会话中的数据进行缓存。这也就是大家常说的MyBatis一级缓存,一级缓存的作用域是SqlSession。

MyBatis一级缓存的运行过程是这样的:执行SQL语句的过程中,首次执行它时从数据库获取的所有数据会被存储在一段高速缓存中,今后执行这条语句时就会从高速缓存中读取结果,而不是再次查询数据库。MyBatis提供了默认下基于Java HashMap的缓存实现。

重点是要明白:MyBatis执行SQL语句之后,这条语句的执行结果被缓存,以后再执行这条语句的时候,会直接从缓存中拿结果,而不是再次执行SQL。但是一旦执行新增或更新或删除操作,缓存就会被清除。下面将分情况来验证一下。

第1种情况:同个session进行两次相同查询

public class Test
{
    public static void main(String[] args) throws Exception
    {
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");

        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession session = sqlSessionFactory.openSession();

        Person p1 = session.selectOne("cn.mybatis.mydemo.mapper.PersonMapper.selectPersonById", 1);

        Person p2 = session.selectOne("cn.mybatis.mydemo.mapper.PersonMapper.selectPersonById", 1);
        
        session.commit();
        
        session.close();
    }
}

结论:MyBatis只进行1次数据库查询。

==> Preparing: select * from t_person where id = ?
==> Parameters: 1(Integer)
<== Total: 1
User{id=1, name='Kit', age=23, birthday=Sun Oct 12 23:20:13 CST 2010}
User{id=1, name='Kit', age=23, birthday=Sun Oct 12 23:20:13 CST 2010}

第2种情况:同个session进行两次不同的查询。

public class Test
{
    public static void main(String[] args) throws Exception
    {
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");

        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession session = sqlSessionFactory.openSession();

        Person p1 = session.selectOne("cn.mybatis.mydemo.mapper.PersonMapper.selectPersonById", 1);

        Person p2 = session.selectOne("cn.mybatis.mydemo.mapper.PersonMapper.selectPersonById", 2);
        
        session.commit();
        
        session.close();
    }
}

结论:MyBatis进行两次数据库查询。

==> Preparing: select * from t_person where id = ?
==> Parameters: 1(Integer)
<== Total: 1
User{id=1, name='kit', age=23, birthday=Sun Oct 12 23:20:13 CST 2010}
==> Preparing: select * from t_person where id = ?
==> Parameters: 2(Integer)
<== Total: 1
User{id=2, name='Jim', age=50, birthday=Sat Dec 06 17:12:01 CST 2010}

第3种情况:不同session,进行相同查询。

public class Test
{
    public static void main(String[] args) throws Exception
    {
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");

        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession session1 = sqlSessionFactory.openSession();

        Person p1 = session1.selectOne("cn.mybatis.mydemo.mapper.PersonMapper.selectPersonById", 1);

        SqlSession session2 = sqlSessionFactory.openSession();
        Person p2 = session2.selectOne("cn.mybatis.mydemo.mapper.PersonMapper.selectPersonById", 2);

        session1.commit();
        session2.commit();

        session1.close();
        session2.close();
    }
}

结论:MyBatis进行两次数据库查询。

==> Preparing: select * from t_person where id = ?
==> Parameters: 1(Integer)
<== Total: 1
User{id=1, name='kit', age=23, birthday=Sun Oct 12 23:20:13 CST 2010}
==> Preparing: select * from t_person where id = ?
==> Parameters: 2(Integer)
<== Total: 1
User{id=2, name='Jim', age=50, birthday=Sat Dec 06 17:12:01 CST 2010}

第4种情况:同个session,查询之后更新数据,再次查询相同的语句。

public class Test
{
    public static void main(String[] args) throws Exception
    {
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");

        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession session = sqlSessionFactory.openSession();

        Person p = null;
        
        p = session.selectOne("cn.mybatis.mydemo.mapper.PersonMapper.selectPersonById", 1);

        p.setAge(30);

        session.update("cn.mybatis.mydemo.mapper.PersonMapper.updatePerson", p);

        p = session.selectOne("cn.mybatis.mydemo.mapper.PersonMapper.selectPersonById", 1);

        session.commit();

        session.close();

    }
}

结论:更新操作之后缓存会被清除:

==> Preparing: select * from t_person where id = ?
==> Parameters: 1(Integer)
<== Total: 1
User{id=1, name='kit', age=23, birthday=Sun Oct 12 23:20:13 CST 2010}
==> Preparing: update t_person set age = ? where ID = ?
==> Parameters: 30(Integer), 1(Integer)
<== Updates: 1
==> Preparing: sselect * from t_person where id = ?
==> Parameters: 1(Integer)
<== Total: 1
User{id=1, name='kit', age=30, birthday=Sun Oct 12 23:20:13 CST 2010}

小结

很明显,以上各种情况验证了一级缓存的概念,在同个SqlSession中,查询语句相同的sql会被缓存,但是一旦执行新增或更新或删除操作,缓存就会被清除。

标签: none

[网站公告]-[2024年兼职介绍]


已有 6 条评论

  1. tony tony

    请教一下 一级缓存有过期时间么?如果没有过期时间 分布式的时候 一服务器修改了数据 另一台服务器岂不是还保留原始数据?

    1. 路人甲 路人甲

      你这个问题问的真好,我也想知道

    2. 路人甲 路人甲

      SqlSession生命周期一般是在请求/方法作用域内,新开session就会查一次数据库,所以没超时时间问题也不会很大

  2. mybatis的一级缓存本质就是map,map是没有过期时间的。

  3. 又菜又懒 又菜又懒

    所以一级缓存基本用不上

添加新评论