看了几篇博客总结了一下。
1. 定义sql注入器
自定义的sql注入器继承DefaultSqlInjector
并重写方法getMethodList
,在其中加入InsertBatchSomeColumn
方法。
public class SpiceSqlInjector extends DefaultSqlInjector {
@Override
public List<AbstractMethod> getMethodList(Class<?> mapperClass) {
List<AbstractMethod> methodList = super.getMethodList(mapperClass);
methodList.add(new InsertBatchSomeColumn());
return methodList;
}
}
2. 在MybatisPlusConfig里面注册bean
@Configuration
@MapperScan("com.example.demo.dao")
public class MybatisPlusConfig {
@Bean
public SpiceSqlInjector easySqlInjector(){
return new SpiceSqlInjector();
}
}
3. 定义自己的基础mapper类
public interface RootMapper<T> extends BaseMapper<T> {
int insertBatchSomeColum(List<T> entityList);
}
后面使用时仅需要其他mapper继承RootMapper即可使用此方法,例如:
public interface UserDAO extends RootMapper<User> {}
// 测试一下是否加载成功
public int insertBatch(List<User> entities){
userDAO.insertBatchSomeColum(entities);
return 0;
}
数据库是否批量插入没测,有空测一下8。