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

bboss持久层在v5.0.3.5中新增简单的高效的db批处理功能,本文介绍使用方法。
[size=large][b]首先在项目中导入bboss 持久层包:[/b][/size]
[size=large][b]maven坐标[/b][/size]
<dependency>
<groupId>com.bbossgroups</groupId>
<artifactId>bboss-persistent</artifactId>
<version>5.0.3.5</version>
</dependency>
[size=large][b]gradle坐标[/b][/size]
compile 'com.bbossgroups:bboss-persistent:5.0.3.5'

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



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


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

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 当前操作的变量
* @param i 行索引
* @throws SQLException
*/
public void handler(PreparedStatement stmt,T record,int i) throws SQLException;

}


[size=large][b]使用实例[/b][/size]
以SQLExecutor组件来做为例子介绍如下:
@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);
datas.add(data);
}
SQLExecutor.delete("delete from batchtest");//清空表数据
//批处理执行
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"));
}
});
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值