bboss高性能db批处理功能使用方法介绍

阅读更多
bboss持久层在v5.0.3.5中新增简单的高效的db批处理功能,本文介绍使用方法。
首先在项目中导入bboss 持久层包:
maven坐标

    com.bbossgroups
    bboss-persistent
    5.1.8

gradle坐标
compile 'com.bbossgroups:bboss-persistent:5.1.8'

轻量级批处理方法
直接操作sql语句的组件SQLExecutor
com.frameworkset.common.poolman.SQLExecutor
public static  void executeBatch(String sql,List datas,int batchsize, BatchHandler batchHandler) throws SQLException	
//指定数据源dbname
public static  void executeBatch(String dbname,String sql,List datas,int batchsize, BatchHandler batchHandler) throws SQLException



加载sql配置文件的组件ConfigSQLExecutor
com.frameworkset.common.poolman.ConfigSQLExecutor
public  void executeBatch(String sqlname,List datas,int batchsize, BatchHandler batchHandler) throws SQLException
//指定数据源dbname
public  void executeBatch(String dbname,String sqlname,List datas,int batchsize, BatchHandler batchHandler) throws SQLException


批处理语句参数设置器:
package com.frameworkset.common.poolman;
/**
 * 轻量级jdbc批处理操作记录设置器
 */

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

public interface BatchHandler {
	/**
	 *
	 * @param stmt jdbc PreparedStatement   parameterIndex the first parameter is 1, the second is 2, ...
	 *                                      x the parameter value
	 * @param record 当前操作的变量
	 * @param i 行索引
	 * @throws SQLException
	 */
	public void handler(PreparedStatement stmt,T record,int i) throws SQLException;

}


使用实例
以SQLExecutor组件来做为例子介绍如下:
@Test
    public void testBatch() throws SQLException {
        List> datas = new ArrayList>();//构造数据
        for(int i = 0; i < 100000; i ++){
            Map data = new HashMap();
            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);
            datas.add(data);
        }
        SQLExecutor.delete("delete from batchtest");//清空表数据
//批处理执行
        SQLExecutor.executeBatch("insert into batchtest (name) values(?)", datas, 10,new BatchHandler>() {
            @Override
            public void handler(PreparedStatement stmt, Map record, int i) throws SQLException {
                stmt.setString(1,record.get("name"));
            }
        });
    }

你可能感兴趣的:(bboss,持久层,批处理)