加入tkmybatis,进行批量插入

首先模仿接口insertList接口:

/**
 * Created by kun.zuo
 * Created by time 2020/12/29
 * Vsrsion 1.0
 */
public class SpecialBatchProvider  extends MapperTemplate {

    public SpecialBatchProvider(Class mapperClass, MapperHelper mapperHelper) {
        super(mapperClass, mapperHelper);
    }

    public String batchInseetList(MappedStatement ms) {
        Class entityClass = getEntityClass(ms);
        //开始拼sql
        StringBuilder sql = new StringBuilder();
        sql.append(SqlHelper.insertIntoTable(entityClass, tableName(entityClass)));
        sql.append(SqlHelper.insertColumns(entityClass, false, false, false));
        sql.append(" VALUES ");
        sql.append("<foreach collection=\"list\" item=\"record\" separator=\",\" >");
        sql.append("<trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\">");
        //获取全部列
        Set<EntityColumn> columnList = EntityHelper.getColumns(entityClass);
        //当某个列有主键策略时,不需要考虑他的属性是否为空,因为如果为空,一定会根据主键策略给他生成一个值
        for (EntityColumn column : columnList) {
            if (column.isInsertable()) {
                //出现类型com.microsoft.sqlserver.jdbc.SQLServerException: 操作数类型冲突: varbinary
                if(column.getJavaType() == Double.class){
                    String record = column.getColumnHolder("record");
                    record = record.substring(0,record.length()-1)+",jdbcType=DECIMAL"+"}";
                    sql.append(record+ ",");
                }else {
                    sql.append(column.getColumnHolder("record") + ",");
                }
            }
        }
        sql.append("</trim>");
        sql.append("</foreach>");
        return sql.toString();
    }
}

 

/**
 * Created by kun.zuo
 * Created by time 2020/12/29
 * Vsrsion 1.0
 */
@RegisterMapper
public interface SpecialBatchMapper<T> {

    /**
     * 批量插入数据库,所有字段都插入,包括主键
     *
     * @return
     */
    @Options(useGeneratedKeys = true, keyProperty = "id")
    @InsertProvider(type = SpecialBatchProvider.class, method = "batchInseetList")
    int batchInseetList(List<T> recordList);
}
上一篇:新Android Studio如何设置Created by username on 2021/2/17.


下一篇:【SQL查询】集合查询之INTERSECT