一、applicationContext.xml 配置内容:
<!--mapping datasource -->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!---data config file-->
<bean id="sqlMapClientTemplate"
class="org.springframework.orm.ibatis.SqlMapClientTemplate">
<property name="sqlMapClient">
<ref bean="sqlMapClient" />
</property>
</bean>
<!-- aop -->
<!-- 关联到类-->
<aop:config>
<aop:advisor
pointcut="execution(* com.cn.zy.dao.WebShiftAssetsDetailDao.processSplitCard(..))"
advice-ref="scAdvice" />
<aop:advisor
pointcut="execution(* com.cn.zy.dao.WebDispAssetsDetailDao.processSplitCard(..))"
advice-ref="scAdvice" />
</aop:config>
<!-- 关联到类中的方法 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="insert*" />
<tx:method name="update*" />
<tx:method name="del*" />
<tx:method name="add*" />
<tx:method name="modify*" />
<tx:method name="save*" />
<tx:method name="process*" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
<tx:advice id="scAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="process*" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
二、javaBean :
//符合xml配置的方法名称 processSplitCard (<tx:method name="process*" />)
public boolean processSplitCard(final Object objectid, final String sql,
final Map pm) throws Exception, DataAccessException {
....
this.getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
public Object doInSqlMapClient(SqlMapExecutor executor)
throws SQLException {
int commitSize = 1000;
boolean commitBatch = false;
executor.startBatch(); // 事务开始
//批量处理重复动作
for (int i = 0; i < size; i++) {
................
executor.insert("insertTblCard", item);
executor.update(sql, pm);
................
if ((i != 0) && (i % commitSize == 0)) {
commitBatch = true;
} else if (i == (size - 1)) {
commitBatch = true;
}
if (commitBatch) {
executor.executeBatch();
if (i != (size - 1)) {
executor.startBatch(); //批量处理时是否要启动下一次事务
}
commitBatch = false;
}
}
}
});
}