美文网首页
提供一种在 xml 中指定使用 XxxTypeHandler 映

提供一种在 xml 中指定使用 XxxTypeHandler 映

作者: 雁过留声_泪落无痕 | 来源:发表于2024-04-09 16:44 被阅读0次

背景

Java 项目,使用 Mybatis Plus,在 xml 中使用了 JSON_ARRAY/JSON_ARRAYAGG 函数,想直接映射成 Java 实体,势必想到要用 JacksonTypeHandler,但是有一个问题,就是对于复杂类型(含泛型),就没法在 xml 中通过 javaType 的方式告知 JacksonTypeHandler 了,遂导致解析失败。

解决

MyJacksonTypeHandler.java

public class MyJacksonTypeHandler extends JacksonTypeHandler {

    private final Class<?> type;

    public MyJacksonTypeHandler(Class<?> type) {
        super(type);
        this.type = type;
    }

    protected Object parse(String json) {
        try {
            if (MyTypeReference.class.isAssignableFrom(type)) {
                MyTypeReference typeReference = (MyTypeReference) type.getConstructor().newInstance();
                return getObjectMapper().readValue(json, typeReference.getType());
            }

            return getObjectMapper().readValue(json, this.type);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}

MyTypeReference.java

public interface MyTypeReference {

    TypeReference<?> getType();

}

TestJsonList.java

@Data
public class TestJsonList {

    private Integer id;

    private List<Dog> jsonInfo;

    @Data
    public static class Dog {
        private String name;
    }

    public static class DogListTypeReference implements MyTypeReference {
        @Override
        public TypeReference<?> getType() {
            return new TypeReference<List<Dog>>(){};
        }
    }

}

TestJsonMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="xxx.mapper.TestJsonMapper">
    <resultMap id="testJsonResultMap" type="xxx.model.TestJson">
        <result property="jsonInfo" column="json_info" jdbcType="LONGVARCHAR"
                javaType="xxx.model.TestJson$Dog"
                typeHandler="com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler"/>
    </resultMap>

    <resultMap id="testJsonListResultMap" type="xxx.model.TestJsonList">
        <result property="jsonInfo" column="json_info" jdbcType="LONGVARCHAR"
                javaType="xxx.model.TestJsonList$DogListTypeReference"
                typeHandler="xxx.handler.MyJacksonTypeHandler"/>
    </resultMap>

    <select id="testJson" resultMap="testJsonResultMap">
        select 1 as id, '{"name": "dahuang"}' as json_info
    </select>

    <select id="testJsonList" resultMap="testJsonListResultMap">
        select 1 as id, '[{"name": "dahuang"}]' as json_info
    </select>
</mapper>

后续

实际测试过程中,发现使用了 LEFT JOIN 后,得到的 json 的所有 value 都会 null,解析后就得到一个所有成员都为 null 的对象,需要过滤下:

MyJacksonTypeHandler.java

public class MyJacksonTypeHandler extends JacksonTypeHandler {

    private final Class<?> type;

    public MyJacksonTypeHandler(Class<?> type) {
        super(type);
        this.type = type;
    }

    @Override
    protected Object parse(String json) {
        try {
            // 如果指定了 MyTypeReference 类型,需要特殊处理
            if (MyTypeReference.class.isAssignableFrom(type)) {
                MyTypeReference typeReference = (MyTypeReference) type.getConstructor().newInstance();
                Object result = getObjectMapper().readValue(json, typeReference.getType());
                // 由于使用了 LEFT JOIN,右表可能为空,导致这里的 json 所有 value 都为 null,单独处理下
                if (isAllFieldNull(result)) {
                    return null;
                } else {
                    removeNullItem(result);
                    return result;
                }
            }

            // 否则,使用默认的处理方式就行
            return getObjectMapper().readValue(json, type);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 去除 List 中对象成员均为 null 的项
     */
    private void removeNullItem(Object object) {
        Class<?> clazz = object.getClass();
        if (List.class.isAssignableFrom(clazz)) {
            ((List<?>) object).removeIf(this::isAllFieldNull);
        }
    }

    /**
     * 判断对象是否所有成员都为 null
     */
    private boolean isAllFieldNull(Object object) {
        Class<?> clazz = object.getClass();
        for (Field field : clazz.getDeclaredFields()) {
            field.setAccessible(true);
            try {
                if (field.get(object) != null) {
                    return false;
                }
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }

        return true;
    }

}

补充

如果不在 XxxMapper.xml 中书写 sql,可以直接在 Java 类上指定 JacksonTypeHandler 即可,也能处理复杂类型(含泛型)

TestJson.java

@Data
@TableName(value = "test_json", autoResultMap = true)
public class TestJson {

    private Integer id;

    @TableField(typeHandler = JacksonTypeHandler.class)
    private Dog jsonInfo;

    @TableField(typeHandler = JacksonTypeHandler.class)
    private List<Dog> jsonInfoList;

    @Data
    public static class Dog {
        private String name;
    }

}

TestController.java

// 不使用 XxxMapper.xml 中的方法
TestJson testJson = testJsonMapper.selectOne(null);

因为在 XxxMapper.xml 中书写 sql,只能通过 resultMap 的方式指定 javaType 和 typeHandler(@TableField 注解指定的 typeHandler 不会被使用),但是却无法将泛型信息传递给指定的 typeHandler。

相关文章

  • velocity的简单使用

    使用Velocity Tools的时候,一定要在toolbox.xml中指定,然后在应用的web.xml中配置。 ...

  • React学习笔记(二)-- 理解JSX

    摘要 JSX(JavaScriptXML)提供了一种在JavaScript中编写声明式的XML的方法,使用JSX可...

  • Jsp(五)

    XML 标签 JSTL XML标签库提供了创建和操作XML文档的标签。引用XML标签库的语法如下: 在使用xml标...

  • 2.数据驱动测试

    1)通过testng.xml传递参数: 适用于简单的参数,可以在testng.xml文件中指定; 注意:在xml数...

  • xmapper路径映射优化

    一、Xmappr介绍 Xmappr是一个使用纯Java编写的处理XML的类库,提供一种非常简单的方式来处理XML,...

  • maven 如何用私有仓库中的包

    在project中的pom.xml文件中指定respository就可以指定从私有仓库中下载jar包使用,类似这样

  • mybatis

    使用mybatis 单个对象的mapper.xml提供一种namespace.id=>sql的映射关系 mybat...

  • Activity的启动模式

    LaunchMode 在声明Activity的xml中指定 android:launchMode="xxx" st...

  • maven的配置

    1、setting.xml 配置 2、私有仓库配置: 3、在pom文件中指定仓库优先于setting.xml

  • Android 自定义可以设置 最大宽高和最小宽高的 Fram

    Android 提供的 FrameLayout 中只提供了 设置最小宽高的属性,在xml中使用的话就是androi...

网友评论

      本文标题:提供一种在 xml 中指定使用 XxxTypeHandler 映

      本文链接:https://www.haomeiwen.com/subject/pkyfxjtx.html