如何使用MyBatis / Spring进行批处理操作?

我想知道如何使用MyBatis 3和Spring 3使用我的插入语句实现批处理操作吗?

例如,这是当前正在执行的操作:

spring.xml:

<bean id="jndiTemplateDatasource" class="org.springframework.jndi.JndiTemplate">

<property name="environment">

<props>

<prop key="java.naming.factory.initial">${context.factory}</prop>

</props>

</property>

</bean>

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">

<property name="jndiTemplate" ref="jndiTemplateDatasource"/>

<property name="jndiName" value="${connectionpool.jndi}"/>

</bean>

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

<property name="dataSource" ref="dataSource"/>

</bean>

<tx:annotation-driven transaction-manager="transactionManager"/>

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

<property name="dataSource" ref="dataSource" />

<property name="configLocation" value="classpath:mybatis-config.xml"/>

</bean>

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

<property name="basePackage" value="com.test" />

</bean>

MyService.xml:

<insert id="insertMyRecord" parameterType="com.test.MyRecord"  >

insert into ... // code removed

</insert>

MyService.java:

public interface MyService {

public void insertMyRecord (MyRecord);

}

MyController.java:

@Controller

public class MyController {

@Autowired

private MyService myService;

@Transactional

@RequestMapping( .... )

public void bulkUpload (@RequestBody List<MyRecord> myRecords) {

for (MyRecord record : myRecords) {

myService.insertMyRecord(record);

}

}

}

免责声明:这只是用于演示目的的伪代码

那么我该怎么做才能将其变成批处理流程呢?

理想情况下,我希望能够以最少的“侵入”代码来做到这一点,即更优选使用批注,但如果不可能的话,下一件最好的事情是什么?

同样,仅需要为此一项服务配置此功能,而不需要为项目中的所有内容配置此功能。

回答:

这是正在运行且经过测试的示例…使用批处理(ibatis + java)更新多行

在这个前 我正在将参加人数从表格更新为partyid。

public static int updateBatch(List<MyModel> attendingUsrList) {

SqlSession session = ConnectionBuilderAction.getSqlSession();

PartyDao partyDao = session.getMapper(PartyDao.class);

try {

if (attendingUsrList.size() > 0) {

partyDao.updateAttendingCountForParties(attendingUsrList);

}

session.commit();

} catch (Throwable t) {

session.rollback();

logger.error("Exception occurred during updateBatch : ", t);

throw new PersistenceException(t);

} finally {

session.close();

}

}

定义变量的模型类:

public class MyModel  {

private long attending_count;

private String eid;

public String getEid() {

return eid;

}

public void setEid(String eid) {

this.eid = eid;

}

public long getAttending_count() {

return attending_count;

}

public void setAttending_count(long attending_count) {

this.attending_count = attending_count;

}

}

party.xml代码

批处理执行的实际查询

<foreach collection="attendingUsrList" item="model"  separator=";">

UPDATE parties SET attending_user_count = #{model.attending_count}

WHERE fb_party_id = #{model.eid}

</foreach>

此处的接口代码

public interface PartyDao {

int updateAttendingCountForParties (@Param("attendingUsrList") List<FBEventModel>attendingUsrList);

}

这是我的批处理会话代码

public static synchronized SqlSession getSqlBatchSession() {

ConnectionBuilderAction connection = new ConnectionBuilderAction();

sf = connection.getConnection();

SqlSession session = sf.openSession(ExecutorType.BATCH);

return session;

}

SqlSession session = ConnectionBuilderAction.getSqlSession();

以上是 如何使用MyBatis / Spring进行批处理操作? 的全部内容, 来源链接: utcz.com/qa/434643.html

回到顶部