前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >升级mp新版本后,service里removeByIds主键类型不一致报错

升级mp新版本后,service里removeByIds主键类型不一致报错

作者头像
阿超
发布2023-01-07 09:20:28
5120
发布2023-01-07 09:20:28
举报
文章被收录于专栏:快乐阿超快乐阿超

先谋后事者逸,先事后图者失。——陈子昂

场景:

主键类型是Integer,使用serviceremoveByIds,传入List<String>报错

MP用的mybatis的反射,只需要在表信息初始化后,获取反射的缓存map,往里面放入我们自定义的转换操作即可

代码如下:

代码语言:javascript
复制
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import io.github.vampireachao.stream.core.reflect.ReflectHelper;
import io.github.vampireachao.stream.core.stream.Steam;
import org.apache.ibatis.reflection.Reflector;
import org.apache.ibatis.reflection.invoker.Invoker;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.ConversionService;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

/**
 * @author VampireAchao
 * @since 2023/1/5 16:34
 */
@Configuration
public class MpConfig {

    public MpConfig(MybatisPlusAutoConfiguration config, ConversionService conversionService) {
        final List<TableInfo> tableInfos = TableInfoHelper.getTableInfos();
        Steam.of(tableInfos).parallel().forEach(tableInfo -> {
            final Reflector reflector = tableInfo.getReflector();
            final Map<String, Invoker> setMethods = ReflectHelper.getFieldValue(reflector, "setMethods");
            setMethods.computeIfPresent(tableInfo.getKeyProperty(), (k, v) -> new Invoker() {

                @Override
                public Object invoke(Object target, Object[] args) throws IllegalAccessException, InvocationTargetException {
                    final Object convert = conversionService.convert(args[0], v.getType());
                    return v.invoke(target, new Object[]{convert});
                }

                @Override
                public Class<?> getType() {
                    return v.getType();
                }
            });
        });
    }

}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2023-01-06,如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

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