Wednesday, April 3, 2019

Spring Boot ibatis Batch insert with Annotations

Spring Boot framework, which has changed the way Java developers use Spring framework for writing Java applications. And also ibatis is powerful lightweight ORM framework. Which i use most of the time.

Lets see how to create ibatis Batch insert with annotations

gradle build file

dependencies {
    compile 'org.json:json:20131018'
    compile 'org.glassfish.jersey.core:jersey-client:2.27'
    compile 'org.glassfish.jersey.inject:jersey-hk2:2.27'
    implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.0.0'
    compileOnly 'org.projectlombok:lombok'
    runtimeOnly 'mysql:mysql-connector-java'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

Mapper Class

package com.scheduler.mapper;

import com.scheduler.model.Resource;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.util.List;

@Mapper
public interface ResourceMapper {

    @Select("select * from resource where account_id = #{accountId}")
    List<Resource> findAll(@Param("accountId") long accountId);

    @Insert("INSERT IGNORE INTO resource(id, account_id, region_id) VALUES(#{id},#{accountId}, #{regionId})")
    void insert(Resource resource);

}


Dao Class

package com.scheduler.dao;
import com.scheduler.mapper.ResourceMapper;
import com.scheduler.model.Resource;
import lombok.extern.java.Log;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.logging.Level;
@Log
@Repository
public class ResourceDao {
    private ResourceMapper resourceMapper;
    private SqlSessionFactory sqlSessionFactory;
    public ResourceDao(ResourceMapper resourceMapper, SqlSessionFactory sqlSessionFactory) {
        this.resourceMapper = resourceMapper;
        this.sqlSessionFactory = sqlSessionFactory;
    }
    public List<Resource> findAll(Long accountId) {
        return this.resourceMapper.findAll(accountId);
    }
    @Transactional()
    public void insert(List<Resource> newResource) {
        try {
            try (SqlSession sqlSession = this.sqlSessionFactory.openSession(ExecutorType.BATCH)) {
                ResourceMapper batchMapper = sqlSession.getMapper(ResourceMapper.class);
                for (Resource resource : newResource) {
                    batchMapper.insert(resource);
                }
                sqlSession.commit();
            }
        } catch (Exception e) {
            log.log(Level.WARNING, "error occurs while adding data", e);
        }

    }

}


By default ibatis uses SimpleExecutor (ExecutorType.SIMPLE) but if you need to do batch insert need to use BatchExecutor which we are telling to the ibatis by this.sqlSessionFactory.openSession(ExecutorType.BATCH))