1、在pom.xml中增加以下节点
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.7.4.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
2、配置redis相关参数
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!-- 最大空闲连接数 -->
<property name="maxIdle" value="100" />
<!-- 最大连接数 -->
<property name="maxTotal" value="500" />
<!-- 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true -->
<property name="blockWhenExhausted" value="true" />
<!-- 获取连接时的最大等待毫秒数(如果设置为阻塞时BlockWhenExhausted),如果超时就抛异常, 小于零:阻塞不确定的时间, 默认-1 -->
<property name="maxWaitMillis" value="300" />
<!-- 在获取连接的时候检查有效性, 默认false -->
<property name="testOnBorrow" value="true" />
</bean>
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<!-- 主机 -->
<property name="hostName" value="192.168.93.128" />
<!-- 端口,默认端口6379,配置文件中修改 -->
<property name="port" value="6379"/>
<!-- 密码,默认无密码,在redis配置中增加 -->
<property name="password" value="admin" />
<!-- 引用jedisPoolConfig配置 -->
<property name="poolConfig" ref="jedisPoolConfig" />
<!-- 使用连接池 -->
<property name="usePool" value="true"/>
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory" />
<!-- 字符串序列化 -->
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<!-- 对象序列化 -->
<property name="valueSerializer">
<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
</property>
<!-- 字符串序列化 -->
<property name="hashKeySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<!-- 对象序列化 -->
<property name="hashValueSerializer">
<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
</property>
</bean>
3、在spring配置文件中导入redis配置文件
<import resource="./spring-redis.xml"/>
4、在web.xml文件加载spring配置文件
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml
</param-value>
</context-param>
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>com.ctjy.wxmis.http.servlet.CustomerDispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:mvc.xml,classpath:dubbo.xml,classpath:mq.xml
</param-value>
</init-param>
<init-param>
<param-name>forward</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
5、java代码中调用redis模版(下面代码只演示key-value类型,根据需要调整)
@Controller
@RequestMapping("/redis")
public class RedisController {
@Autowired
private RedisTemplate redisTemplate;
@ResponseBody
@RequestMapping("/redis")
public String redis() {
ValueOperations<String, Object> value = redisTemplate.opsForValue();
try {
value.set("myredis", "myredis");
} catch (Exception e) {
System.out.println(e.getMessage());
}
System.out.println(value.get("myredis"));
return "test redis";
}
}