java – 如何使用MyBatis / Spring实现批处理?

前端之家收集整理的这篇文章主要介绍了java – 如何使用MyBatis / Spring实现批处理?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想知道如何使用MyBatis 3& amp;春天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();
原文链接:https://www.f2er.com/java/125197.html

猜你在找的Java相关文章