您的位置:首页 > 数据库 > Redis

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

2018-01-15 14:56 405 查看
其他相关:《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配置:
<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;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息