Java inserts large amount of data into database and submits it in batches

Go directly to code
1, TestVo.java (test entity class)

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 (test class)

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;
    }


}

Creation is not easy. Please help me follow my official account. Thank you.
Insert image description here

Guess you like

Origin blog.csdn.net/qq_41060647/article/details/131937539
Recommended