spring整合iBATIS问题

strike2368168 strike2368168 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
最近做个批处理的项目,用的是spring2.5+iBATIS2做框架
用的是iBATIS回调函数的方式做的批处理,但是遇到了一个问题做了批量插入后,数据库却没有反应
不知为什么,请高手帮忙看看
还有一个问题就是用回调函数的方式,事物是不是不用手工启动。我在spring里把jdbc的自动提交设置成了FALSE
spring配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd "
default-autowire="byName">

<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<!--
<property name="url" value="jdbc:sqlserver://172.16.5.48:1433;DatabaseName=TAMS"/>
<property name="username" value="sa"/>
<property name="password" value="TEST"/>
-->

<property name="url" value="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=TAMS;"/>
<property name="username" value="ad"/>
<property name="password" value="ad"/>
<!-- 连接池启动时的初始值 -->
<property name="initialSize" value="1"/>
<!-- 连接池的最大值 -->
<property name="maxActive" value="500"/>
<!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
<property name="maxIdle" value="2"/>
<!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
<property name="minIdle" value="1"/>
<property name="defaultAutoCommit" value="false"/>
</bean>

<!-- 配置事务管理器-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>

<!-- 配置业务bean -->

<!-- 采用@Transactional注解方式来使用事务
<tx:annotation-driven transaction-manager="txManager"/> -->

<bean id="sqlMapClient"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation"
value="classpath:sqlmap-config.xml" />
<property name="dataSource" ref="dataSource" />
</bean>

<bean id="baseTxService"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
abstract="true">
<property name="transactionManager" ref="txManager" />
<property name="proxyTargetClass" value="true" />
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="query*">readOnly</prop>
<prop key="get*">readOnly</prop>
<prop key="del*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>

<bean id="textDao" class="com.strike.java.core.db.daoImpl.TextDaoImpl">
<property name="dataSource" ref="dataSource"></property>
<property name="sqlMapClient" ref="sqlMapClient"></property>
</bean>
</beans>

daoimpl

/**
*
*/
package com.strike.java.core.db.daoImpl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.springframework.orm.ibatis.SqlMapClientCallback;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.ibatis.sqlmap.client.SqlMapExecutor;
import com.strike.java.core.db.dao.TextDao;
import com.strike.java.core.db.daoBean.TextDaoBean;
import com.strike.java.core.pattern.db.BaseBatch;

/**
* @author gaoweizhuang
*
*/
public class TextDaoImpl extends SqlMapClientDaoSupport implements TextDao {

/* (non-Javadoc)
* @see com.strike.java.core.db.dao.TextDao#text()
*/
@Override
public int text() throws SQLException {
// TODO Auto-generated method stub
final List<TextDaoBean> parm = new ArrayList();

TextDaoBean textDaoBean = new TextDaoBean();
textDaoBean.setUsername("2");
textDaoBean.setPassword("2");
parm.add(textDaoBean);

// 初始化
final int size = 0;

// 执行批处理操作模板
getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
public Object doInSqlMapClient(SqlMapExecutor executor)
throws SQLException {
System.out.println("当前事物状态: " + getSqlMapClient().
getDataSource().getConnection().getAutoCommit());
// 开启事物
getSqlMapClient().startTransaction();
// 开始插入批处理
executor.startBatch();
// 变量集合
Iterator<TextDaoBean> iter = parm.iterator();

// 日志记录批量插入开始
logger.info("batch start:");
// 循环操作
// while(iter.hasNext()){
// // 插入数据
// executor.insert("BC_MST_PREF_SqlMap.insertUser", parm.get(0));
// }
TextDaoBean textDaoBean = iter.next();
executor.insert("BC_MST_PREF_SqlMap.insertUser", textDaoBean);

// 执行批处理
executor.executeBatch();
// 执行事物
getSqlMapClient().commitTransaction();
// 结束事物
getSqlMapClient().endTransaction();

// 日志记录批处理结束
logger.info("本次批处理插入数据" + size + "条");

// 返回处理条数
return size;
}
});
// log结束
logger.info("batch end:");
return size;

// super.insertBatch(parm);
}

}


sqlxml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace="BC_MST_PREF_SqlMap">
<typeAlias alias="TextDaoBean" type="com.strike.java.core.db.daoBean.TextDaoBean"/>
<resultMap id="resultMap" class="com.strike.java.core.db.daoBean.TextDaoBean">
<result property="username" column="USERNAME" columnIndex="1"/>
<result property="password" column="PASSWORD" columnIndex="2"/>
</resultMap>

<insert id="insertUser" parameterClass="TextDaoBean">
insert into t_user (USERNAME,PASSWORD) values (#username#,#password#)
</insert>

</sqlMap>
...全文

相关推荐

评论 10
收藏 已收藏
分享
返回顶部
strike2368168
Web 开发 81,108 成员 341,721 内容