redis.properties
redis.maxTotal=200
redis.maxWaitMillis=1000
redis.timeout=3000
redis.testOnBorrow=true
redis.node1.host=127.0.0.1
redis.node1.port=6379
redis.dbIndex=1
applicationContext-redis.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 配置数据源 redis -->
<!-- 配置JedisPoolConfig实例 -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${redis.maxTotal}" />
<property name="maxWaitMillis" value="${redis.maxWaitMillis}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean>
<!-- 配置JedisConnectionFactory -->
<bean id="jedisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis.node1.host}" />
<property name="port" value="${redis.node1.port}" />
<property name="database" value="${redis.dbIndex}" />
<property name="timeout" value="${redis.timeout}"></property>
<property name="poolConfig" ref="poolConfig" />
</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.StringRedisSerializer" />
</property>
<property name="hashKeySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<property name="hashValueSerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<!--开启事务报错 资源不够 -->
<property name="enableTransactionSupport" value="false"></property>
</bean >
</beans>
controller:
```
@Autowired
private RedisTemplate RedisTemplate;
@Autowired
private JedisConnectionFactory jedisConnectionFactory;
@RequestMapping(value = "RedisDb", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
@ResponseBody
public String RedisDb(){
//获取redis默认db1中的数据
Object object = RedisTemplate.opsForValue().get("KW");
System.out.println(object);
//将db1切换到db0
jedisConnectionFactory.setDatabase(0);
Object object2 = RedisTemplate.opsForValue().get("CountAll");
System.out.println(object2);
return null;
}