天天看点

SpringBoot简单的直接连接Redis数据库+操作redis数据库存取值

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.GenericToStringSerializer;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
@EnableCaching
public class RedisConfig  {

        @Bean
        public RedisConnectionFactory redisCF(){
            //如果什么参数都不设置,默认连接本地6379端口
            JedisConnectionFactory factory = new JedisConnectionFactory();
            factory.setPort(端口);
           factory.setHostName("IP地址");
        
            factory.setPassword("密码");
         //设置超时时间 
       factory.setTimeout(100000);

            return factory;
        }

  /*  @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory factory){
        //创建一个模板类
        RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
        //将刚才的redis连接工厂设置到模板类中
        template.setConnectionFactory(factory);
        return template;
    }*/



  @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory factory) {
      StringRedisTemplate template = new StringRedisTemplate(factory);
      //定义key序列化方式
      //RedisSerializer<String> redisSerializer = new StringRedisSerializer();//Long类型会出现异常信息;需要我们上面的        自定义key生成策略,一般没必要
      //定义value的序列化方式
      Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
      ObjectMapper om = new ObjectMapper();
      om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
      om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
      jackson2JsonRedisSerializer.setObjectMapper(om);

      // template.setKeySerializer(redisSerializer);
      template.setValueSerializer(jackson2JsonRedisSerializer);
      template.setHashValueSerializer(jackson2JsonRedisSerializer);
      template.afterPropertiesSet();
      return template;
    }
    }      

使用方式: 

以下为伪代码,仅作自学记录。

@Autowired
private RedisConnectionFactory factory;

@RequestMapping("/test1")
public void testRedis() {
    //得到一个连接
    RedisConnection conn = factory.getConnection();
    //conn.set("hello".getBytes(), "world".getBytes());
    conn.set("siwei".getBytes(), "test1".getBytes());
    System.out.println(new String(conn.get("siwei".getBytes())));
}      
@Autowired
StringRedisTemplate template;      
Map<Object, Object> resultMap = template.opsForHash().entries(RedisKey);
// System.out.println("resultMap:" + resultMap);
String value1 = (String) template.opsForHash().get(RedisKey, 里面的Key);      
// System.out.println("value:" + value1);      
JSONObject jsonObjectData = JSON.parseObject(value1);
// System.out.println(jsonObjectData);

String 取数据 = jsonObjectData.getString(字段值名称);      

继续阅读