前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >r2dbc配置枚举转换器

r2dbc配置枚举转换器

作者头像
阿超
发布2024-03-05 10:48:41
1340
发布2024-03-05 10:48:41
举报
文章被收录于专栏:快乐阿超快乐阿超

你如果愿意做哲学家,尽管做好了,但是你在你的全部哲学思维中,仍然要做一个人。——休谟

首先配置Converter

代码语言:javascript
复制



import jakarta.annotation.Nullable;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.WritingConverter;
import org.springframework.stereotype.Component;

/**
 * EnumToIntConverter
 *
 * @author achao@apache.org
 */
@Component
@WritingConverter
public class AgoraNotifyTypeEnumToIntConverter implements Converter<AgoraNotifyTypeEnum, Integer> {
    @Override
    public Integer convert(@Nullable AgoraNotifyTypeEnum source) {
        if (source == null) {
            return null;
        }
        return source.getValue();
    }
}

以及

代码语言:javascript
复制


import cn.hutool.core.util.EnumUtil;

import jakarta.annotation.Nullable;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.ReadingConverter;
import org.springframework.stereotype.Component;

/**
 * IntToIEnumConverter
 *
 * @author achao@apache.org
 */
@Component
@ReadingConverter
public class IntToAgoraNotifyTypeEnumConverter implements Converter<Integer, AgoraNotifyTypeEnum> {
    @Override
    public AgoraNotifyTypeEnum convert(@Nullable Integer source) {
        if (source == null) {
            return null;
        }
        return EnumUtil.getBy(AgoraNotifyTypeEnum::getValue, source);
    }
}

然后还需要配置

代码语言:javascript
复制


import io.r2dbc.spi.ConnectionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.CustomConversions;
import org.springframework.data.r2dbc.convert.R2dbcCustomConversions;
import org.springframework.r2dbc.connection.R2dbcTransactionManager;
import org.springframework.transaction.ReactiveTransactionManager;
import org.springframework.transaction.reactive.TransactionalOperator;

import java.util.List;

/**
 * R2dbcConfig
 *
 * @author VampireAchao<achao @ hutool.cn>
 * @since 2023/10/27
 */
@Configuration
public class R2dbcConfig {

    @Bean
    public R2dbcCustomConversions r2dbcCustomConversions(List<Converter<?, ?>> converters) {
        return new R2dbcCustomConversions(CustomConversions.StoreConversions.NONE, converters);
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-03-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档