MysqlInsertAllBatch
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import org.apache.ibatis.executor.keygen.NoKeyGenerator;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;
/**
* 自定义mysql的批量保存语法:
* 如:insert into user(id, name, age) values (1, "a", 17), (2,"b", 18)
* 自定义mapper如下使用:int mysqlInsertAllBatch(@Param("list") List<T> batchList);
* @author mori
* @date 2021-07-28 10:52:00
*/
public class MysqlInsertAllBatch extends AbstractMethod {
@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
final String sql = "<script>insert into %s %s values %s</script>";
final String fieldSql = prepareFieldSql(tableInfo);
final String valueSql = prepareValuesSqlForMysqlBatch(tableInfo);
final String sqlResult = String.format(sql, tableInfo.getTableName(), fieldSql, valueSql);
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sqlResult, modelClass);
return this.addInsertMappedStatement(mapperClass, modelClass, "mysqlInsertAllBatch", sqlSource, new NoKeyGenerator(), null, null);
}
private String prepareFieldSql(TableInfo tableInfo) {
StringBuilder fieldSql = new StringBuilder();
fieldSql.append(tableInfo.getKeyColumn()).append(",");
tableInfo.getFieldList().forEach(x -> {
fieldSql.append(x.getColumn()).append(",");
});
fieldSql.delete(fieldSql.length() - 1, fieldSql.length());
fieldSql.insert(0, "(");
fieldSql.append(")");
return fieldSql.toString();
}
private String prepareValuesSqlForMysqlBatch(TableInfo tableInfo) {
final StringBuilder valueSql = new StringBuilder();
valueSql.append("<foreach collection=\"list\" item=\"item\" index=\"index\" open=\"(\" separator=\"),(\" close=\")\">");
valueSql.append("#{item.").append(tableInfo.getKeyProperty()).append("},");
tableInfo.getFieldList().forEach(x -> valueSql.append("#{item.").append(x.getProperty()).append("},"));
valueSql.delete(valueSql.length() - 1, valueSql.length());
valueSql.append("</foreach>");
return valueSql.toString();
}
}
DeleteAll
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;
/**
* 物理删除表中所有数据
* 自定义mapper如下使用:
* Integer deleteAll();
* @author mori
* @date 2021-07-28 11:06:00
*/
public class DeleteAll extends AbstractMethod {
@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
/* 执行 SQL ,动态 SQL 参考类 SqlMethod */
String sql = "delete from " + tableInfo.getTableName();
/* mapper 接口方法名一致 */
String method = "deleteAll";
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
return this.addDeleteMappedStatement(mapperClass, method, sqlSource);
}
}
MyLogicSqlInjector
import java.util.List;
/**
* sql注入器
* @author mori
* @date 2021-07-28 10:54:00
*/
public class MyLogicSqlInjector extends DefaultSqlInjector {
/**
* 查询方法列表
* 如果只需增加方法,保留MP自带方法
* 可以super.getMethodList() 再add
*
* @return {@link List<AbstractMethod>}
*/
@Override
public List<AbstractMethod> getMethodList(Class<?> mapperClass) {
List<AbstractMethod> methodList = super.getMethodList(mapperClass);
methodList.add(new MysqlInsertAllBatch());
methodList.add(new DeleteAll());
methodList.add(new LogicDeleteByIdWithFill());
return methodList;
}
}
CustomBaseMapper
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* 自定义扩展mybatis-plus的BaseMapper
* @author mori
* @date 2021-07-28 10:57:00
*/
public interface CustomBaseMapper<T> extends BaseMapper<T> {
/**
* mysql批量插入
* 如果要自动填充,@{@code Param}(xx) xx参数名必须是 list/collection/array 3个的其中之一
* @param batchList 批处理列表
* @return int
*/
int mysqlInsertAllBatch(@Param("list") List<T> batchList);
/**
* 带自动填充功能的逻辑删除
*
* @param entity 实体
* @return int
*/
int deleteByIdWithFill(T entity);
/**
* 物理删除表中所有数据
*
* @return {@link Integer}
*/
Integer deleteAll();
}