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

场景:

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

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

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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();
}
});
});
}

}