概述
这里只是类和方法的封装,机器如何安装redis服务还得按百度教程去做;
一:pom文件,maven引包
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.1.0</version>
</dependency>
二:Dao层
package com.ye_0809.util.redis;
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 the redisTemplate to set
*/
public void setRedisTemplate(RedisTemplate<K, V> redisTemplate){
this.redisTemplate=redisTemplate;
}
/**
* 获取 RedisSerializer
* <br>------------------------------<br>
*/
protected RedisSerializer<String> getRedisSerializer() {
return redisTemplate.getStringSerializer();
}
}
三:Service层
package com.ye_0809.util.redis;
import java.util.List;
public interface RedisSaveManageService {
/**
* 新增
* <br>------------------------------<br>
* @param user
* @return
*/
boolean add(String key ,String value);
/**
* 批量新增 使用pipeline方式
* <br>------------------------------<br>
* @param list
* @return
*/
boolean add(List<String> key,List<String> values);
/**
* 删除
* <br>------------------------------<br>
* @param key
*/
void delete(String key);
/**
* 删除多个
* <br>------------------------------<br>
* @param keys
*/
void delete(List<String> keys);
/**
* 修改
* <br>------------------------------<br>
* @param user
* @return
*/
boolean update(String key,String value);
/**
* 通过key获取
* <br>------------------------------<br>
* @param keyId
* @return
*/
String get(String key);
}
四:impl实现类:
package com.ye_0809.util.redis;
import java.util.ArrayList;
import java.util.List;
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.stereotype.Service;
@Service
public class RedisSaveManagerServiceImpl extends com.ye_0809.util.redis.AbstractBaseRedisDao<String, String> implements RedisSaveManageService {
/**
* 新增
*<br>------------------------------<br>
* @param user
* @return
*/
@Override
public boolean add( final String key,final String value) {
// TODO Auto-generated method stub
boolean result=redisTemplate.execute(new RedisCallback<Boolean>() {
@Override
public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
// TODO Auto-generated method stub
RedisSerializer<String> serializer=getRedisSerializer();
//序列化
byte[] key2=serializer.serialize(key);
byte[] value2=serializer.serialize(value);
return connection.setNX(key2, value2);
}
});
return result;
}
/**
* 批量新增 使用pipeline方式
*<br>------------------------------<br>
*@param list
*@return
*/
@Override
public boolean add(final List<String> key, final List<String> values) {
boolean result=redisTemplate.execute(new RedisCallback<Boolean>() {
@Override
public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
RedisSerializer<String> serializer=getRedisSerializer();
for (int i = 0; i < values.size(); i++) {
byte[] key2=serializer.serialize(key.get(i));
byte[] value2=serializer.serialize(values.get(i));
connection.setNX(key2, value2);
}
return true;
}
},false,true);
return result;
}
@Override
public void delete(String key) {
List<String> list = new ArrayList<>();
list.add(key);
delete(list);
}
@Override
public void delete(final List<String> keys) {
redisTemplate.execute(new RedisCallback<Boolean>() {
@Override
public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
// TODO Auto-generated method stub
RedisSerializer<String> serializer=getRedisSerializer();
//序列化
for(String k:keys){
byte[] key = serializer.serialize(k);
connection.del(key);
}
return true;
}
});
}
@Override
public boolean update(final String key,final String value) {
// TODO Auto-generated method stub
if (get(key) == null) {
throw new NullPointerException("数据行不存在, key = " + key);
}
boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
public Boolean doInRedis(RedisConnection connection)
throws DataAccessException {
RedisSerializer<String> serializer = getRedisSerializer();
byte[] key2 = serializer.serialize(key);
byte[] value2 = serializer.serialize(value);
connection.set(key2, value2);
return true;
}
});
return result;
}
@Override
public String get(final String key) {
String result = redisTemplate.execute(new RedisCallback<String>() {
public String doInRedis(RedisConnection connection)
throws DataAccessException {
RedisSerializer<String> serializer = getRedisSerializer();
byte[] key2 = serializer.serialize(key);
byte[] value = connection.get(key2);
if (value == null) {
return null;
}
String value2 = serializer.deserialize(value);
return value2;
}
});
return result;
}
}
最后!!! 还要去配置文件配置下。我是配置在spring-mybatis里面了(也可以把它抽出来单独配个xml)
<!-- redis配置加到这里 -->
<context:property-placeholder location="classpath:redis.properties" ignore-unresolvable="true"/>
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${maxIdle}" />
<property name="maxActive" value="${maxActive}" />
<property name="maxWait" value="${maxWait}" />
<property name="testOnBorrow" value="${testOnBorrow}" />
</bean>
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="${host}" p:port="${port}" p:pool-config-ref="poolConfig"/>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="connectionFactory" />
</bean>
<!-- redis配置 end! -->
最后
以上就是自由嚓茶为你收集整理的redis整合(目前是在ssm框架下)的全部内容,希望文章能够帮你解决redis整合(目前是在ssm框架下)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复