一、先在applicationContext.xml中加入以下bean
<!-- 配置JedisPoolConfig -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!-- 最大空闲数 -->
<property name="maxIdle" value="50" />
<!-- 最大连接数 -->
<property name="maxTotal" value="200" />
<!-- 最大等待时间 -->
<property name="maxWaitMillis" value="30000" />
</bean>
<!-- 配置JedisConnectionFactory -->
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="localhost" />
<property name="port" value="6379" />
<property name="poolConfig" ref="poolConfig" />
</bean>
<!-- 配置Spring RedisTemplate -->
<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" id="jdkSerializationRedisSerializer" />
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" id="stringRedisSerializer" />
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory"/>
<property name="keySerializer" ref="stringRedisSerializer"/>
<!-- <property name="valueSerializer" ref="jdkSerializationRedisSerializer"/> -->
<property name="valueSerializer" ref="stringRedisSerializer"/>
</bean>
二、创建redisTemplate实体类,用于实现封装字符串的redis api
package testMaven2;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisStringCommands.BitOperation;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations= {"classpath:config/applicationContext.xml"})
/**
* JedisTemplate encapsulates Jedis API
* @author Hades
* @date 2018年7月18日
*/
public class CodeTest1 {
@Autowired
ApplicationContext applicationContext;
@Autowired
static RedisTemplate<String, Object> redisTemplate;
@SuppressWarnings("unchecked")
@Test
public void test() {
redisTemplate = applicationContext.getBean(RedisTemplate.class);
try {
// Jedis jedis = new Jedis("127.0.0.1", 6379);
Class<?> cl = Class.forName("testMaven2.CodeTest1");
CodeTest1 codeTest1 = (CodeTest1) cl.newInstance();
Long result = codeTest1.bitCount("key1");
System.out.println(result);
Long result1 = codeTest1.bitOp(BitOperation.AND, "key1", "key2");
System.out.println(result1);
Long result2 = codeTest1.decr("key1");
System.out.println(result2);
Long result3 = codeTest1.decrBy("key1", 1L);
System.out.println(result3);
Boolean result4 = codeTest1.getBit("key1", 0L);
System.out.println(result4);
byte[] bytes = codeTest1.getRange("key3", 0L, 1L);
String str = new String(bytes);
System.out.println(str);
byte[] bytes1 = codeTest1.getSet("key3", "123");
String str1 = new String(bytes1);
System.out.println(str1);
List<byte[]> list = codeTest1.mget("key1", "key2", "key3");
for (byte[] bs : list) {
String str2 = new String(bs);
System.out.println(str2);
}
codeTest1.mset("key4", "1", "key5", "2");
Boolean bool = codeTest1.setNx("key10", "ok");
System.out.println(bool);
codeTest1.setNx("key6", "1");
Map<String, String> map = new HashMap<String, String>();
map.put("key7", "1");
map.put("key8", "1");
Boolean bool2 = codeTest1.mSetNx(map);
System.out.println(bool2);
codeTest1.setEx("key9", 10L, "2");
codeTest1.pSetEx("key11", 10000L, "2");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
public Long bitCount(final String key) {
return redisTemplate.execute(new RedisCallback<Long>() {
@Override
public Long doInRedis(RedisConnection connection) throws DataAccessException {
Long result = connection.bitCount(key.getBytes());
return result;
}
});
}
public Long bitOp(BitOperation op, final String key1, final String key2) {
return redisTemplate.execute(new RedisCallback<Long>() {
@Override
public Long doInRedis(RedisConnection connection) throws DataAccessException {
Long result = connection.bitOp(op, key1.getBytes(), key2.getBytes());
return result;
}
});
}
public Long decr(final String key) {
return redisTemplate.execute(new RedisCallback<Long>() {
@Override
public Long doInRedis(RedisConnection connection) throws DataAccessException {
Long result = connection.decr(key.getBytes());
return result;
}
});
}
public Long decrBy(final String key, final Long value) {
return redisTemplate.execute(new RedisCallback<Long>() {
@Override
public Long doInRedis(RedisConnection connection) throws DataAccessException {
Long result = connection.decrBy(key.getBytes(), value);
return result;
}
});
}
public Boolean getBit(final String key, Long offset) {
return redisTemplate.execute(new RedisCallback<Boolean>() {
@Override
public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
Boolean result = connection.getBit(key.getBytes(), offset);
return result;
}
});
}
public byte[] getRange(final String key, Long begin, Long end) {
return redisTemplate.execute(new RedisCallback<byte[]>() {
@Override
public byte[] doInRedis(RedisConnection connection) throws DataAccessException {
byte[] result = connection.getRange(key.getBytes(), begin, end);
return result;
}
});
}
public byte[] getSet(final String key, String value) {
return redisTemplate.execute(new RedisCallback<byte[]>() {
@Override
public byte[] doInRedis(RedisConnection connection) throws DataAccessException {
byte[] result = connection.getSet(key.getBytes(), value.getBytes());
return result;
}
});
}
public List<byte[]> mget(final String key, final String key1, final String key2) {
return redisTemplate.execute(new RedisCallback<List<byte[]>>() {
@Override
public List<byte[]> doInRedis(RedisConnection connection) throws DataAccessException {
List<byte[]> list = connection.mGet(key.getBytes(), key1.getBytes(), key2.getBytes());
return list;
}
});
}
public void mset(final String key, String value, final String key1, String value1) {
redisTemplate.execute(new RedisCallback<Void>() {
@Override
public Void doInRedis(RedisConnection connection) throws DataAccessException {
Map<byte[], byte[]> map = new HashMap<byte[], byte[]>();
map.put(key.getBytes(), value.getBytes());
map.put(key1.getBytes(), value1.getBytes());
connection.mSet(map);
return null;
}
});
}
public Boolean setNx(final String key, String value) {
return redisTemplate.execute(new RedisCallback<Boolean>() {
@Override
public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
Boolean bool = connection.setNX(key.getBytes(), value.getBytes());
return bool;
}
});
}
public Boolean mSetNx(Map<String, String> map) {
return redisTemplate.execute(new RedisCallback<Boolean>() {
@SuppressWarnings("unchecked")
@Override
public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
Map<byte[], byte[]> byteMap = new HashMap<byte[], byte[]>();
Iterator<?> iterator = map.entrySet().iterator();
while(iterator.hasNext()) {
Map.Entry<String, String> entry = (Map.Entry<String, String>) iterator.next();
String key = entry.getKey();
String value = entry.getValue();
byteMap.put(key.getBytes(), value.getBytes());
}
Boolean bool = connection.mSetNX(byteMap);
return bool;
}
});
}
public void setEx(final String key, Long seconds, String value) {
redisTemplate.execute(new RedisCallback<Void>() {
@Override
public Void doInRedis(RedisConnection connection) throws DataAccessException {
connection.setEx(key.getBytes(), seconds, value.getBytes());
return null;
}
});
}
public void pSetEx(final String key, Long milliseconds, String value) {
redisTemplate.execute(new RedisCallback<Void>() {
@Override
public Void doInRedis(RedisConnection connection) throws DataAccessException {
connection.pSetEx(key.getBytes(), milliseconds, value.getBytes());
return null;
}
});
}
}