mybatis + ehcache 配置

mybatis + ehcache 配置

1、引入ehcache jar包,pom.xml添加

    
      net.sf.ehcache
      ehcache
      2.10.4
    

2、添加ehcache配置文件,ehcache.xml



    
    

    
    
    

    

 

3、添加ehcache-config.xml,springmvc集成cache



    
    

    
    
        
    

    
        
        
    

4、将ehcache-config.xml引入spring配置文件中


5、给需要启用ehcache的sql方法添加启用配置

import java.util.Collection;
import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;

public interface UserMapper {
    int insertSelective(User record);

    @Cacheable(value = "userCache")
    User selectByPrimaryKey(Long id);

    @CacheEvict(value = "userCache", allEntries = true)
    int updateByPrimaryKeySelective(User record);

    @CacheEvict(value = "userCache", allEntries = true)
    int updateByPrimaryKeysSelective(@Param("record") User record, @Param("ids") Collection ids);


    /**
     * 删除
     *
     * @param record
     */
    @CacheEvict(value = "userCache", allEntries = true)
    void deleteUser(User record);

}
@CacheEvict 在执行增删改操作时,为了保证缓存和数据库信息一致性,会将缓存信息清空
@Cacheable 在查询时,会先在缓存中查找数据,当缓存中数据不存在时,才会执行之后方法查找数据库

你可能感兴趣的:(mybatis)