现在编写代码,总时刻要求自己,不只是实现功能,而且要质量更高。
最近使用ibatis框架,其灵活的sql编写,能够颗粒度的优化,蛮不错的。
另外使用批量insert时,找了好久,终于被我找到一种合适的批量添加操作。强调一下,本人使用的是oracle数据库。
传到DAO层的是一个List集合类
insert all
into ckb_dna_volume_info
(dna_num,d_board_name,source_cryovial_id,date_processed,buffer_volume,buffer_reagent,create_user)
values
select * from dual
另外,在网上有另外的两种批量insert的写法,我这边使用oracle+ibatis测试了,行不通,方法如下(提示错误信息:ORA-00936: missing expression):
insert into ckb_dna_volume_info
(dna_num,d_board_name,source_cryovial_id,date_processed,buffer_volume,buffer_reagent,create_user)
values
select dna_num,d_board_name,source_cryovial_id,date_processed,buffer_volume,buffer_reagent,create_user from
(
select
#list[].gCryovialId# as dna_num, #list[].gBoxId# as d_board_name, #list[].fCryovialId# as source_cryovial_id,
#list[].operatdate# as date_processed, #list[].additionalBufferVolume# as buffer_volume, #list[].bufferReagent# as buffer_reagent,
#list[].operator# as create_user
from dual
)
insert into ckb_dna_volume_info
(dna_num,d_board_name,source_cryovial_id,date_processed,buffer_volume,buffer_reagent,create_user)
values
上述只是在sql里写批量,其实ibatis还可以在DAO层用java代码实现,写法还可以如下:
try {
this.getSqlMapClient().startTransaction();
this.getSqlMapClient().startBatch();
for(CkbInfo info :infos){
this.getSqlMapClientTemplate().insert("CKBDNAInfo.addCkbRedilutedInfo", info);
}
this.getSqlMapClient().executeBatch();
this.getSqlMapClient().commitTransaction();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
try {
this.getSqlMapClient().endTransaction();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
但是上述方法新开启了事务却导致了事务问题,所以还是用另外一种方式来处理,即基于回调方式的实现(当然,要注意你的数据库游标设置的最大值是多少,尽可能大些,不然会超出游标的最大设置值):
// 执行回调
sqlMapClientTemplate.execute(new SqlMapClientCallback() {
// 实现回调接口
public Object doInSqlMapClient(SqlMapExecutor executor)
throws SQLException {
// 开始批处理
executor.startBatch();
for (CkbInfo info :infos) {
// 插入操作
executor.insert("CKBDNAInfo.addCkbRedilutedInfo", info);
}
// 执行批处理
executor.executeBatch();
return null;
}
});