ssm整合redisTemplete
1允趟,引入坐標(biāo)(版本一定要對的上屎慢,不然就可能出現(xiàn)錯誤)
<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"/>
<!--設(shè)置連接池-->
<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>
<!--設(shè)置鏈接屬性-->
<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!M碓洹亭罪!-->
<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發(fā)送請求
image.png
4.3 查看redis,已經(jīng)存進去
image.png