java大数据量插入数据库批量提交

1 篇文章 0 订阅
1 篇文章 0 订阅

直接上代码
1、TestVo.java(测试实体类)

package net.beidousky.web.app.domain;
import io.swagger.annotations.ApiModelProperty;

/**
 * 测试
 */
public class TestVo {

    @ApiModelProperty(value = "类型")
    private int type;
    @ApiModelProperty(value = "名称")
    private String name; //名称
    @ApiModelProperty(value = "地址")
    private String address; //地址
    @ApiModelProperty(value = "经度")
    private Double lon;//经度
    @ApiModelProperty(value = "纬度")
    private Double lat;//纬度

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Double getLon() {
        return lon;
    }

    public void setLon(Double lon) {
        this.lon = lon;
    }

    public Double getLat() {
        return lat;
    }

    public void setLat(Double lat) {
        this.lat = lat;
    }

}

2、Test.java(测试类)

package net.beidousky.web.app.service.impl;

import com.alibaba.druid.pool.DruidDataSource;
import net.beidousky.web.app.domain.TestVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

/**
 * 批量入库测试
 * @author jwb
 */
@Service
public class Test{

    @Autowired
    private DruidDataSource dataSource;

    /**
     * 批量提交入库
     * @param list
     */
    private int insertStandardizationData(List<TestVo> list) {
        StringBuffer sql = new StringBuffer();
        try {
            sql.append("insert into test (age,name,address,lon,lat) values (?,?,?,?,?)");
            //获取数据库连接
            Connection con = dataSource.getConnection();
            //关闭事务自动提交
            con.setAutoCommit(false);
            PreparedStatement pst =  con.prepareStatement(sql.toString());
            int num = 0;
            for (int i = 0; i < list.size(); i++) {
                num++;
                TestVo testVo = (TestVo)list.get(i);
                pst.setInt(1, testVo.getType());
                pst.setString(2, testVo.getName());
                pst.setString(3, testVo.getAddress());
                pst.setDouble(4, testVo.getLon());
                pst.setDouble(5, testVo.getLat());
                pst.addBatch();//把一个SQL命令加入命令列表
                if (num%2000==0 || num==list.size()) {
                    pst.executeBatch();
                    con.commit();
                }
            }
            pst.close();
            con.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }


}

创作不易,大家帮忙关注一下我的公众号,感谢。
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值