spring框架和redis结合,需要使用spring-data框架功能
如下:
1.导入spring-data-redis.jar
2.在spring中配置下面组件
<?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:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd">
<!-- 开启IOC注解 -->
<context:component-scan base-package="cn.xdl.jd.redis"/>
<!-- JedisTemplate -->
<bean id="template"
class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="redisConnectionFactory"></property>
</bean>
<!-- 连接池对象 -->
<bean id="redisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<!-- 连接机器名字-->
<property name="hostName" value="localhost"></property>
<!-- 端口号-->
<property name="port" value="6379"></property>
<property name="poolConfig" ref="redisPoolConfig"></property>
</bean>
<!-- 连接池参数 -->
<bean id="redisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!-- 最大连接数-->
<property name="maxTotal" value="100"></property>
<property name="minIdle" value="5"></property>
</bean>
</beans>
建立测试类
测试redis中存取数据
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.data.redis.connection.RedisConnectionFactory;
<!-- 导包要注意是这个包“jedis.JedisConnectionFactory”-->
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import cn.xdl.jd.entity.Address;
import redis.clients.jedis.JedisPoolConfig;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:spring-redis.xml"})
public class TestRedisTemplate {
<!-- 注入RedisTemplate-->
@Resource
private RedisTemplate template;
<!-- 向redis里放数据-->
@Test
public void test1(){
ValueOperations opt = template.opsForValue();
opt.set("template", "RedisTemplate");
}
<!-- 向redis里取数据-->
@Test
public void test2(){
ValueOperations opt = template.opsForValue();
Object value = opt.get("template");
System.out.println(value);
}
}