Programming & Development / April 19, 2025

Using MyBatis for Update Operations in Spring Batch: Manual vs. Batch Writer Approaches

Spring Batch MyBatis MyBatisBatchItemWriter XML Mapper ItemWriter batch update Java SqlSessionFactory ApiRetry batch processing database updates

✅ 1. Manual MyBatis Update (Per Row)

This method manually invokes an update per item.

🔹 XML Mapper (ApiRetryMapper.xml)

xml

<mapper namespace="com.example.batch.mapper.ApiRetryMapper">
    <update id="updateApiRetryStatus">
        UPDATE ApiRetry
        SET status = #{status}, retryCount = #{retryCount}, updatedAt = NOW()
        WHERE id = #{id}
    </update>
</mapper>

🔹 Mapper Interface

java

public interface ApiRetryMapper {
    void updateApiRetryStatus(@Param("id") Long id,
                              @Param("status") String status,
                              @Param("retryCount") int retryCount);
}

🔹 ItemWriter Implementation

java

public class ApiRetryItemWriter implements ItemWriter<ApiRetry> {

    @Autowired
    private ApiRetryMapper apiRetryMapper;

    @Override
    public void write(List<? extends ApiRetry> items) {
        for (ApiRetry item : items) {
            apiRetryMapper.updateApiRetryStatus(item.getId(), item.getStatus(), item.getRetryCount());
        }
    }
}

✅ 2. Batch Update with MyBatisBatchItemWriter (Recommended for Performance)

Uses Spring's MyBatis batch writer which sends batch SQL to the DB.

🔹 XML Mapper (ApiRetryMapper.xml)

xml

<mapper namespace="com.example.batch.mapper.ApiRetryMapper">
    <update id="batchUpdateApiRetryStatus">
        UPDATE ApiRetry
        SET status = #{status}, retryCount = #{retryCount}, updatedAt = NOW()
        WHERE id = #{id}
    </update>
</mapper>

🔹 Mapper Interface

java

public interface ApiRetryMapper {
    void batchUpdateApiRetryStatus(ApiRetry apiRetry); // This will be called for each item in batch
}

🔹 MyBatisBatchItemWriter Bean Configuration

java

@Bean
public MyBatisBatchItemWriter<ApiRetry> myBatisBatchItemWriter(SqlSessionFactory sqlSessionFactory) {
    MyBatisBatchItemWriter<ApiRetry> writer = new MyBatisBatchItemWriter<>();
    writer.setSqlSessionFactory(sqlSessionFactory);
    writer.setStatementId("com.example.batch.mapper.ApiRetryMapper.batchUpdateApiRetryStatus");
    return writer;
}

🔁 Spring Batch Config (Common for Both)

java

@Bean
public Step apiRetryStep(StepBuilderFactory stepBuilderFactory,
                         MyBatisPagingItemReader<ApiRetry> apiRetryReader,
                         ItemWriter<ApiRetry> writer) {
    return stepBuilderFactory.get("apiRetryStep")
        .<ApiRetry, ApiRetry>chunk(10)
        .reader(apiRetryReader)
        .writer(writer)
        .build();
}

@Bean
@StepScope
public MyBatisPagingItemReader<ApiRetry> apiRetryReader(SqlSessionFactory sqlSessionFactory) {
    MyBatisPagingItemReader<ApiRetry> reader = new MyBatisPagingItemReader<>();
    reader.setSqlSessionFactory(sqlSessionFactory);
    reader.setQueryId("com.example.batch.mapper.ApiRetryMapper.selectApiRetries");
    return reader;
}

✅ Which Should You Use?

FeatureManual ItemWriterMyBatisBatchItemWriterEase of debugging✅ Easy⚠️ More indirectPerformance⚠️ Slower for large data✅ Optimized for batch writesTransactional✅ Manual management✅ Handled by batch contextParallel step support✅ Yes✅ Yes

If you're dealing with large datasets, go with MyBatisBatchItemWriter. For simple updates or low volume, the manual writer is just fine.


Comments

No comments yet

Add a new Comment

NUHMAN.COM

Information Technology website for Programming & Development, Web Design & UX/UI, Startups & Innovation, Gadgets & Consumer Tech, Cloud Computing & Enterprise Tech, Cybersecurity, Artificial Intelligence (AI) & Machine Learning (ML), Gaming Technology, Mobile Development, Tech News & Trends, Open Source & Linux, Data Science & Analytics

Categories

Tags

©{" "} Nuhmans.com . All Rights Reserved. Designed by{" "} HTML Codex