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