MysqlInsertAllBatch

    1. import com.baomidou.mybatisplus.core.injector.AbstractMethod;
    2. import com.baomidou.mybatisplus.core.metadata.TableInfo;
    3. import org.apache.ibatis.executor.keygen.NoKeyGenerator;
    4. import org.apache.ibatis.mapping.MappedStatement;
    5. import org.apache.ibatis.mapping.SqlSource;
    6. /**
    7. * 自定义mysql的批量保存语法:
    8. * 如:insert into user(id, name, age) values (1, "a", 17), (2,"b", 18)
    9. * 自定义mapper如下使用:int mysqlInsertAllBatch(@Param("list") List<T> batchList);
    10. * @author mori
    11. * @date 2021-07-28 10:52:00
    12. */
    13. public class MysqlInsertAllBatch extends AbstractMethod {
    14. @Override
    15. public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
    16. final String sql = "<script>insert into %s %s values %s</script>";
    17. final String fieldSql = prepareFieldSql(tableInfo);
    18. final String valueSql = prepareValuesSqlForMysqlBatch(tableInfo);
    19. final String sqlResult = String.format(sql, tableInfo.getTableName(), fieldSql, valueSql);
    20. SqlSource sqlSource = languageDriver.createSqlSource(configuration, sqlResult, modelClass);
    21. return this.addInsertMappedStatement(mapperClass, modelClass, "mysqlInsertAllBatch", sqlSource, new NoKeyGenerator(), null, null);
    22. }
    23. private String prepareFieldSql(TableInfo tableInfo) {
    24. StringBuilder fieldSql = new StringBuilder();
    25. fieldSql.append(tableInfo.getKeyColumn()).append(",");
    26. tableInfo.getFieldList().forEach(x -> {
    27. fieldSql.append(x.getColumn()).append(",");
    28. });
    29. fieldSql.delete(fieldSql.length() - 1, fieldSql.length());
    30. fieldSql.insert(0, "(");
    31. fieldSql.append(")");
    32. return fieldSql.toString();
    33. }
    34. private String prepareValuesSqlForMysqlBatch(TableInfo tableInfo) {
    35. final StringBuilder valueSql = new StringBuilder();
    36. valueSql.append("<foreach collection=\"list\" item=\"item\" index=\"index\" open=\"(\" separator=\"),(\" close=\")\">");
    37. valueSql.append("#{item.").append(tableInfo.getKeyProperty()).append("},");
    38. tableInfo.getFieldList().forEach(x -> valueSql.append("#{item.").append(x.getProperty()).append("},"));
    39. valueSql.delete(valueSql.length() - 1, valueSql.length());
    40. valueSql.append("</foreach>");
    41. return valueSql.toString();
    42. }
    43. }

    DeleteAll

    1. import com.baomidou.mybatisplus.core.injector.AbstractMethod;
    2. import com.baomidou.mybatisplus.core.metadata.TableInfo;
    3. import org.apache.ibatis.mapping.MappedStatement;
    4. import org.apache.ibatis.mapping.SqlSource;
    5. /**
    6. * 物理删除表中所有数据
    7. * 自定义mapper如下使用:
    8. * Integer deleteAll();
    9. * @author mori
    10. * @date 2021-07-28 11:06:00
    11. */
    12. public class DeleteAll extends AbstractMethod {
    13. @Override
    14. public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
    15. /* 执行 SQL ,动态 SQL 参考类 SqlMethod */
    16. String sql = "delete from " + tableInfo.getTableName();
    17. /* mapper 接口方法名一致 */
    18. String method = "deleteAll";
    19. SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
    20. return this.addDeleteMappedStatement(mapperClass, method, sqlSource);
    21. }
    22. }

    MyLogicSqlInjector

    1. import java.util.List;
    2. /**
    3. * sql注入器
    4. * @author mori
    5. * @date 2021-07-28 10:54:00
    6. */
    7. public class MyLogicSqlInjector extends DefaultSqlInjector {
    8. /**
    9. * 查询方法列表
    10. * 如果只需增加方法,保留MP自带方法
    11. * 可以super.getMethodList() 再add
    12. *
    13. * @return {@link List<AbstractMethod>}
    14. */
    15. @Override
    16. public List<AbstractMethod> getMethodList(Class<?> mapperClass) {
    17. List<AbstractMethod> methodList = super.getMethodList(mapperClass);
    18. methodList.add(new MysqlInsertAllBatch());
    19. methodList.add(new DeleteAll());
    20. methodList.add(new LogicDeleteByIdWithFill());
    21. return methodList;
    22. }
    23. }

    CustomBaseMapper

    1. import com.baomidou.mybatisplus.core.mapper.BaseMapper;
    2. import org.apache.ibatis.annotations.Param;
    3. import java.util.List;
    4. /**
    5. * 自定义扩展mybatis-plus的BaseMapper
    6. * @author mori
    7. * @date 2021-07-28 10:57:00
    8. */
    9. public interface CustomBaseMapper<T> extends BaseMapper<T> {
    10. /**
    11. * mysql批量插入
    12. * 如果要自动填充,@{@code Param}(xx) xx参数名必须是 list/collection/array 3个的其中之一
    13. * @param batchList 批处理列表
    14. * @return int
    15. */
    16. int mysqlInsertAllBatch(@Param("list") List<T> batchList);
    17. /**
    18. * 带自动填充功能的逻辑删除
    19. *
    20. * @param entity 实体
    21. * @return int
    22. */
    23. int deleteByIdWithFill(T entity);
    24. /**
    25. * 物理删除表中所有数据
    26. *
    27. * @return {@link Integer}
    28. */
    29. Integer deleteAll();
    30. }