这里的配置和http://my.oschina.net/u/2274874/blog/639504中用到的差不多,只是做了一些小改动。
把整合ibatis这段改一下就可以了,这里把标签改简洁了,不改也可以。
<!-- 整合mybatis --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="DataSource" /> <property name="configLocation" value="classpath:SqlMapConfig.xml" /> </bean>
这个DAO继承了SqlSessionDaoSupport,用到mybatis-spring,主要就是批量处理。在网上找了很多资料,发现很多都是通过改造xml的sql语句来达到批量操作的效果。这里我只是实验了批量插入的例子,其他的没试,估计也差不多吧,以后有用到再说,现在只是试试。
public class BaseDao extends SqlSessionDaoSupport { private static final Logger logger = Logger.getLogger(Thread .currentThread().getStackTrace()[1].getClassName()); @Resource(name = "sqlSessionFactory") private SqlSessionFactory sqlSessionFactory; private SqlSession batchSession; @PostConstruct public void SqlSessionFactory() { super.setSqlSessionFactory(sqlSessionFactory); } public int batchInsert(String statement, List<?> list) { batchSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false); int i = 0; for(int cnt = list.size(); i < cnt; i++) { batchSession.insert(statement, list.get(i)); if((i + 1) % Constants.BATCH_DEAL_NUM == 0) {//Constants.BATCH_DEAL_NUM为批量提交的条数 batchSession.flushStatements(); } } batchSession.flushStatements(); batchSession.close(); return i; } public int batchUpdate(String statement, List<?> list) { batchSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false); int i = 0; for(int cnt = list.size(); i < cnt; i++) { batchSession.update(statement, list.get(i)); if((i + 1) % Constants.BATCH_DEAL_NUM == 0) { batchSession.flushStatements(); } } batchSession.flushStatements(); batchSession.close(); return i; } public int batchDelete(String statement, List<?> list) { batchSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false); int i = 0; for(int cnt = list.size(); i < cnt; i++) { batchSession.delete(statement, list.get(i)); if((i + 1) % Constants.BATCH_DEAL_NUM == 0) { batchSession.flushStatements(); } } batchSession.flushStatements(); batchSession.close(); return i; } }
下面是一个使用的例子,用的是mybatis3.X的版本。
@Service("TestinfoDaoImpl") public class TestinfoDaoImpl extends BaseDao implements TestinfoDao { public void deleteTestinfo(Map<String, Object> index) { this.getSqlSession().delete("deleteTestinfo", index); } // 批量插入 public void insertBatchTestinfo(List<TestInfo> list) { this.batchInsert("insertTestinfo", list); } public void insertTestinfo(TestInfo instance) { this.getSqlSession().insert("insertTestinfo", instance); } public List<TestInfo> selectTestinfos(Map<String, Object> index) { return this.getSqlSession().selectList("selectTestinfos", index); } // 查询条数 public int selectTestinfosCount(Map<String, Object> index) { return this.getSqlSession().selectOne("selectTestinfosCount", index); } public void updateTestinfo(Map<String, Object> index) { this.getSqlSession().update("updateTestinfo", index); } }
插入语句的sql
<insert id="insertTestinfo" parameterType="TestInfo"> INSERT INTO TESTINFO ( TESTID, TESTNM, PASSWD, ROLEID, EMAIL, MOBILE, REMARK ) VALUES ( #{testid}, #{testnm}, #{passwd}, #{roleid}, #{email}, #{mobile}, #{remark} ) </insert>
插入的结果log,从log中可以看出是批量插入。这里为了测试好看,设置了50条提交一次。
Preparing: INSERT INTO TESTINFO ( TESTID, TESTNM, PASSWD, ROLEID, EMAIL, MOBILE, REMARK ) VALUES ( ?, ?, ?, ?, ?, ?, ? ) Parameters: test0(String), test0(String), 123456(String), 1(String), (String), (String), (String) Parameters: test1(String), test1(String), 123456(String), 1(String), (String), (String), (String) Parameters: test2(String), test2(String), 123456(String), 1(String), (String), (String), (String) ......省略n行 Parameters: test47(String), test47(String), 123456(String), 1(String), (String), (String), (String) Parameters: test48(String), test48(String), 123456(String), 1(String), (String), (String), (String) Parameters: test49(String), test49(String), 123456(String), 1(String), (String), (String), (String) stmt enter cache ooo Using Connection [oracle.jdbc.driver.T4CConnection@9209e8] Preparing: INSERT INTO TESTINFO ( TESTID, TESTNM, PASSWD, ROLEID, EMAIL, MOBILE, REMARK ) VALUES ( ?, ?, ?, ?, ?, ?, ? ) Parameters: test50(String), test50(String), 123456(String), 1(String), (String), (String), (String) Parameters: test51(String), test51(String), 123456(String), 1(String), (String), (String), (String) Parameters: test52(String), test52(String), 123456(String), 1(String), (String), (String), (String) ......省略n行 Parameters: test97(String), test97(String), 123456(String), 1(String), (String), (String), (String) Parameters: test98(String), test98(String), 123456(String), 1(String), (String), (String), (String) Parameters: test99(String), test99(String), 123456(String), 1(String), (String), (String), (String)