spring boot + sql server大数据量批量新增

先发出来 注释慢慢完善

/**
     * 获取类中所有属性注解@TableId和@TableField
     *
     * @param instance
     * @return
     * @throws NoSuchFieldException
     */
    public static Map getDeclaredFieldsInfo(Object instance) throws NoSuchFieldException {
        Map map = new HashMap();
        Class clazz = instance.getClass();
        Field[] fields = clazz.getDeclaredFields();
        StringBuilder stringBuilder = new StringBuilder();

        for (int i = 0; i < fields.length; i++) {
            String annotationName = "";
            boolean annotationId = fields[i].isAnnotationPresent(TableId.class);
            if (annotationId) {
                // 获取注解值
                annotationName = fields[i].getAnnotation(TableId.class).value();
            }
            boolean annotationPresent = fields[i].isAnnotationPresent(TableField.class);
            if (annotationPresent) {
                // 获取注解值
                annotationName = fields[i].getAnnotation(TableField.class).value();
            }
            stringBuilder.append(annotationName);
            stringBuilder.append(",");
            // 字段名称
            String attributesName = fields[i].getName();
            map.put(attributesName, new String[]{annotationName, fields[i].getType().getName()});
        }
        return map;
    }


  
    /**
     * 采用saveBulkCopy的方式批量保存数据
     *
     * @param objects
     * @param 
     */
    public  void saveBulkCopy(List objects) throws SQLException {
        if (ObjectUtil.isEmpty(objects)) {
            return;
        }
        T objClass = objects.get(0);
        Connection connection = DriverManager.getConnection(URL, USER, PASSWORD);
        try {
            // 数据库表名
            String tableName = objClass.getClass().getAnnotation(TableName.class).value();
            // 表字段属性名和类型@TableId和@TableField
            Map declaredFieldsInfo = getDeclaredFieldsInfo(objClass);

            ResultSet rs = executeSQL(connection, "select * from " + tableName + " where 1=0");
            CachedRowSetImpl crs = new CachedRowSetImpl();
            crs.populate(rs);
            //循环插入数据
            long startTime = System.currentTimeMillis();
            //既然是批量插入肯定是需要循环
            for (int i = 0, leg = objects.size(); i < leg; i++) {
                //移动指针到“插入行”,插入行是一个虚拟行
                crs.moveToInsertRow();
                //更新虚拟行的数据(实体类要新增的字段,大家根据自己的实体类的字段来修改)
                //数据库字段     ,填写的值
                JSONObject jsonObject = JSONUtil.parseObj(objects.get(i), false, true);
                for (Map.Entry map : jsonObject.entrySet()) {
                    // 属性和值
                    String key = map.getKey();
                    Object value = map.getValue();
                    // 过滤不是数据库的字段
                    if (!declaredFieldsInfo.containsKey(key)) {
                        continue;
                    }
                    // 字段名称,字段类型
                    String[] strings = declaredFieldsInfo.get(key);
                    String type = strings[1];

                    if (StrUtil.equals(type, String.class.getName())) {
                        if (ObjectUtil.isEmpty(value) || StrUtil.equals("null", value.toString())) {
                            crs.updateString(key, null);
                        } else {
                            crs.updateString(key, value.toString());
                        }
                    } else if (StrUtil.equals(type, Integer.class.getName())) {
                        if (ObjectUtil.isEmpty(value) || StrUtil.equals("null", value.toString())) {
                            crs.updateInt(key, 0);
                        } else {
                            crs.updateInt(key, Integer.parseInt(value.toString()));
                        }
                    } else if (StrUtil.equals(type, LocalDateTime.class.getName())) {
                        LocalDateTime localDateTime = (LocalDateTime) value;
                        crs.updateDate(key, localTimeToDate(localDateTime));
                    } else if (StrUtil.equals(type, LocalDate.class.getName())) {
                        LocalDate localDate = (LocalDate) value;
                        crs.updateDate(key, Date.valueOf(localDate));
                    } else if (StrUtil.equals(type, Long.class.getName())) {
                        crs.updateLong(key, 0);
                    } else if (StrUtil.equals(type, Double.class.getName())) {
                        crs.updateDouble(key, (Double) value);
                    } else if (StrUtil.equals(type, int.class.getName())) {
                        crs.updateInt(key, (Integer) value);
                    } else if (StrUtil.equals(type, java.util.Date.class.getName())) {
                        java.util.Date date = (java.util.Date) value;
                        crs.updateDate(key, new Date(date.getTime()));
                    } else if (StrUtil.equals(type, java.math.BigDecimal.class.getName())) {
                        if (ObjectUtil.isEmpty(value) || StrUtil.equals("null", value.toString())) {
                            crs.updateBigDecimal(key, BigDecimal.ZERO);
                        } else {
                            BigDecimal bigDecimal = null;
                            if (value instanceof BigDecimal) {
                                bigDecimal = (BigDecimal) value;
                            } else if (value instanceof String) {
                                bigDecimal = new BigDecimal((String) value);
                            } else if (value instanceof BigInteger) {
                                bigDecimal = new BigDecimal((BigInteger) value);
                            } else if (value instanceof Number) {
                                bigDecimal = new BigDecimal(((Number) value).doubleValue());
                            }
                            crs.updateBigDecimal(key, bigDecimal);
                        }
                    } else {
                        log.info("未知的数据类型:{}", type);
                    }
                }
                //插入虚拟行
                crs.insertRow();
                //移动指针到当前行
                crs.moveToCurrentRow();
            }
            //进行批量插入
            SQLServerBulkCopyOptions copyOptions = new SQLServerBulkCopyOptions();
            copyOptions.setKeepIdentity(false);
            copyOptions.setBatchSize(1000);
            copyOptions.setUseInternalTransaction(false);

            SQLServerBulkCopy bulkCopy = new SQLServerBulkCopy(connection);
            bulkCopy.setBulkCopyOptions(copyOptions);
            bulkCopy.setDestinationTableName(tableName);
            bulkCopy.writeToServer(crs);
            crs.close();
            bulkCopy.close();
            log.info("耗时:{},数量:{}", System.currentTimeMillis() - startTime, objects.size());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    // 数据查找,返回查找的内容,向上抛异常
    public ResultSet executeSQL(Connection con, String sql, Object... object) throws SQLException {
        PreparedStatement ps = con.prepareStatement(sql);
        for (int i = 0; i < object.length; i++) {
            //ps传入参数的下标是从1开始
            ps.setObject(i + 1, object[i]);
        }
        //返回结果集
        return ps.executeQuery();
    }

    public static java.sql.Date localTimeToDate(LocalDateTime lt) {
        return new java.sql.Date(lt.atZone(ZoneId.systemDefault()).toInstant()
                .toEpochMilli());
    }

你可能感兴趣的:(数据库,spring,boot,后端,sqlserver)