-
为什么使用缓存?
首次访问时,查询数据库,并将数据存储到内存中;再次访问时直接访问缓存,减少IO、硬盘读写次数、提高效率
-
Mybatis中的一级缓存和二级缓存?
-
一级缓存:
它指的是SqlSession对象的缓存。当执行完查询之后,查询的结果会同时存在在SqlSession中。当再次查询同样的数据,mybatis会先去SqlSession中查询是否有,有就直接拿出来使用。当SqlSession对象消失时,Mybatis的一级缓存也就消失了。
-
dao
public interface UserDao { //根据id查询用户信息 public User findUserById(Integer id); }
-
xml
<mapper namespace="com.by.dao.UserDao"> <select id="findUserById" resultType="User" parameterType="int"> select * from user where id=#{id} </select> </mapper>
-
test
Mabytis的缓存
最新推荐文章于 2024-07-06 23:36:01 发布