Springmvc+redis spring-data-redis.jar连接方式

特殊需要的jar包:
redis-2.6.1.jar;
spring-data-redis-1.4.1.RELEASE.jar

redis.properties配置:
redis.maxIdle=300
redis.maxTotal=600
redis.maxWaitMillis=1000
redis.testOnBorrow=true
redis.minIdle=1
redis.testOnReturn=true
redis.testWhileIdle=true

redis.host=*.*.*.*
redis.port=26379
redis.pass=****

applicationContext.xml配置:
<context:property-placeholder location="classpath:redis.properties" />

    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxIdle" value="${redis.maxIdle}" />
        <property name="maxTotal" value="${redis.maxTotal}" />
        <property name="maxWaitMillis" value="${redis.maxWaitMillis}" />
        <property name="testOnBorrow" value="${redis.testOnBorrow}" />
    </bean>

    <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
        p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}"  p:pool-config-ref="poolConfig"/>

    <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
        <property name="connectionFactory"   ref="connectionFactory" />
    </bean>

AbstractBaseRedisDao.java代码:
package com.tg.redis.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;

public abstract class AbstractBaseRedisDao<K, V> { 

    @Autowired 
    protected RedisTemplate<K, V> redisTemplate; 

    /**
     * 设置redisTemplate
     * @param redisTemplate
     */ 
    public void setRedisTemplate(RedisTemplate<K, V> redisTemplate) { 
        this.redisTemplate = redisTemplate; 
    } 

    /**
     * 获取 RedisSerializer
     */ 
    protected RedisSerializer<String> getRedisSerializer() { 
        return redisTemplate.getStringSerializer(); 
    } 
}

BaseDao.java代码:
package com.tg.redis.dao;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class BaseDao extends AbstractBaseRedisDao<String, Map<String, Object>> implements IBaseDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     *
     */
    @SuppressWarnings("rawtypes")
    public void addMap(final Map map) {
        redisTemplate.execute(new RedisCallback<Object>() {
            public Object doInRedis(RedisConnection connection) throws DataAccessException {
                RedisSerializer<String> serializer = getRedisSerializer();
                Set entries = map.entrySet();
                if(entries != null) {
                    Iterator iterator = entries.iterator();
                    while(iterator.hasNext()) {
                        Entry entry = (Entry) iterator.next();
                        byte[] key  = serializer.serialize((String) entry.getKey()); 
                        byte[] name = serializer.serialize((String) entry.getValue()); 
                        connection.setNX(key, name);
                    }
                }
                return null;
            } 
        }); 
    }

    public void addDb(String code, String name, String value) {
        String sql = "INSERT INTO `redis_db`.`tb_configure_his` (`id`, `code`, `name`, `value`, `flagDate`) " +
                "VALUES (NULL, '" + code + "', '" + name + "', '" + value + "', now());";
        jdbcTemplate.execute(sql);
    }

    /** 
     * 通过key获取
     * @param keyId
     * @return
     */ 
    public String get(final String keyId) { 
        String result = redisTemplate.execute(new RedisCallback<String>() { 
            public String doInRedis(RedisConnection connection) 
                    throws DataAccessException { 
                RedisSerializer<String> serializer = getRedisSerializer(); 
                byte[] key = serializer.serialize(keyId); 
                byte[] value = connection.get(key); 
                if (value == null) { 
                    return null; 
                } 
                String name = serializer.deserialize(value); 
                return name; 
            } 
        }); 
        return result; 
    }

    public Long setList(final String listName, final String listValue) {
        Long x = redisTemplate.execute(new RedisCallback<Long>() { 
            public Long doInRedis(RedisConnection connection) throws DataAccessException { 
                RedisSerializer<String> serializer = getRedisSerializer(); 
                byte[] key = serializer.serialize(listName); 
                byte[] value = serializer.serialize(listValue); 
                Long x = connection.lPush(key, value);
                return x;
            } 
        }); 
        return x; 
    }

    public Long setList(final String listName, final List<String> list) {
        Long x = redisTemplate.execute(new RedisCallback<Long>() {
            public Long doInRedis(RedisConnection connection) throws DataAccessException {
                RedisSerializer<String> serializer = getRedisSerializer();
                byte[] key = serializer.serialize(listName);
                Long x = 0l;
                if (list != null && list.size() > 0) {
                    for (int i = 0; i < list.size(); i++) {
                        byte[] value = serializer.serialize(list.get(i));
                        x += connection.lPush(key, value);
                    }
                }
                return x;
            }
        });
        return x; 
    }

    public List<String> getList(final String keyId) {
        List<String> result = redisTemplate.execute(new RedisCallback<List<String>>() {
            public List<String> doInRedis(RedisConnection connection) throws DataAccessException {
                RedisSerializer<String> serializer = getRedisSerializer();
                byte[] key = serializer.serialize(keyId);
                List<byte[]> list = connection.lRange(key, 0, connection.lLen(key) - 1);
                List<String> resultList = new ArrayList<String>();
                if (list != null && list.size() > 0) {
                    for (int i = 0; i < list.size(); i++) {
                        byte[] value = list.get(i);
                        if (value == null) {
                            return null;
                        }
                        String obj = serializer.deserialize(value);
                        resultList.add(obj);
                    }
                }
                return resultList;
            }
        });
        return result;
    }

}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值