前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >mp中typeHandler自动获取字段类型

mp中typeHandler自动获取字段类型

作者头像
阿超
发布2023-03-23 21:14:31
1.2K0
发布2023-03-23 21:14:31
举报
文章被收录于专栏:快乐阿超快乐阿超

相熟的人表现出恭而敬之的样子总是叫人感到可笑。——歌德

一般我们在实体类上指定

@TableName(autoResultMap = true)

即可使用typeHandler指定转换器,然后就可以自动转换了

例如List<XXX>Json可以如下使用:

代码语言:javascript
复制
@TableField(typeHandler = JsonListHandler.class)
private List<CalcUnitEnum> calcUnits;

这里JsonListHandler如下,JacksonUtil就懒得赘述了:

代码语言:javascript
复制
import com.baomidou.mybatisplus.extension.handlers.AbstractJsonTypeHandler;

import java.util.List;

/**
 * @author VampireAchao
 * @since 2023/3/20 17:43
 */
public class JsonListHandler<T> extends AbstractJsonTypeHandler<List<T>> {

    private Class<T> clazz;

    public void setClazz(Class<T> clazz) {
        this.clazz = clazz;
    }

    @Override
    protected List<T> parse(String json) {
        return JacksonUtil.toList(json, clazz);
    }

    @Override
    protected String toJson(List<T> obj) {
        return JacksonUtil.toJson(obj);
    }
}

光有这个是不够的,还需要在注入表信息时候,将字段内的泛型拿到

代码语言:javascript
复制

import com.baomidou.mybatisplus.core.handlers.PostInitTableInfoHandler;
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import io.github.vampireachao.stream.core.reflect.ReflectHelper;
import org.apache.ibatis.mapping.ResultMap;
import org.apache.ibatis.mapping.ResultMapping;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.type.TypeHandler;
import org.springframework.stereotype.Component;

/**
 * @author VampireAchao
 * @since 2023/3/20 18:10
 */
@Component
public class JsonPostInitTableInfoHandler implements PostInitTableInfoHandler {

    /**
     * 参与 TableInfo 初始化
     *
     * @param tableInfo     TableInfo
     * @param configuration Configuration
     */
    @Override
    public void postTableInfo(TableInfo tableInfo, Configuration configuration) {
        for (TableFieldInfo fieldInfo : tableInfo.getFieldList()) {
            if (fieldInfo.getTypeHandler() == null) {
                continue;
            }
            if (tableInfo.getResultMap() == null) {
                return;
            }
            ResultMap resultMap = configuration.getResultMap(tableInfo.getResultMap());
            for (ResultMapping resultMapping : resultMap.getResultMappings()) {
                TypeHandler<?> handler = resultMapping.getTypeHandler();
                if (handler instanceof JsonListHandler) {
                    JsonListHandler<Object> typeHandler = (JsonListHandler<Object>) handler;
                    typeHandler.setClazz((Class<Object>) ReflectHelper.getGenericTypes(fieldInfo.getField().getGenericType())[0]);
                }
            }
        }
        PostInitTableInfoHandler.super.postTableInfo(tableInfo, configuration);
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2023-03-21,如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

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