欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ibatis 批量 事物提交 博客分类: ibatis 批量 insert 提交数据 事物提交  

程序员文章站 2024-03-25 16:20:04
...

用 iabtis 

<iterate conjunction="">

 批量提交,缺点是提交数量有限制,size<1000;

批量提交: http://fengxiaoshuang429201406254717.iteye.com/blog/2392798

 

 

大量数据的话用事物提交:

我这边设置的是,一条一条insert,满5000条,事物提交;

 

如果过来一万条数据,事物会分成两批提交;

 

service impl实现层:

/** 5000条提交一次 */
    public static final int        BATCH_SIZE           = 5000;

    /** 事务模板 */
    protected TransactionTemplate transactionTemplate;

    /** sql执行模板 */
    protected SqlMapClientTemplate sqlMapClientTemplate = new SqlMapClientTemplate();
    private SqlMapClient sqlMapClient;
	
	 public SqlMapClientTemplate getSqlMapClientTemplate() {
        return sqlMapClientTemplate;
    }

    public void setSqlMapClientTemplate(SqlMapClientTemplate sqlMapClientTemplate) {
        this.sqlMapClientTemplate = sqlMapClientTemplate;
    }

    public SqlMapClient getSqlMapClient() {
        return sqlMapClient;
    }

    public void setSqlMapClient(SqlMapClient sqlMapClient) {
        this.sqlMapClient = sqlMapClient;
        this.sqlMapClientTemplate.setSqlMapClient(sqlMapClient);
    }

    public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
        this.transactionTemplate = transactionTemplate;
    }
	
	
	 @Override
    public void getBankHandlingCharge(final List<PostAccountVO> vo) {

        sqlMapClientTemplate.execute(new SqlMapClientCallback() {
            public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
                executor.startBatch();
                int indexInBatch = 0;
                List<PostAccountInfoDO> infoDOList = new LinkedList<PostAccountInfoDO>();
                for (PostAccountVO poa : vo){
                    PostAccountInfoDO infoDO = new PostAccountInfoDO();
                    infoDO.setAmount(poa.getAmount());
                    infoDO.setFundCategory(poa.getFundCategory());
                    infoDO.setPostStaus(poa.getPostStaus());
                    infoDO.setFundChannelCode(poa.getFundChannelCode());
                    infoDO.setHappenDate(poa.getHappenDate());
                    infoDO.setRemarks(poa.getRemarks());
                    infoDO.setCreater(poa.getCreater());
                    infoDO.setCreateTime(poa.getCreateTime());
                    infoDO.setAuditor(poa.getCreater());
                    infoDO.setAuditorTime(poa.getCreateTime());
                    infoDO.setDeleteFlag(poa.getDeleteFlag());
                    infoDO.setInstOrder(poa.getInstOrder());
                    infoDOList.add(infoDO);
                }

                for (PostAccountInfoDO postAccountInfoDO:infoDOList) {
                    if (indexInBatch > BATCH_SIZE) {//5000条作为一批处理。超过5000,则作为下一批。
                        executor.executeBatch();
                        executor.startBatch();
                        indexInBatch = 0;
                    }
                    executor.insert("MS-POST-ACCOUNT-INFO-INSERT", postAccountInfoDO);

                    indexInBatch++;

            }
                return executor.executeBatch();
            }
    });
    }

 

 

insert 方法不是dao层的实现方法,是executor里边的insert方法,也就是说这里直接访问了mapping.xml文件里边 id="MS-POST-ACCOUNT-INFO-INSERT" 的方法;

 

 mapping.xml层:

<insert id="MS-POST-ACCOUNT-INFO-INSERT">
   <selectKey resultClass="java.lang.String" keyProperty="accountSeqNo">
      <![CDATA[
       select seq_post_account_info.nextval from dual
   ]]>
   </selectKey>
   <![CDATA[
       insert into TB_POST_TABLE(ACCOUNT_SEQ_NO,AMOUNT,FUND_CATEGORY,POST_STAUS,FUND_CHANNEL_CODE,HAPPEN_DATE,REMARKS,CREATER,CREATE_TIME,AUDITOR,AUDITOR_TIME,DELETE_FLAG,INST_ORDER)
       values
       ( #accountSeqNo#, #amount#, #fundCategory#, #postStaus#, #fundChannelCode#, #happenDate#, #remarks#, #creater#, #createTime#, #auditor#, #auditorTime#, #deleteFlag#, #instOrder#)
   ]]>
</insert>

 

 

 

 

 

事务提交失败回滚:

 我在事物提交前抛出异常(如下图),数据并没有添加到数据库,应该是有回滚机制在里边吧


ibatis 批量 事物提交
            
    
    博客分类: ibatis   批量 insert 提交数据   事物提交  
 

 

 

 

 

 

 

 

  • ibatis 批量 事物提交
            
    
    博客分类: ibatis   批量 insert 提交数据   事物提交  
  • 大小: 19 KB