MyBatis一级缓存介绍

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAeG1oLXN4aC0xMzE0,size_13,color_FFFFFF,t_70,g_se,x_16默认情况下,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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值