MyBatisPlus使用自定义JsonTypeHandler实现自动转化JSON

个人主页金鳞踏雨

个人简介 :大家好,我是金鳞,一个初出茅庐的Java小白

目前状况:22届普通本科毕业生,几经波折了,现在任职于一家国内大型知名日化公司,从事Java开发工作

我的博客:这里是CSDN,是我学习技术,总结知识的地方。希望和各位大佬交流,共同进步 ~

背景

在项目中使用了Mybatis-Plus框架,调用了Mapper层的 insert() ,如下所示,DingRobotMsg对象 的属性包含了其它的对象(Text、Content),数据库表字段里有与之对应的字段,类型为json

java 复制代码
@Service
public class DingRobotMsgServiceImpl extends ServiceImpl<DingRobotMsgMapper, DingRobotMsg> implements IDingRobotMsgService {

    @Autowired
    private DingRobotMsgMapper dingRobotMsgMapper;

    @Override
    public void insertRobotMsg(DingRobotMsg dingRobotMsg) {
        // 新增
        dingRobotMsg.setState("1");
        if (dingRobotMsg.getMsgtype().equals("text") || dingRobotMsg.getMsgtype().equals("file")) {
            dingRobotMsgMapper.insert(dingRobotMsg);
        } else {
            // TODO: 类型错误!
        }
    }
}
java 复制代码
@Data
@TableName("t_dingtalk_recemsg")
public class DingRobotMsg {
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    @TableField(value = "msgtype")
    private String msgtype;

    private Content content;

    private Text text;

    // ...
}

这种情况,我们如何在不增加业务逻辑(数据处理)的情况下实现数据库的插入操作呢?

JsonTypeHandler

有的对象字段需要存储为Json ,可以直接转成Json赋值后再保存。也可以通过Mybatis的TypeHandler自动处理。

通用 JsonTypeHandler 工具类

java 复制代码
/**
 * 对象字段转存为Json类型
 * @param <T>
 */
@MappedTypes({Text.class, Content.class})
public class JsonTypeHandler<T> extends BaseTypeHandler<T> {

    private final Class<T> type;

    public JsonTypeHandler(Class<T> type) {
        if (type == null) {
            throw new IllegalArgumentException("Type argument cannot be null");
        }
        this.type = type;
    }

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException {
        // 将对象转换为JSON字符串并设置到PreparedStatement中
        ps.setString(i, JSON.toJSONString(parameter));
    }

    @Override
    public T getNullableResult(ResultSet rs, String columnName) throws SQLException {
        // 从ResultSet中获取JSON字符串并转换为指定类型的对象
        String jsonString = rs.getString(columnName);
        return JSON.parseObject(jsonString, type);
    }

    @Override
    public T getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        // 从ResultSet中获取JSON字符串并转换为指定类型的对象
        String jsonString = rs.getString(columnIndex);
        return JSON.parseObject(jsonString, type);
    }

    @Override
    public T getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        // 从CallableStatement中获取JSON字符串并转换为指定类型的对象
        String jsonString = cs.getString(columnIndex);
        return JSON.parseObject(jsonString, type);
    }
}

JsonTypeHandler 的使用

在entry对象的字段上面加上下面的注解即可!

java 复制代码
@TableField(typeHandler = JsonTypeHandler.class)
private Content content;

@TableField(typeHandler = JsonTypeHandler.class)
private Text text;

文章到这里就结束了,如果有什么疑问的地方,可以在评论区指出~

希望能和大佬们一起努力,诸君顶峰相见

再次感谢各位小伙伴儿们的支持!!!

相关推荐
BillKu29 分钟前
推荐 Eclipse Temurin 的 OpenJDK
java·ide·eclipse
Morri331 分钟前
[Java恶补day53] 45. 跳跃游戏Ⅱ
java·算法·leetcode
悟能不能悟33 分钟前
eclipse怎么把项目设为web
java·eclipse
乂爻yiyao34 分钟前
java 代理模式实现
java·开发语言·代理模式
2301_770373731 小时前
Java集合
java·开发语言
哈喽姥爷1 小时前
Spring Boot---自动配置原理和自定义Starter
java·spring boot·后端·自定义starter·自动配置原理
老华带你飞3 小时前
考研论坛平台|考研论坛小程序系统|基于java和微信小程序的考研论坛平台小程序设计与实现(源码+数据库+文档)
java·vue.js·spring boot·考研·小程序·毕设·考研论坛平台小程序
CHEN5_023 小时前
leetcode-hot100 11.盛水最多容器
java·算法·leetcode
songx_993 小时前
leetcode18(无重复字符的最长子串)
java·算法·leetcode