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

其他相关:《Springmvc+redis jedis.jar连接方式,JedisSentinelPool哨兵连接池,集群方式》
  《Springmvc+redis jedis.jar连接方式,ShardedJedisPool分片连接池》
特殊需要的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配置:


    
        
        
        
        
    

    

    
        
    

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 { 

    @Autowired 
    protected RedisTemplate redisTemplate; 

    /**
     * 设置redisTemplate
     * @param redisTemplate
     */ 
    public void setRedisTemplate(RedisTemplate redisTemplate) { 
        this.redisTemplate = redisTemplate; 
    } 

    /**
     * 获取 RedisSerializer
     */ 
    protected RedisSerializer 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> implements IBaseDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     *
     */
    @SuppressWarnings("rawtypes")
    public void addMap(final Map map) {
        redisTemplate.execute(new RedisCallback() {
            public Object doInRedis(RedisConnection connection) throws DataAccessException {
                RedisSerializer 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() { 
            public String doInRedis(RedisConnection connection) 
                    throws DataAccessException { 
                RedisSerializer 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() { 
            public Long doInRedis(RedisConnection connection) throws DataAccessException { 
                RedisSerializer 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 list) {
        Long x = redisTemplate.execute(new RedisCallback() {
            public Long doInRedis(RedisConnection connection) throws DataAccessException {
                RedisSerializer 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 getList(final String keyId) {
        List result = redisTemplate.execute(new RedisCallback>() {
            public List doInRedis(RedisConnection connection) throws DataAccessException {
                RedisSerializer serializer = getRedisSerializer();
                byte[] key = serializer.serialize(keyId);
                List list = connection.lRange(key, 0, connection.lLen(key) - 1);
                List resultList = new ArrayList();
                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;
    }

} 
  

你可能感兴趣的:(redis,spring)