1、pom文件引入jar包
<!-- redis -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.7.2.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
2、配置spring-redis文件
这里我注释了使用密码那一项,如果你需要使用密码请取消注释
<?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:cache="http://www.springframework.org/schema/cache"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:redis="http://www.springframework.org/schema/redis" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
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.0.xsd
http://www.springframework.org/schema/redis http://www.springframework.org/schema/redis/spring-redis-1.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- 载入redis.properties,这里要特别注意,如果有多个properties文件,必须用逗号分开,不能写成两个 <context:property-placeholder/> -->
<context:property-placeholder location="classpath:redis.properties" />
<!-- 配置JedisPoolConfig连接池-->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${spring.redis.pool.maxIdle}"></property>
<property name="minIdle" value="${spring.redis.pool.minIdle}"></property>
<property name="maxWaitMillis" value="${spring.redis.pool.maxWaitMillis}"></property>
</bean>
<!-- 配置jedis连接工厂 -->
<bean id="connectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="poolConfig" ref="poolConfig"></property>
<property name="hostName" value="${spring.redis.host}"></property>
<property name="port" value="${spring.redis.port}"></property>
<!-- <property name="password" value="${spring.redis.password}"></property> -->
<property name="database" value="${spring.redis.database}"></property>
<property name="timeout" value="${spring.redis.timeout}"></property>
</bean>
<!-- 配置RedisTemplate -->
<bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer" />
<bean id="cacheRedisTemplate" class="org.springframework.data.redis.core.RedisTemplate" >
<property name="connectionFactory" ref="connectionFactory" />
<property name="keySerializer" ref="stringRedisSerializer" />
<property name="hashKeySerializer" ref="stringRedisSerializer" />
<property name="valueSerializer" ref="stringRedisSerializer" />
<property name="hashValueSerializer" ref="stringRedisSerializer" />
</bean>
</beans>
3、配置redis.properties文件
# Redis Setting
# Redis默认有16个库,序号是0-15,默认是选中的是0号数据库
spring.redis.database=0
# Redis服务器地址
spring.redis.host=127.0.0.1
# Redis服务器连接端口,默认是6379
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
# spring.redis.password=你的密码
# 连接池最大阻塞等待时间(使用负值表示没有限制),根据实际情况修改
spring.redis.pool.maxWaitMillis=-1
# 连接池中的最大空闲连接,根据实际情况修改
spring.redis.pool.maxIdle=8
# 连接池中的最小空闲连接,根据实际情况修改
spring.redis.pool.minIdle=0
# 连接超时时间(毫秒),根据实际情况修改
spring.redis.timeout=2000
4、spring集成spring-redis文件
方式一:在spring配置文件中加入:
<import resource="classpath:spring-redis.xml"/>
方式二:直接将spring-redis的东西写到spring配置文件里。
5、编写controller
package com.hrx.controller;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
*
* @ClassName: OrdersController
* @Description: TODO
* @author: HRX
* @date: 2018年12月5日 下午9:29:27
*/
@Controller()
@RequestMapping("/ordersController")
public class OrdersController {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
public static final String SUCCESS = "success";
public static final String FAIL = "fail";
@ResponseBody
@RequestMapping("/insert")
public String insert(String key,String value){
try{
System.out.println(key+"---------------"+value);
redisTemplate.opsForValue().set(key, value,60*10,TimeUnit.SECONDS);
return SUCCESS;
}catch(Exception e){
e.printStackTrace();
return e.getMessage();
}
}
@ResponseBody
@RequestMapping("/find")
public String find(String key){
try{
String value = (String) redisTemplate.opsForValue().get(key);
System.out.println(key+":"+value);
return value;
}catch(Exception e){
e.printStackTrace();
return e.getMessage();
}
}
@ResponseBody
@RequestMapping("/delete")
public String delete(String key){
try{
redisTemplate.delete(key);
return SUCCESS;
}catch(Exception e){
e.printStackTrace();
return e.getMessage();
}
}
@ResponseBody
@RequestMapping("/update")
public String update(String key,String value){
try{
if(find(key) != null){
redisTemplate.opsForValue().set(key, value,60*10,TimeUnit.SECONDS);
return SUCCESS;
}
else
return FAIL;
}catch(Exception e){
e.printStackTrace();
return e.getMessage();
}
}
}
6、启动项目,进行测试。