码迷,mamicode.com
首页 > 编程语言 > 详细

SpringBoot整合Redis

时间:2018-10-24 22:01:39      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:nio   lib   col   bubuko   http   end   null   exce   技术分享   

结构:

技术分享图片

 pom引入依赖:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.8</version>
        </dependency>

application.yml配置:

spring:
  redis:
    host: 192.168.3.101
    port: 6379
    password: redis-01

Order实体类:

package com.example.bootredis;

public class Order {
    private Integer orderId;
    private String orderName;

    public Integer getOrderId() {
        return orderId;
    }

    public void setOrderId(Integer orderId) {
        this.orderId = orderId;
    }

    public String getOrderName() {
        return orderName;
    }

    public void setOrderName(String orderName) {
        this.orderName = orderName;
    }
}

redis序列化器(默认的jackson)(代码中已经注释掉了)

使用fastjson需要自定义序列化器:

package com.example.bootredis.serializer;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;

import java.nio.charset.Charset;

/**
 * 自定义序列化类
 * @param <T>
 */
public class FastJsonRedisSerializer<T> implements RedisSerializer<T> {

    public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
    private Class<T> clazz;

    public FastJsonRedisSerializer(Class<T> clazz) {
        super();
        this.clazz = clazz;
    }

    @Override
    public byte[] serialize(T t) throws SerializationException {
        if (null == t) {
            return new byte[0];
        }
        return JSON.toJSONString(t, SerializerFeature.WriteClassName).getBytes(DEFAULT_CHARSET);
    }

    @Override
    public T deserialize(byte[] bytes) throws SerializationException {
        if (null == bytes || bytes.length <= 0) {
            return null;
        }
        String str = new String(bytes, DEFAULT_CHARSET);
        return (T) JSON.parseObject(str, clazz);
    }

}

装配序列化器

package com.example.bootredis.config;

import com.example.bootredis.serializer.FastJsonRedisSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfiguration {

    /*@Bean
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<Object, Object>();
        redisTemplate.setConnectionFactory(connectionFactory);
        // 使用jackson2的序列对象
        RedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
        redisTemplate.setDefaultSerializer(genericJackson2JsonRedisSerializer);

        return redisTemplate;
    }*/

    @Bean
    public RedisTemplate<Object, Object> redisTemplate(
            RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<Object, Object> template = new RedisTemplate<>();
        //使用fastjson序列化
        FastJsonRedisSerializer fastJsonRedisSerializer = new FastJsonRedisSerializer(Object.class);
        // value值的序列化采用fastJsonRedisSerializer
        template.setValueSerializer(fastJsonRedisSerializer);
        template.setHashValueSerializer(fastJsonRedisSerializer);
        // key的序列化采用StringRedisSerializer
        template.setKeySerializer(new StringRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }
}

测试类:

package com.example.bootredis;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.Map;

@SpringBootApplication
public class BootRedisApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext
                context = SpringApplication.run(BootRedisApplication.class, args);
        Map<String, RedisTemplate> beansOfType = context.getBeansOfType(RedisTemplate.class);
        System.out.println(beansOfType);
        System.out.println("-----------------");
        RedisTemplate redisTemplate = context.getBean("stringRedisTemplate", RedisTemplate.class);
        redisTemplate.opsForValue().set("test", "test-value");
        Order order = new Order();
        order.setOrderId(100001);
        order.setOrderName("商品订单");
        // redisTemplate.opsForValue().set("order", JSON.toJSONString(order));
        // 通过配置jackson的序列化器操作的
        /*RedisTemplate cRedisTemplate = context.getBean("redisTemplate", RedisTemplate.class);
        //cRedisTemplate.opsForValue().set("order-1", order);
        System.out.println(cRedisTemplate.opsForValue().get("order-1"));*/
        RedisTemplate cRedisTemplate = context.getBean("redisTemplate", RedisTemplate.class);
        cRedisTemplate.opsForValue().set("order-1", order);

        context.close();
    }
}

 

 

SpringBoot整合Redis

标签:nio   lib   col   bubuko   http   end   null   exce   技术分享   

原文地址:https://www.cnblogs.com/lm970585581/p/9845956.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!