springmvc集成redis

3 篇文章 0 订阅
1 篇文章 0 订阅

1.创建一个redis-context.xml文件:内容如下

<span style="font-size:18px;"><beansxmlns="http://www.springframework.org/schema/beans" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p" 
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
      http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/tx 
      http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-3.0.xsd
         ">
  
  <!-- scannerredis properties  --> 
  <context:property-placeholderlocation="classpath:redis.properties"/>
  
  <beanid="connectionFactory"class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"  
    p:host-name="${redis.host}" 
    p:port="${redis.port}" 
    p:password="${redis.password}"  
    p:pool-config-ref="poolConfig"/>
    
  <beanid="poolConfig"class="redis.clients.jedis.JedisPoolConfig">  
    <propertyname="maxIdle"value="${redis.maxIdle}"/>  
    <propertyname="maxTotal"value="${redis.maxTotal}"/>  
    <propertyname="maxWaitMillis"value="${redis.maxWaitMillis}"/>  
    <propertyname="testOnBorrow"value="${redis.testOnBorrow}"/>  
  </bean>  
    
    <!-- <bean id = "connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
  <constructor-arg index="0"ref="poolConfig"/>
  <property name="hostName" value="${redis.host}"/>
  <property name="port" value="${redis.port}"/>
  <property name="timeout" value="${redis.timeout}"/>
  <property name="password" value="${redis.password}"/>
  </bean> -->
    
  <beanid="redisTemplate"class="org.springframework.data.redis.core.StringRedisTemplate">  
    <propertyname="connectionFactory"  ref="connectionFactory"/>  
    <propertyname="keySerializer">
<beanclass="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<propertyname="valueSerializer">
<beanclass="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</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> 
   
</beans></span>



2.再创建一个redis.properties文件:内容如下

<span style="font-size:18px;"># Redis settings
redis.host=127.0.0.1
redis.port=6379
redis.password=
  
redis.maxIdle=300
redis.maxTotal=600
redis.maxWaitMillis=1000
redis.testOnBorrow=true</span>



3.在springMVC-servlet.xml中添加下面内容

 <!-- 引入同文件夹下的redis属性配置文件 -->

    <importresource="redis-context.xml"/>


4.新建java类文件,用于对redis操作

<span style="font-size:18px;">package com.pam.redis;


import java.io.Serializable;


import javax.annotation.Resource;


import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;


@Component("redisBase")
public class RedisBase{

@Resource(name="redisTemplate")
private RedisTemplate<Serializable, Serializable> redisTemplate;

/**
* 获取数据
* @param key
* @return
*/
public String getInfo(final String key){
return (String) this.redisTemplate.execute(new RedisCallback<Object>() {
            @Override
            public Object doInRedis(RedisConnection connection) throws DataAccessException {
                byte[] keys = redisTemplate.getStringSerializer().serialize(key);
                if (connection.exists(keys)) {
                    byte[] value = connection.get(keys);
                    String valueStr = redisTemplate.getStringSerializer().deserialize(value);
                    return valueStr;
                }
                return null;
            }
        });
}

/**
* 存储数据(设定过期时间)
* @param key
* @param value
* @param timeout 单位秒,
*/
public void setAndUpdateInfo(String key,String value,int timeout){
this.redisTemplate.execute(new RedisCallback<Object>() {


            @Override
            public Object doInRedis(RedisConnection connection) throws DataAccessException {
            connection.pSetEx(redisTemplate.getStringSerializer().serialize(key), 60*1000, redisTemplate.getStringSerializer().serialize(value));
                return null;
            }
        });
}

/**
* 删除数据
* @param key
*/
public void delete(String key){
this.redisTemplate.delete(key);
}
}</span>



注:内有已注释内容

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值