SpringMVC 整合 redis3.0x

第一步导入架包

commmons-pool2-2.4.2.jar

javax.annoaion-api-1.3.2.jar

jedis-2.6.2.jar

spring-dae-redis-1.4.4.release.jar

由于Spring版本4.1.7过低这四个架包是我测试多个架包总结出来的

第二步编写redis.properties文件

#############Common Redis configuration
cache.redis.maxIdle=100
cache.redis.minIdle=8
cache.redis.maxActive=20
cache.redis.maxWait=1000
cache.redis.testOnBorrow=true
##############Redis configuration
cache.redis.host=127.0.0.1
cache.redis.port=6379
cache.redis.password=
cache.redis.db=0
cache.redis.timeout=2000
##############
cache.cacheExpire=500

第三步修改applicationcontent.xml文件配置

注意

<!-- 加载属性文件 -->

<context:property-placeholder location="classpath*:resources/*.properties" />

因为spring采用反射机制扫描<context:property-placeholder在只有一个ioc容器·的情况下只会扫秒第一个所以要改为以上形式

修改applicationcontent.xml文件配置
<!-- 加载属性文件 -->

<context:property-placeholder location="classpath*:resources/*.properties" />





<!-- redis服务器中心 -->

<bean id="redisConnectionFactory"

      class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">

    <property name="usePool" value="true"></property>

    <property name="hostName" value="${cache.redis.host}" />

    <property name="port" value="${cache.redis.port}" />

    <property name="password" value="${cache.redis.password}" />

    <property name="timeout" value="${cache.redis.timeout}" />

    <property name="database" value="${cache.redis.db}"></property>

    <constructor-arg index="0" ref="jedisPoolConfig" />

</bean>

<!-- jedis 配置 -->

<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">

    <property name="maxTotal" value="${cache.redis.maxActive}" />

    <property name="maxIdle" value="${cache.redis.maxIdle}" />

    <property name="minIdle" value="${cache.redis.minIdle}"></property>

    <property name="maxWaitMillis" value="${cache.redis.maxWait}" />

    <property name="testOnBorrow" value="${cache.redis.testOnBorrow}" />

</bean>

<!-- redis操作模板,面向对象的模板 -->

<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">

    <property name="connectionFactory" ref="redisConnectionFactory" />

    <!-- 如果不配置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>

第四步添加redis工具类

使用方式注意查看注解,近似与service接口

package runyui.SalarySystem.config;







import org.springframework.data.redis.core.StringRedisTemplate;

import org.springframework.stereotype.Component;



import javax.annotation.Resource;





/**

 * @author 28785

 */

@Component("redisCache")

public class RedisCacheManagerImpl {

    @Resource

    private StringRedisTemplate redisTemplate;





    /**lib

     * 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);

    }

}

第五步测试

在controller类中注入redis工具类

编写请求

前端编写

启动redis,启动项目

点击链接出现如下效果

即为成功

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值