spring mvc整合redis缓存

1. 新建dynamic web project, spring-redis 目录结构以及jar包列表如下




web.xml添加spring mvc servlet

<servlet>
<servlet-name>mymvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mymvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>


<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>mymvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>



mymvc.xml添加配置如下:

<mvc:annotation-driven />

<context:component-scan base-package="com.yf.controller"></context:component-scan>


    <!-- 试图解析器 -->
    <bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
    
    <!-- 静态资源映射  -->
    <mvc:resources location="/js/" mapping="/js/**" />
    <mvc:resources location="/css/" mapping="/css/**" />
    <mvc:resources location="/img/" mapping="/img/**" />
    <mvc:resources location="/fonts/" mapping="/fonts/**" />
    
    <context:property-placeholder location="classpath*:/application.properties" />
<!-- jedis pool配置 -->


<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!-- property name="maxActive" value="${redis.maxActive}" /-->
<property name="maxIdle" value="${redis.maxIdle}" />
<!-- property name="maxWait" value="${redis.maxWait}" /-->
<property name="testOnBorrow" value="true" />
</bean>


<!-- spring data redis -->
<bean id="jedisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis.host}" />
<property name="port" value="${redis.port}" />
<property name="password" value="${redis.pass}" />
<property name="poolConfig" ref="jedisPoolConfig" />
</bean>


<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory" />
<property name="keySerializer">
<bean
class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean>
</property>
<property name="valueSerializer">
<bean
class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean>
</property>
<property name="hashKeySerializer">
<bean
class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean>
</property>
<property name="hashValueSerializer">
<bean
class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean>
</property>
</bean>


src目录下新建application.properties文件,并添加redis属性

redis.host=192.168.199.124
redis.port=6379
redis.pass=
redis.default.db=0
redis.timeout=100000
redis.maxActive=300 
redis.maxIdle=100
redis.maxWait=1000
redis.testOnBorrow=true

配置完成,下面编写代码。


2. 新建controller类供测试用

@Controller
public class MyController {

@Autowired
RedisTemplate redisTemplate; 

@RequestMapping(value="/")
public String goHome(HttpServletRequest request,HttpServletResponse response){

redisTemplate.opsForValue().set("test","汉字");
System.out.println(redisTemplate.opsForValue().get("test"));

redisTemplate.opsForHash().put("key1", "field1", "value1");
return "home";
}


}




部署运行,浏览器输入 http://localhost:8080/spring-redis/

Console打印,获取的值 “汉字”

进入redis服务器查看




进入RedisDesktopManager查看


自此测试整合完成。


期间碰到问题:

1. 

<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory" />
<property name="keySerializer">
<bean
class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean>
</property>
<property name="valueSerializer">
<bean
class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"></bean>

JdkSerializationRedisSerializer需要改成StringRedisSerializer


否则在redis_cli查看value,会多出\xac\xed\x00\x05t\x00




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值