• 加入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);
    }
  • 相关阅读:
    Flex的DateChooser组件中文显示方法
    FLEX XML XMLList XMLListCollection ArrayCollection相互转换
    开机电脑cpu占用100%
    FLEX 动态绑定chart 数据
    FLEX 数组 转化成 xml
    FLEX 通过url 得到网页内容 xml通信
    FLEX 时间计算
    FlEX 如何调试
    unix,linux,windows 哪个更好,更适合做服务器
    FLEX httpService 用法介绍
  • 原文地址:https://www.cnblogs.com/zuokun/p/14240717.html
Copyright © 2020-2023  润新知