redis与springmvc整合完整过程,附带源码地址

一、redis与springmvc整合前准备工作

1.redis下载地址:https://github.com/MSOpenTech/redis/releases

2.解压下载的压缩包,打开一个 cmd 窗口 使用cd命令切换目录到 E:\test\redis 运行 redis-server.exe redis.windows.conf 

会出现以下画面


出现这个页面之后,再重新打开一个 cmd 窗口,上面的窗口别关,

切换到redis目录下运行 redis-cli.exe -h 127.0.0.1 -p 6379 


config get requirepass  获取redis密码

config set requirepass zwl 设置连接密码为zwl

auth zwl 命令用于检测给定的密码和配置文件中的密码是否相符

 set myKey abc  设置键值对  key:myKey    value:abc

 get myKey   取出键值对

3.这样redis已经启动,然后需要进行springmvc与redis整合

二、redis与springmvc整合

1.db.properties配置

#[REDIS]
#######开发环境#######
#redis服务器地址(需自己根据实际情况设置)
redis.host=127.0.0.1
redis.port=6379
#自己配置密码后
#redis.pass=zwl
#默认配置
redis.pass=
redis.timeout=-1
  
redis.maxIdle=100
redis.minIdle=8
redis.maxWait=-1
redis.testOnBorrow=true

2.applicationContext.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: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 ">    
    <!-- 自动扫描 -->
    <context:component-scan base-package="com.redis"/>
    <!-- 加载文件 -->
    <context:property-placeholder location="classpath:db.properties" />


    <!-- jedis 配置 -->
    <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操作模板,面向对象的模板 -->
    <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
        <property name="connectionFactory" ref="connectionFactory" />
        <!-- 如果不配置Serializer,那么存储的时候只能使用String,如果用对象类型存储,那么会提示错误 -->
        <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工具类

package com.redis.common;


import javax.annotation.Resource;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;


/** 
* @author 作者 ZhangWanLong 
* @version 创建时间:2018年2月19日 下午2:36:42 
* 类说明 
*/
@Component("redisCache")
public class RedisCacheUtil {
   @Resource
    private StringRedisTemplate  redisTemplate;
    
    /**
     * 向Hash中添加值
     * @param key      可以对应数据库中的表名
      * @param field    可以对应数据库表中的唯一索引
     * @param value    存入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层调用

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";
    }
    
}


项目源码地址:http://download.csdn.net/download/zwl18210851801/10253181



猜你喜欢

转载自blog.csdn.net/zwl18210851801/article/details/79410270