ssm整合redisTemplete
1,引入坐标(版本一定要对的上,不然就可能出现错误)
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.3.0.RELEASE</version>
</dependency>
2,redis.properties
redis.host=192.168.240.135
redis.port=6379
redis.password=***
redis.timeout=100000
redis.maxTotal=300
redis.maxIdle=200
redis.maxWait=10000
redis.testOnBorrow=true
redis.testOnReturn=true
# 默认缓存失效时间
defaultCacheExpireTime=3600
3,springRedis.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"
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">
<!--扫描redis配置文件-->
<context:property-placeholder ignore-unresolvable="true" location="classpath:redis.properties"/>
<!--设置连接池-->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}"/>
<property name="maxTotal" value="${redis.maxTotal}" />
<property name="maxWaitMillis" value="${redis.maxWait}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
<property name="testOnReturn" value="${redis.testOnReturn}" />
</bean>
<!--设置链接属性-->
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:hostName="${redis.host}"
p:port="${redis.port}"
p:password="${redis.password}"
p:pool-config-ref="poolConfig"
p:timeout="100000"/>
<!-- Jedis模板配置 -->
<!-- <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">-->
<!-- <property name="connectionFactory" ref="connectionFactory" />-->
<!-- -->
<!-- </bean>-->
<bean id="redisTemplate"
class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<!-- 如果不配置Serializer,那么存储的时候只能使用String,如果用对象类型存储,那么会提示错误 can't cast to String!!!-->
<property name="keySerializer">
<!--对key的默认序列化器。默认值是StringSerializer-->
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<!--是对value的默认序列化器,默认值是取自DefaultSerializer的JdkSerializationRedisSerializer。-->
<property name="valueSerializer">
<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
</property>
<!--存储Map时key需要的序列化配置-->
<property name="hashKeySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<!--存储Map时value需要的序列化配置-->
<property name="hashValueSerializer">
<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
</property>
</bean>
</beans>
3,引入到主applicationContext.xml
<!-- redis -->
<import resource="classpath:springRedis.xml"/>
4,测试
4.1 Controller 添加一段代码
@Controller
@ResponseBody
@RequestMapping("/admin")
@CrossOrigin
public class AdminController {
@Autowired
AdminService adminService;
@Autowired
JWTUtils jwt;
@Autowired
RedisTemplate redisTemplate;
@RequestMapping(value = "/login", method = RequestMethod.POST)
DataReturn login(@RequestBody Admin admin) {
redisTemplate.opsForValue().append("teskinfly","handsome");//在这里测试
if (!adminService.checkPwd(admin.getAName(), admin.getAPwd()))
return new DataReturn(ReturnCode.FAIL);
Admin byName = adminService.findByName(admin.getAName());
String s = jwt.create(byName.getAId(), byName.getAName());
return new DataReturn(ReturnCode.SUCCESS, s);
}
}
4.2 用postman发送请求
4.3 查看redis,已经存进去