All Downloads are FREE. Search and download functionalities are using the official Maven repository.

fun.bigtable.kraken.util.DbUtils Maven / Gradle / Ivy

There is a newer version: 2.0.9.1
Show newest version
package fun.bigtable.kraken.util;

import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.support.TransactionSynchronization;
import org.springframework.transaction.support.TransactionSynchronizationManager;

import java.util.Collection;
import java.util.function.Consumer;

public abstract class DbUtils {

    private static final Logger log = LoggerFactory.getLogger(DbUtils.class);

    public static  void syncBehindTransaction(Consumer consumer, T data){
        TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
            @Override
            public void afterCommit() {
                try{
                    consumer.accept(data);
                }
                catch (Exception e){
                    log.error("执行失败");
                }
            }
        });
    }

    /**
     * 批量插入
     *
     * @param consumer 批量插入方法
     * @param collection 批量插入的对象
     * @param  数据类
     * @param  Collection
     */
    public static > void batchInsert(Consumer consumer, C collection) {
        if(CollectionUtils.isEmpty(collection)){
            return;
        }

        consumer.accept(collection);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy