02_redis在ssm中的接入



demo需求:

完成redis和spring的整合

demo来由:

随着nosql的市场不断扩大,小叶子怎么能不赶个潮流,所以本次demo主要目的就是将redis接入spring并完成简单的存取。

demo所用jar包(maven依赖):

<jedis.version>2.9.0</jedis.version>
	<redis.spring.version>1.7.2.RELEASE</redis.spring.version>
	<jackson.version>2.9.0</jackson.version>
	
	<!-- jedis -->
	<dependency>
		<groupId>redis.clients</groupId>
		<artifactId>jedis</artifactId>
		<version>${jedis.version}</version>
	</dependency>

	<!-- redis-spring -->
	<dependency>
		<groupId>org.springframework.data</groupId>
		<artifactId>spring-data-redis</artifactId>
		<version>${redis.spring.version}</version>
	</dependency>

	<!-- jackson 依赖 spring-data-redis必备 -->
	<dependency>
		<groupId>com.fasterxml.jackson.core</groupId>
		<artifactId>jackson-core</artifactId>
		<version>${jackson.version}</version>
	</dependency>
	<dependency>
		<groupId>com.fasterxml.jackson.core</groupId>
		<artifactId>jackson-databind</artifactId>
		<version>${jackson.version}</version>
	</dependency>
	<dependency>
		<groupId>com.fasterxml.jackson.core</groupId>
		<artifactId>jackson-annotations</artifactId>
		<version>${jackson.version}</version>
	</dependency>

demo主要代码:

首先说明一点,spring目前用<context:property-placeholder  />装填外部属性文件只会加载一次,意思就是后面的都是不执行的,所以要引入外部属性文件,只能引一次,是以数据库的连接属性和redis的连接属性只能放在一个属性文件

db-redis.properties

dataSource.driverClass=com.mysql.jdbc.Driver
dataSource.jdbcUrl=jdbc:mysql://localhost:3306/tinydemo?useUnicode=true&characterEncoding=UTF-8
dataSource.user=root
dataSource.password=123456
dataSource.maxPoolSize=20
dataSource.maxIdleTime = 1000
dataSource.minPoolSize=6
dataSource.initialPoolSize=5
redis.host=127.0.0.1
redis.port=6379
redis.pass=
redis.maxIdle=300
redis.maxActive=600
redis.maxWait=1000
redis.testOnBorrow=true

applicationContext-core.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
			            http://www.springframework.org/schema/beans/spring-beans.xsd
			            http://www.springframework.org/schema/context
			            http://www.springframework.org/schema/context/spring-context-4.2.xsd
			            http://www.springframework.org/schema/mvc
			            http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
			            http://www.springframework.org/schema/tx
			            http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
			            http://mybatis.org/schema/mybatis-spring 
			            http://mybatis.org/schema/mybatis-spring.xsd ">

	<!-- mybatis:scan会将org.fkit.mapper包里的所有接口当作mapper配置,之后可以自动引入mapper类 -->
	<mybatis:scan base-package="com.xyz.*.mapper" />


	<!-- 扫描org.fkit包下面的java文件,有Spring的相关注解的类,则把这些类注册为Spring的bean -->
	<context:component-scan base-package="com.xyz" />

	<!-- 使用PropertyOverrideConfigurer后处理器加载数据源参数 -->
	<context:property-placeholder location="classpath:db-redis.properties" />

	<!-- 配置c3p0数据源 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
		destroy-method="close" p:driverClass="${dataSource.driverClass}" p:jdbcUrl="${dataSource.jdbcUrl}"
		p:user="${dataSource.user}" p:password="${dataSource.password}" />

	<!-- 配置SqlSessionFactory,org.mybatis.spring.SqlSessionFactoryBean是Mybatis社区开发用于整合Spring的bean -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<property name="mapperLocations" value="classpath:com/xyz/*/mapper/*.xml" />
		<property name="configLocation" value="classpath:conf/mybatis/mybatis-conf.xml" />
	</bean>

	<!-- JDBC事务管理器 -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
		p:dataSource-ref="dataSource" />

	<!-- 启用支持annotation注解方式事务管理 -->
	<tx:annotation-driven transaction-manager="transactionManager" />

</beans>

applicationContext-redis.xml

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">
       
    <!-- <context:property-placeholder location="classpath:redis.properties" /> -->

	<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
		<property name="maxIdle" value="${redis.maxIdle}" />
		<property name="maxTotal" value="${redis.maxActive}" />
		<property name="maxWaitMillis" value="${redis.maxWait}" />
		<property name="testOnBorrow" value="${redis.testOnBorrow}" />
	</bean>

	<bean id="jedisConnectionFactory"
		class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
		<property name="hostName" value="${redis.host}" />
		<property name="port" value="${redis.port}" />
		<property name="poolConfig" ref="jedisPoolConfig" />
	</bean>

	<!--redis操作模版,使用该对象可以操作redis -->
	<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
		<property name="connectionFactory" ref="jedisConnectionFactory" />
		<!--如果不配置Serializer,那么存储的时候缺省使用String,如果用User类型存储,那么会提示错误User can't cast 
			to String!! -->
		<property name="keySerializer">
			<bean
				class="org.springframework.data.redis.serializer.StringRedisSerializer" />
		</property>
		<property name="valueSerializer">
			<bean
				class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer" />
		</property>
		<property name="hashKeySerializer">
			<bean
				class="org.springframework.data.redis.serializer.StringRedisSerializer" />
		</property>
		<property name="hashValueSerializer">
			<bean
				class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer" />
		</property>
		<!--开启事务 -->
		<property name="enableTransactionSupport" value="true"></property>
	</bean>

</beans>

模拟应用

.
    @Autowired
	private PagesMapper pagesMapper;
	
	@Autowired  
    private RedisTemplate<String, List<Pages>> redisTemplate;//redis操作模板  

	@Override
	public PageInfo<Pages> getPages(int current, int limit) {
		PageHelper.startPage(current, limit);
		PageInfo<Pages> pageInfo = new PageInfo<Pages>(pagesMapper.selectAllPages());
		
		//模拟redis的存取
		redisTemplate.opsForValue().set("当前"+current+"页", pageInfo.getList());
		List<Pages> list = redisTemplate.opsForValue().get("当前"+current+"页");
		System.out.println(list.size());
		for (Pages pages : list) {
			System.out.println(pages.getFirst());
		}
		
		return pageInfo;
	}

demo资源位置:

svn://106.15.229.200/Javaweb/tiny_demo(com.xyz.paging) 用户temp/密码temp