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


    个人主页金鳞踏雨

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

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

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

    背景

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

    1. @Service
    2. public class DingRobotMsgServiceImpl extends ServiceImpl implements IDingRobotMsgService {
    3. @Autowired
    4. private DingRobotMsgMapper dingRobotMsgMapper;
    5. @Override
    6. public void insertRobotMsg(DingRobotMsg dingRobotMsg) {
    7. // 新增
    8. dingRobotMsg.setState("1");
    9. if (dingRobotMsg.getMsgtype().equals("text") || dingRobotMsg.getMsgtype().equals("file")) {
    10. dingRobotMsgMapper.insert(dingRobotMsg);
    11. } else {
    12. // TODO: 类型错误!
    13. }
    14. }
    15. }
    1. @Data
    2. @TableName("t_dingtalk_recemsg")
    3. public class DingRobotMsg {
    4. @TableId(value = "id", type = IdType.AUTO)
    5. private Long id;
    6. @TableField(value = "msgtype")
    7. private String msgtype;
    8. private Content content;
    9. private Text text;
    10. // ...
    11. }

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

    JsonTypeHandler

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

    通用 JsonTypeHandler 工具类

    1. /**
    2. * 对象字段转存为Json类型
    3. * @param
    4. */
    5. @MappedTypes({Text.class, Content.class})
    6. public class JsonTypeHandler extends BaseTypeHandler {
    7. private final Class type;
    8. public JsonTypeHandler(Class type) {
    9. if (type == null) {
    10. throw new IllegalArgumentException("Type argument cannot be null");
    11. }
    12. this.type = type;
    13. }
    14. @Override
    15. public void setNonNullParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException {
    16. // 将对象转换为JSON字符串并设置到PreparedStatement中
    17. ps.setString(i, JSON.toJSONString(parameter));
    18. }
    19. @Override
    20. public T getNullableResult(ResultSet rs, String columnName) throws SQLException {
    21. // 从ResultSet中获取JSON字符串并转换为指定类型的对象
    22. String jsonString = rs.getString(columnName);
    23. return JSON.parseObject(jsonString, type);
    24. }
    25. @Override
    26. public T getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
    27. // 从ResultSet中获取JSON字符串并转换为指定类型的对象
    28. String jsonString = rs.getString(columnIndex);
    29. return JSON.parseObject(jsonString, type);
    30. }
    31. @Override
    32. public T getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
    33. // 从CallableStatement中获取JSON字符串并转换为指定类型的对象
    34. String jsonString = cs.getString(columnIndex);
    35. return JSON.parseObject(jsonString, type);
    36. }
    37. }

    JsonTypeHandler 的使用

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

    1. @TableField(typeHandler = JsonTypeHandler.class)
    2. private Content content;
    3. @TableField(typeHandler = JsonTypeHandler.class)
    4. private Text text;

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

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

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

  • 相关阅读:
    如何通过port-forward命令在本地访问 k8s 集群服务
    面试官常问的前端知识
    从 React 源码彻底搞懂 Ref 的全部 api
    【开发工具】【Valgrind】内存问题检测工具(valgrind)的使用
    ServletConfig与ServletContext
    SVG 基本语法
    JS起源与简介
    JDBC MySQL任意文件读取分析
    openGauss学习笔记-66 openGauss 数据库管理-创建和管理schema
    快速修复“找不到xinput1_3.dll无法继续执行此代码的”问题的5个方法
  • 原文地址:https://blog.csdn.net/weixin_43715214/article/details/133001758