1. Preparations before the integration of redis and springmvc
1.Redis download address: https://github.com/MSOpenTech/redis/releases
2. Unzip the downloaded compressed package, open a cmd window and use the cd command to switch the directory to E:\test\redis and run redis-server.exe redis.windows.conf
The following screen will appear
After this page appears, reopen a cmd window, do not close the above window,
Switch to the redis directory and run redis-cli.exe -h 127.0.0.1 -p 6379
config get requirepass to get the redis password
config set requirepass zwl Set the connection password to zwl
The auth zwl command is used to check whether the given password matches the password in the configuration file
set myKey abc set key value pair key: myKey value: abc
get myKey to retrieve the key-value pair
3. In this way, redis has been started, and then you need to integrate springmvc and redis
Second, redis and springmvc integration
1.db.properties configuration
#[REDIS]
#######Development environment########
#redis server address (you need to set it according to the actual situation)
redis.host=127.0.0.1
redis.port=6379 #After
configuring the password by yourself
# redis.pass=zwl #Default
configuration
redis.pass=
redis.timeout=-1
redis.maxIdle=100
redis.minIdle=8
redis.maxWait=-1
redis.testOnBorrow=true
2.applicationContext.xml configuration
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http:/ /www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0 .xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd ">
<!-- autoscan -->
<context: component-scan base-package="com.redis"/>
<!-- load file -->
<context:property-placeholder location="classpath:db.properties" />
<!-- jedis configuration -->
< bean id="poolConfig"class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="minIdle" value="${redis.minIdle}" />
<property name="maxWaitMillis" value="${redis.maxWait}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean>
<!-- redis服务器中心 -->
<bean id="connectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="poolConfig" ref="poolConfig" />
<property name="port" value="${redis.port}" />
<property name="hostName" value="${redis.host}" />
<property name="password" value="${redis.pass}" />
<property name="timeout" value="${redis.timeout}" />
</bean>
<!-- redis operation template, object-oriented template-->
<bean id="redisTemplate" class="org. springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<!-- If Serializer is not configured, only String can be used for storage. If it is stored in object type, then An error will be prompted -->
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="valueSerializer">
<bean class=" org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
</property>
</bean>
</beans>
3.redis tool class
package com.redis.common;
import javax.annotation.Resource;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
/**
* @author ZhangWanLong
* @version Creation time: February 19, 2018 2:36:42 pm
* Class description
*/
@Component("redisCache")
public class RedisCacheUtil {
@Resource
private StringRedisTemplate redisTemplate; /** * Add value to Hash * @param key can correspond to database The table name in * @param field can correspond to the unique index in the database table * @param value The value stored in redis */ public void hset(String key, String field, String value) {
if(key == null || "".equals(key)){
return ;
}
redisTemplate.opsForHash().put(key, field, value);
} /** * 从redis中取出值 * @param key * @param field * @return */ public String hget(String key, String field){ if(key == null || "".equals(key)){ return null; } return (String) redisTemplate.opsForHash().get(key, field); } /** * 判断 是否存在 key 以及 hash key * @param key * @param field * @return */
public boolean hexists(String key, String field){
if(key == null || "".equals(key)){
return false;
}
return redisTemplate.opsForHash().hasKey(key, field);
} /** * 查询 key中对应多少条数据 * @param key * @return */ public long hsize(String key) { if(key == null || "".equals(key)){ return 0L; } return redisTemplate.opsForHash().size(key); } /** * 删除 * @param key * @param field */ public void hdel(String key, String field) {
if(key == null || "".equals(key)){
return;
}
redisTemplate.opsForHash().delete(key, field);
}
}
4. Controller layer call
package com.redis.controller;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.redis.common.RedisCacheUtil;
/**
* @author 作者 ZhangWanLong
* @version 创建时间:2018年2月19日 下午2:45:07
* 类说明
*/
@Controller
@RequestMapping("/redis/")
public class RedisDemoController {
@Resource
private RedisCacheUtil redisCache;
// 查询数据
@ResponseBody
@RequestMapping("list")
public String list(HttpServletResponse response, HttpServletRequest request){
String re = redisCache.hget("student", "test");
System.out.println(String.valueOf(re));
return "success";
}
// 添加数据
@ResponseBody
@RequestMapping("add")
public String add(HttpServletResponse response, HttpServletRequest request){
redisCache.hset("student", "test", "小明");
return "success";
}
}
Project source code address: http://download.csdn.net/download/zwl18210851801/10253181