设计的话, 是在dao层写批量新增的方法,以及实现类dao的实现类, 在service调用这个dao就可以了! 不过最终走的还是单个只不过是集合的遍历, 所以不用再mapper.xml里面配置方法。
IReconBankOrderCpsBatchDao里面的方法:
public interface IReconBankOrderCpsBatchDao {
/**
* 保存多条记录
*
* @param l_eos : 交易对账-控制表-EO列表
*/
void saveAll(List<ReconBankOrderCpsEo> l_eos);
/**
* 批量更新记录
* @param l_eos : 交易对账-控制表-EO列表
*/
void batchUpdate(List<ReconBankOrderCpsEo> l_eos);
/**
* 批量删除
* @param eos
*/
void batchDelete(List<ReconBankOrderCpsEo> eos);
}
dao的实现类:
@Repository("reconBankOrderCpsBatchDao")
public class ReconBankOrderCpsBatchDaoImpl implements
IReconBankOrderCpsBatchDao {
static Logger logger = LogManager.getLogger(ReconBankOrderCpsBatchDaoImpl.class);
@Resource(name = "reconTransBaseSqlSessionTemplate")
private SqlSessionTemplate sqlSessionTemplate;
@Override
public void saveAll(List<ReconBankOrderCpsEo> l_eos) {
SqlSession session = sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH, false);
IReconBankOrderCpsDao dao = session.getMapper(IReconBankOrderCpsDao.class);
int size = l_eos.size();
try {
for (int i = 0; i < size; i++) {
dao.save(l_eos.get(i));
if (i % 1000 == 0 || i == size - 1) {
//手动每1000个一提交,提交后无法回滚
session.commit();
//清理缓存,防止溢出
session.clearCache();
}
}
} catch (Exception e) {
logger.error("批量保存失败:" ,e);
session.rollback();
} finally {
session.close();
}
}
@Override
public void batchUpdate(List<ReconBankOrderCpsEo> l_eos) {
SqlSession session = sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH, false);
IReconBankOrderCpsDao dao = session.getMapper(IReconBankOrderCpsDao.class);
int size = l_eos.size();
try {
for (int i = 0; i < size; i++) {
dao.update(l_eos.get(i));
if (i % 500 == 0 || i == size - 1) {
//手动每500个一提交,提交后无法回滚
session.commit();
//清理缓存,防止溢出
session.clearCache();
}
}
} catch (Exception e) {
session.rollback();
} finally {
session.close();
}
}
@Override
public void batchDelete(List<ReconBankOrderCpsEo> eos) {
SqlSession session = sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH, false);
IReconBankOrderCpsDao dao = session.getMapper(IReconBankOrderCpsDao.class);
int size = eos.size();
try {
for (int i = 0; i < size; i++) {
dao.delete(eos.get(i).getBankOrderId());
if (i % 1000 == 0 || i == size - 1) {
//手动每1000个一提交,提交后无法回滚
session.commit();
//清理缓存,防止溢出
session.clearCache();
}
}
} catch (Exception e) {
session.rollback();
} finally {
session.close();
}
}
}
使用的时候只需要在service里面调用dao就可以了。
对于批量新增, 也可以在mapper.xml中配置方法, 实现批量新增。
<!-- 保存多条记录 -->
<insert id="saveAll" parameterType="java.util.List">
INSERT INTO RECONCAV.T_CBS_RECON_BANKORDER_HANDLE
<trim prefix="(" suffix=")" >
<include refid="allColumns" />
</trim>
SELECT RECONCAV.SEQ_bankorder_hd_tmp.NEXTVAL,RC.* from(
<foreach collection="list" item="eo" index="index" separator="union all">
SELECT
#{eo.reconId ,jdbcType=VARCHAR},
#{eo.reconDate ,jdbcType=TIMESTAMP},
#{eo.orderDate ,jdbcType=TIMESTAMP},
#{eo.bankTransType ,jdbcType=NUMERIC},
#{eo.bankAcctId ,jdbcType=VARCHAR},
#{eo.productNo ,jdbcType=NUMERIC},
#{eo.bankNo ,jdbcType=NUMERIC},
#{eo.bankName ,jdbcType=VARCHAR},
#{eo.channelNo ,jdbcType=VARCHAR},
#{eo.channelName ,jdbcType=VARCHAR},
#{eo.merchantCode ,jdbcType=VARCHAR},
#{eo.orderAmount ,jdbcType=NUMERIC},
#{eo.cost ,jdbcType=NUMERIC},
#{eo.settleAmount ,jdbcType=NUMERIC},
#{eo.fundsReconStatus ,jdbcType=NUMERIC},
#{eo.aprrovalStatus ,jdbcType=NUMERIC},
#{eo.submitBy ,jdbcType=VARCHAR},
#{eo.submitDetail ,jdbcType=VARCHAR},
#{eo.submitTime ,jdbcType=TIMESTAMP},
#{eo.approvalBy ,jdbcType=VARCHAR},
#{eo.aprrovalDetail ,jdbcType=VARCHAR},
#{eo.aprrovalTime ,jdbcType=TIMESTAMP},
#{eo.memo ,jdbcType=VARCHAR},
#{eo.crtTime ,jdbcType=TIMESTAMP},
#{eo.updTime ,jdbcType=TIMESTAMP},
#{eo.uploadId ,jdbcType=NUMERIC},
#{eo.oraTid ,jdbcType=NUMERIC},
#{eo.idTxn ,jdbcType=VARCHAR},
#{eo.errorTransCode ,jdbcType=VARCHAR},
#{eo.oldTransType ,jdbcType=NUMERIC},
#{eo.accountBankName ,jdbcType=VARCHAR},
#{eo.accountBankFullName ,jdbcType=VARCHAR},
#{eo.payee ,jdbcType=VARCHAR},
#{eo.failReason ,jdbcType=VARCHAR},
#{eo.bankReturnDate ,jdbcType=TIMESTAMP},
#{eo.accountBank ,jdbcType=VARCHAR},
#{eo.traceNo ,jdbcType=VARCHAR}
FROM DUAL
</foreach>
) RC
</insert>