升级mp新版本后,service里removeByIds主键类型不一致报错
报错 类型 版本 升级 主键 Service 一致 MP
2023-06-13 09:16:32 时间
先谋后事者逸,先事后图者失。——陈子昂
场景:
主键类型是Integer
,使用service
中removeByIds
,传入List<String>
报错
MP
用的mybatis
的反射,只需要在表信息初始化后,获取反射的缓存map
,往里面放入我们自定义的转换操作即可
代码如下:
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();
}
});
});
}
}
相关文章
- 报错:“来自数据源的String类型的给定值不能转换为指定目标列的类型nvarchar。”「建议收藏」
- 【错误记录】Kotlin 1.5.0 编译报错 ( 1.5.0 中 Float 不能直接转 Byte 类型 )
- ORA-19726: cannot plug data [string] at level string into database running at compatibility level string ORACLE 报错 故障修复 远程处理
- ORA-28382: Global wallet operation in RAC failed ORACLE 报错 故障修复 远程处理
- ORA-29830: operator does not exist ORACLE 报错 故障修复 远程处理
- ORA-48181: OS write system call failure ORACLE 报错 故障修复 远程处理
- MySQL Error number: 4028; Symbol: ER_TABLE_MUST_HAVE_A_VISIBLE_COLUMN; SQLSTATE: HY000 报错 故障修复 远程处理
- MySQL Error number: MY-010125; Symbol: ER_TRANSPORTS_WHAT_TRANSPORTS; SQLSTATE: HY000 报错 故障修复 远程处理
- ORA-01071: cannot perform operation without starting up ORACLE ORACLE 报错 故障修复 远程处理
- MySQL Error number: MY-013899; Symbol: ER_IB_MSG_LOG_PFS_CREATE_TABLES_FAILED; SQLSTATE: HY000 报错 故障修复 远程处理
- MySQL Error number: MY-013964; Symbol: ER_REPLICATION_INCOMPATIBLE_TABLE_WITH_GIPK; SQLSTATE: HY000 报错 故障修复 远程处理
- Mongodb报错解决方案快速有效的处理方法(mongodb报错)
- Oracle报错出现无效数字(oracle出现无效数字)