Home  >  Article  >  Java  >  How does springboot integrate mybatis to implement database update batch processing?

How does springboot integrate mybatis to implement database update batch processing?

WBOY
WBOYforward
2023-05-12 20:52:181182browse

    springboot integrates mybatis to implement database update batch processing

    1. Write methods in the mapper interface

    /**
     * 修改book表中的销量和库存
     * 要使用批处理
     */
    Integer batchBookCountStork(@Param("bookList") List bookList);

    2. In mapper.xml Write the corresponding update sql statement in

    
        UPDATE t_book
        
            
                WHEN #{book.bookId} THEN sales+#{book.count}
            
            
                WHEN #{book.bookId} THEN stock-#{book.count}
            
        
        
            
                #{book.bookId}
            
        
      

    3. The sql statement process of this configuration file is as follows:

    update t_book(表名)
    set sales(这个是数据库的销量字段名) = case book_id(这个是数据库的id字段名)
        when bookid(从list集合中取出来的) then sales+(从集合中取出的数据)
        ...(这里可以一直进行拼接)
      end,
        stock(这个是数据库的库存字段名) = CASE book_id(这个是数据库的id字段名)
        when bookid(从list集合中取出来的) then stock-(从集合中取出数据)
        ...(这里可以一直进行拼接)
      end,
    where `book_id`(这个是数据库的id字段名) IN(bookid(从list集合中取出来),bookid(从list集合中取出来)...)

    4. The meaning of this sql statement:

    Update table The data is based on the id value traversed through the collection, set the field name to be updated, and bind the field value to be updated with the primary key id of the table. When the primary key id is consistent with the id value taken out from the list, let this The field name to be updated, take the value after then

    Mybatis batch update databaseMybatisBatchUtils batchInsertupdate spring boot

    MybatisBatchUtils

        int cnt = mybatisBatchUtils.batchUpdateOrInsert(addList, UiConfigDetailMapper.class,
                                (item, uiConfigDetailMapper) -> uiConfigDetailMapper.insertSelective(item));
    package cn.XXX.dao.serivce.common;
    
    import com.XXX.doctorusercenter.exception.BusinessException;
    import lombok.extern.slf4j.Slf4j;
    import org.apache.ibatis.session.ExecutorType;
    import org.apache.ibatis.session.SqlSession;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.springframework.stereotype.Component;
    
    import javax.annotation.Resource;
    import java.util.List;
    import java.util.function.BiFunction;
    
    
    @Slf4j
    @Component
    public class MybatisBatchUtils {
    
        /**
         * 每次处理1000条
         */
        private static final int BATCH_SIZE = 1000;
    
        @Resource
        private SqlSessionFactory sqlSessionFactory;
    
        /**
         * 批量处理修改或者插入
         *
         * @param data        需要被处理的数据
         * @param mapperClass Mybatis的Mapper类
         * @param function    自定义处理逻辑
         * @return int 影响的总行数
         */
        public  int batchUpdateOrInsert(List data, Class mapperClass, BiFunction function)  {
            int i = 1;
            SqlSession batchSqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
            try {
                U mapper = batchSqlSession.getMapper(mapperClass);
                int size = data.size();
                for (T element : data) {
                  function.apply(element, mapper);
                    if ((i % BATCH_SIZE == 0) || i == size) {
                        batchSqlSession.flushStatements();
                    }
                    i++;
                }
                // 非事务环境下强制commit,事务情况下该commit相当于无效
                batchSqlSession.commit(true);
            } catch (Exception e) {
                batchSqlSession.rollback();
                // throw new BusinessException(e.getMessage());
                log.error("batchUpdateOrInsert", e);
            } finally {
                batchSqlSession.close();
            }
            return i - 1;
        }
    }

    The above is the detailed content of How does springboot integrate mybatis to implement database update batch processing?. For more information, please follow other related articles on the PHP Chinese website!

    Statement:
    This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete