Spring通过“注释Annotation”集成 EHCahe

Spring中提供了JDBC包装功能,对于Cache可由程序员选择。

通过注释功能使用EHCahe非常简单

首先,配置Spring的配置文件加入:

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	 xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring  
    	   http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
           http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
	<!-- Configures the @Controller programming model -->
	<ehcache:annotation-driven cache-manager="ehCacheManager" /> 
	<bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">  
        <property name="configLocation" value="classpath:ehcache.xml" />  
    </bean> 

其次,完成EHCahe的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
	xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="false">    
	<diskStore path="java.io.tmpdir/EhCacheSpringAnnotationsExampleApp" />      
	<defaultCache eternal="false" 
		maxElementsInMemory="1000"        
		overflowToDisk="false" 
		diskPersistent="false" 
		timeToIdleSeconds="0"      
		timeToLiveSeconds="600"
		memoryStoreEvictionPolicy="LRU"/>  
		    
	<cache name="metaColumnCache" eternal="false"       
		maxElementsInMemory="100" 
		overflowToDisk="false" 
		diskPersistent="false"       
		timeToIdleSeconds="0" 
		timeToLiveSeconds="1200"      
		memoryStoreEvictionPolicy="LRU" /> 
	<cache name="ppValueCache" eternal="false"       
		maxElementsInMemory="100" 
		overflowToDisk="false" 
		diskPersistent="false"       
		timeToIdleSeconds="0" 
		timeToLiveSeconds="1200"      
		memoryStoreEvictionPolicy="LRU" /> 
               。。。可以创建多个cache
</ehcache>

第三,把注释加到Dao的实现类中,如:


public class PPValueDAO extends BaseDAO implements IPPValueDAO {
	

	@Transactional(propagation = Propagation.REQUIRED)
	@TriggersRemove(cacheName="ppValueCache",removeAll=true)
	public int delete(PPValue persistentInstance) {
		String sql = "delete from " + TABLE + " where id=:id";
		int id = persistentInstance.getId();
		Map paramMap = new HashMap();
		paramMap.put("id", id);
		return getNamedParameterJdbcTemplate().update(sql, paramMap);
	}

	

	private RowMapper adminsRowMapper = new RowMapper() {
		public Object mapRow(ResultSet rs, int arg1) throws SQLException {
			PPValue ppValue = new PPValue();
			ppValue.setId(rs.getInt("id"));
			ppValue.setRegId(rs.getInt("regId"));
			ppValue.setPpValue(rs.getString("ppValue"));
			ppValue.setPpId(rs.getInt("ppId"));
			return ppValue;
		}
	};
	@Cacheable(cacheName="ppValueCache")
	public List<PPValue> findByProperty(String propertyName, Object value) {
		String sql = "select * from " + TABLE + " where " + propertyName + "=:"
				+ propertyName;
		Map paramMap = new HashMap();
		paramMap.put(propertyName, value);
		return (List<PPValue>) getNamedParameterJdbcTemplate().query(sql,
				paramMap, adminsRowMapper);
	}

	@Transactional(propagation = Propagation.REQUIRED)
	@TriggersRemove(cacheName="ppValueCache",removeAll=true)
	public int save(PPValue transientInstance) {
		final PPValue one = findByRegIdPPId(transientInstance.getRegId(), transientInstance.getPpId());
		if(one!=null && one.getId()>0) {
			one.setPpValue(transientInstance.getPpValue());
			return this.update(one);
		}
		String sql = "insert into " + TABLE + " ( " + FIELDS + " ) values ( "
				+ SAVED_VALUES + " )";
		KeyHolder keyHolder = new GeneratedKeyHolder();
		SqlParameterSource ps = new BeanPropertySqlParameterSource(
				transientInstance);
		getNamedParameterJdbcTemplate().update(sql, ps, keyHolder);
		return keyHolder.getKey().intValue();
	}

	@Transactional(propagation = Propagation.REQUIRED)
	@TriggersRemove(cacheName="ppValueCache",removeAll=true)
	public int update(PPValue detachedInstance) {
		String sql = "update " + TABLE + " set " + UPDATED_VALUES
				+ " where id=:id";
		SqlParameterSource ps = new BeanPropertySqlParameterSource(
				detachedInstance);
		return getNamedParameterJdbcTemplate().update(sql, ps);
	}

	@Transactional(propagation = Propagation.REQUIRED)
	@TriggersRemove(cacheName="ppValueCache",removeAll=true)
	public int deleteByPPId(int ppId) {
		String sql = "delete from " + TABLE + " where ppId=:ppId";
		Map paramMap = new HashMap();
		paramMap.put("ppId", ppId);
		return getNamedParameterJdbcTemplate().update(sql, paramMap);
	}
	@Cacheable(cacheName="ppValueCache")
	public PPValue findByRegIdPPId(int regId, int ppId) {
		String sql = "select * from " + TABLE + " where regId=:regId and ppId=:ppId";
		Map paramMap = new HashMap();
		paramMap.put("regId", regId);
		paramMap.put("ppId", ppId);
		List<PPValue> result = (List<PPValue>) getNamedParameterJdbcTemplate().query(sql,
				paramMap, adminsRowMapper);
		if(result!=null && result.size()>0)return result.get(0);
		return new PPValue();
	}
	
}

最后,下载Jar包:http://code.google.com/p/ehcache-spring-annotations/downloads/detail?name=ehcache-spring-annotations-1.1.3-dep.tar.gz&can=2&q=

点击打开链接


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值