Introduction to the use of bboss high-performance db batch function

The bboss persistence layer adds a simple and efficient db batch function in v5.0.3.5. This article introduces the use method.
First import the bboss persistence layer package in the project:
maven coordinates
<dependency>
    <groupId>com.bbossgroups</groupId>
    <artifactId>bboss-persistent</artifactId>
    <version>5.0.3.5</version>
</dependency>
gradle Coordinate
compile 'com.bbossgroups:bboss-persistent:5.0.3.5'

Lightweight batch method
directly operates the component SQLExecutor
com.frameworkset.common.poolman.SQLExecutor
public static <T> void executeBatch(String sql,List<T> datas,int batchsize, BatchHandler<T> batchHandler) throws SQLException	
//Specify the data source dbname
public static <T> void executeBatch(String dbname,String sql,List<T> datas,int batchsize, BatchHandler<T> batchHandler) throws SQLException



The component that loads the sql configuration file ConfigSQLExecutor
com.frameworkset.common.poolman.ConfigSQLExecutor
public <T> void executeBatch(String sqlname,List<T> datas,int batchsize, BatchHandler<T> batchHandler) throws SQLException
//Specify the data source dbname
public <T> void executeBatch(String dbname,String sqlname,List<T> datas,int batchsize, BatchHandler<T> batchHandler) throws SQLException


Batch statement parameter setter:
package com.frameworkset.common.poolman;
/**
 * Lightweight jdbc batch operation record setter
 */

import java.sql.PreparedStatement;
import java.sql.SQLException;

public interface BatchHandler<T> {
	/**
	 *
	 * @param stmt jdbc PreparedStatement   parameterIndex the first parameter is 1, the second is 2, ...
	 *                                      x the parameter value
	 * @param record the variable of the current operation
	 * @param i row index
	 * @throws SQLException
	 */
	public void handler(PreparedStatement stmt,T record,int i) throws SQLException;

}


The example
of using the SQLExecutor component is introduced as an example as follows:
@Test
    public void testBatch() throws SQLException {
        List<Map<String,String>> datas = new ArrayList<Map<String,String>>();//构造数据
        for(int i = 0; i < 100000; i ++){
            Map<String,String> data = new HashMap<String, String>();
            if(i % 3 == 0)
                data.put("name","jack_"+i);
            else if(i % 3 == 1)
                data.put("name","brown_"+i);
            else if(i % 3 == 2)
                data.put("name","john_"+i);
            data.add(data);
        }
        SQLExecutor.delete("delete from batchtest");//Clear table data
// batch execution
        SQLExecutor.executeBatch("insert into batchtest (name) values(?)", datas, 10,new BatchHandler<Map<String,String>>() {
            @Override
            public void handler(PreparedStatement stmt, Map<String,String> record, int i) throws SQLException {
                stmt.setString(1,record.get("name"));
            }
        });
    }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326450929&siteId=291194637