Mybatis_二级缓存

我们可以自己配置管理二级缓存

在一个项目中肯定会存在很多公用的查询数据,对于这一部分的数据,没不要每一个用户访问时都去查询数据库,因此配置二级缓存僵尸飞车必要的。

1.在核心配置文件sqlMapConfig.xml中的configuration下的setting下加入:

        <!-- 
        开启二级缓存
        在mybatis中,只要是缓存的配置都是二级缓存
         -->
        <setting name="cacheEnabled" value = "true" />

2.在某一个映射xml(例如PersonMap.xml)中的mapper下加入:

    <!-- 当前映射文件开启二级缓存-->
    <cache />

3.Person必须序列化并生成UID(因为缓存的数据不只是放在内存中,也可以放在硬盘上,因为内存大小是有限的):

public class Person implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private Integer id;
    private String name;
    private Integer gender;
    private String address;
    private Date birthday;
    private List<Orders> orderList;
    public List<Orders> getOrderList() {
        return orderList;
    }
    public void setOrderList(List<Orders> orderList) {
        this.orderList = orderList;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getGender() {
        return gender;
    }
    public void setGender(Integer gender) {
        this.gender = gender;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

UID在序列化好后鼠标放在Person上的会显示。

设置好二级缓存后,就算session名字不同,只要查询的sql相同,第二次就不会再发出sql而是从缓存取。因此二级缓存是跨session的。

    SqlSessionFactory sessionFactory;
    public void selectPersonByIdLazy() {
        // 创建SqlSession
        SqlSession session = sessionFactory.openSession();
        try {
            // 首先查询二级缓存如果有数据就从其中拿出数据就可以,如果没有数据,就去一级缓存中来查,一级缓存如果也没有,那就去数据库查,查出来后把数据放进一级和二级缓存中
            Person person = session.selectOne("xxx.x.mapper.PersonMapper.selectPersonById", 1);
            System.out.println(person);
        }finally {
                session.close();
            }
            //第二次跨session查,也是从二级缓存中取
            SqlSession session1 = sessionFactory.openSession();
            try {
            Person person = session1.selectOne("xxx.x.mapper.PersonMapper.selectPersonById", 1);
            System.out.println(person);
        }finally {
            session.close();
        }
    }

和一级缓存同理,两次查询中间如果有修改语句,会清掉缓存。第二次查询还是要查数据库。

用不用缓存是可配的。上面查询的id是selectPersonById,那就在PersonMapper.xml中的这个id中配置:

useCache:是否启用二级缓存
    <select id="selectPersonByID" parameterType="java.lang.Integer" resultMap="BaseResultMap" useCache="true">
        select * from person_test where id = #{id}
    </select>

true是用缓存,false是不用缓存,如果是false,那么两次查询中间没有修改,第二次查询也会查数据库

更新是会不会清掉缓存也能配置,在更新中写:

flushCache:是否清掉缓存,true是清掉,false是不清,如果是false,那么两次查询中间修改了,第二次查询也是从缓存中取
    <update id="update" parameterType = "xxx.x.Person" flushCache="true">
        update person t set 
        t.name = #{name},
        t.gender = #{gender},
        t.person_addr = #{personAddr},
        t.birthday = #{birthday}
        where t.person_id = #{personId}
    </update>
flushCache在insert update select delete中都有

  

猜你喜欢

转载自www.cnblogs.com/lonske/p/9026940.html