zoukankan      html  css  js  c++  java
  • 加入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);
    }
  • 相关阅读:
    2019.9.6文艺理论笔记
    2019.9.5作业整理
    2019下高级英语笔记
    2019下实用口才课笔记
    【学英语~磨耳朵】2013年以来看过的所有美剧&电影&纪录片等等
    python温度转换代码
    python蟒蛇绘制的代码以及目前还不知道怎么用的RGB颜色对照表
    看TED演讲——Why you will fail to have a great career
    结构与算法(5)-----队列
    结构与算法(4)-----栈
  • 原文地址:https://www.cnblogs.com/zuokun/p/14240717.html
Copyright © 2011-2022 走看看