spring整合redistemplate

添加依赖

 
    
    
      org.apache.commons
      commons-pool2
      2.4.2
    
    
    
    
      org.springframework.data
      spring-data-redis
      2.1.8.RELEASE
    
    
    
      redis.clients
      jedis
      2.9.1
    
  • SDR 1.x要求JDK 6.0及以上,要求Spring框架4.3.9.RELEASE及以上。
  • Redis 2.6.x及以上。在connectors方面,Spring Redis集成了Jedis

如果不满足要求,会因为版本问题导致某些类加载出问题

配置redis



    
    

    
    
    
    
        
        
        
        
        
        
        
        
    
    
        
        
        
        
        
        
    
    
    
        
        
        
            
        
        
            
        
        
            
        
        
            
        
        
        
    

    
        
    

    
    
        
    
    

创建一个redistemplate工具

package cn.coolservice.oa.utils;

/**
 * @Author: 樊小铭
 * Date: 2020/2/12 14:43
 * @Version:
 * @Description:  工具来源:http://www.jeepxie.net/article/6360.html
 */
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.Resource;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;


public class RedisTemplateUtil {
    @Resource
    private RedisTemplate redisTemplate;

    public void set(String key, Object value) {
        ValueOperations valueOperations = redisTemplate
                .opsForValue();
        valueOperations.set(key, value);
    }

    public Object get(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    public void setList(String key, List value) {
        ListOperations listOperations = redisTemplate
                .opsForList();
        listOperations.leftPush(key, value);
    }

    public Object getList(String key) {
        return redisTemplate.opsForList().leftPop(key);
    }

    public void setSet(String key, Set value) {
        SetOperations setOperations = redisTemplate.opsForSet();
        setOperations.add(key, value);
    }

    public Object getSet(String key) {
        return redisTemplate.opsForSet().members(key);
    }

    public void setHash(String key, Map value) {
        HashOperations hashOperations = redisTemplate
                .opsForHash();
        hashOperations.putAll(key, value);
    }

    public Object getHash(String key) {
        return redisTemplate.opsForHash().entries(key);
    }

    public void delete(String key) {
        redisTemplate.delete(key);
    }

    public void setRedisTemplate(RedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    public RedisTemplate getRedisTemplate() {
        return redisTemplate;
    }
}

你可能感兴趣的:(java实战,java崛起)