其他分享
首页 > 其他分享> > 加入tkmybatis,进行批量插入

加入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);
}

标签:entityClass,Created,批量,插入,column,tkmybatis,record,sql,append
来源: https://www.cnblogs.com/zuokun/p/14240717.html