springboot2.0整合kafka_spring与mybatis整合

springboot2.0整合kafka_spring与mybatis整合首先引入POM依赖 <!–nosql数据库–> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependenc…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

首先引入POM依赖

		<!--nosql数据库-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-redis</artifactId>
		</dependency>

		<!--spring2.0集成redis所需common-pool2-->
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-pool2</artifactId>
			<version>2.4.2</version>
		</dependency>

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

加入配置

这里我用的是YML的格式

spring: 
    redis:
      #数据库索引
      database: 0
      host: 127.0.0.1
      port: 6379
      password:
      jedis:
        pool:
          #最大连接数
          max-active: 8
          #最大阻塞等待时间(负数表示没限制)
          max-wait: -1
          #最大空闲
          max-idle: 8
          #最小空闲
          min-idle: 0
      #连接超时时间
      timeout: 10000

正常情况下这样就可以直接使用了

@SpringBootTest
@RunWith(SpringRunner.class)
@Component
public class TestRedis {

    @Autowired
    private RedisTemplate redisTemplate;
    
    @Test
    public void set(){
        redisTemplate.opsForValue().set("test:set","testValue1");
    }
}

完了?

没错,完了!

可能你就好奇了,不对呀,刚刚还引入了fastjson啊,这就是文章的重点了,然我们跑一下刚刚的程序,看一下我们存的键值对

springboot2.0整合kafka_spring与mybatis整合

虽然存取是没什么问题,但是这看着让人很蓝受,让我们翻阅Redis的源码发现RedisTemplate中有这么一段

springboot2.0整合kafka_spring与mybatis整合

这是由于默认序列化方式导致的,竟然发现了问题,现在就解决它

首先自定义一个通用序列化方式的类

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);
    }

}

然后我们找源码

org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration

毫无疑问,它就是Redis的核心配置了,新建一个类RedisConfig把RedisAutoConfiguration中的内容全部复制过来,稍加修改

@Configuration
@ConditionalOnClass(RedisOperations.class)
@EnableConfigurationProperties(RedisProperties.class)
public class RedisConfig {

    @Bean
    @ConditionalOnMissingBean(name = "redisTemplate")
    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;
    }

    @Bean
    @ConditionalOnMissingBean(StringRedisTemplate.class)
    public StringRedisTemplate stringRedisTemplate(
            RedisConnectionFactory redisConnectionFactory) {
        StringRedisTemplate template = new StringRedisTemplate();
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }

}

如上代码所见,我们修改了key和value的序列化规则,key就用Redis本身提供的序列化方式,value就用我们刚刚定义的fastjson序列化方式。

至此完毕



版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/189299.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • expdp / impdp 用法详解[通俗易懂]

    expdp / impdp 用法详解[通俗易懂]一、注意事项:EXP和IMP是客户端工具程序,它们既可以在客户端使用,也可以在服务端使用。EXPDP和IMPDP是服务端的工具程序,他们只能在ORACLE服务端使用,不能在客户端使用。IMP只适用于EXP导出的文件,不适用于EXPDP导出文件;IMPDP只适用于EXPDP导出的文件,而不适用于EXP导出文件。二、使用expdp导出文件前的设置:1、创建逻辑目录,该命令不会在操作系统…

    2022年4月18日
    65
  • idea快捷键设置成idea_idea快捷键设置

    idea快捷键设置成idea_idea快捷键设置====一、快捷键的设置=====1、单击文件,然后点Settings2、点击Keymap进入EditorActions3、选择自己要设置的快捷键,先Remove(移除)原来的快捷键,再Add自己想设置的快捷键,直接一次性输入即可,假如是Ctrl+d,那么就同时按下这两个键即可。移除之后就没了,再自己重新设置其它快捷键可以自己搜索二、IDEA默认快捷键(持续更新)1、自己常用便捷操作(初级):**①ctrl+d—–

    2022年9月20日
    3
  • qt交叉编译环境搭建_arm linux gcc

    qt交叉编译环境搭建_arm linux gccaarch64-linux-gnu-gcc是由Linaro公司基于GCC推出的的ARM交叉编译工具

    2022年10月10日
    2
  • 最全中文停用词表(可直接复制)

    最全中文停用词表(可直接复制)最全的停用此表整理词表名词表文件中文停用词表cn_stopwords.txt哈工大停用词表hit_stopwords.txt百度停用词表baidu_stopwords.txt机器智能实验室停用词库scu_stopwords.txt以上停用词表链接:https://github.com/goto456/stopwords以下是我常用的1893个停用词,可直接复制!”#$%&'()*+,—……….

    2022年6月17日
    43
  • html怎么将表格居中_HTML居中代码

    html怎么将表格居中_HTML居中代码表格是一种以有组织的方式呈现大量信息的绝佳方式。销售数据、网页流量、股票市场趋势和学生成绩是经常以表格形式呈现的信息示例。使用HTML将表格添加到网页时,将其置于页面中心可能更具视觉吸引力。居中文本和图片通常是通过text-align类或通过CSS来完成的,但是居中表格需要不同的方法。下面提供了有关如何使表格在网页上居中的详细信息。将表格添加到网页时,默认情况下,它与页面或容器的左侧对齐,如下所示。上表的HTML源代码如下。要使此表居中,您需要添加;margin-left:auto;margin-r

    2022年9月18日
    5
  • linux命令大全网址https://www.linuxcool.com/

    linux命令大全网址https://www.linuxcool.com/linux命令大全网址https://www.linuxcool.com/

    2022年7月1日
    26

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号