Skip to content

Instantly share code, notes, and snippets.

@magicianlib
Last active July 24, 2024 09:54
Show Gist options
  • Save magicianlib/e763d9266818de3d4070a6a236e1d5d1 to your computer and use it in GitHub Desktop.
Save magicianlib/e763d9266818de3d4070a6a236e1d5d1 to your computer and use it in GitHub Desktop.
MybatisHelper(MyBatis Batch Operator Helper)
import java.util.function.Consumer;
import java.util.function.Function;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.support.TransactionOperations;
import org.springframework.transaction.support.TransactionTemplate;
/**
* MyBatis Batch Operator Helper
*
* @since Spring 5.2
*/
@Component
public class MybatisHelper {
@Autowired
// @Qualifier("....") // Optional
private SqlSessionFactory sqlSessionFactory;
public void batchOperation(Consumer<SqlSession> consumer) {
SqlSession session = sqlSessionFactory.openSession(
ExecutorType.BATCH,
false
);
try {
consumer.accept(session);
session.commit();
} catch (Exception e) {
session.rollback();
throw new RuntimeException(e);
} finally {
session.close();
}
}
public <R> R simpleTx(
TransactionTemplate template,
Function<SqlSession, R> function,
BoolFunction<R> commit
) {
return tx(template, ExecutorType.SIMPLE, function, commit);
}
public <R> R batchTx(
TransactionTemplate template,
Function<SqlSession, R> function,
BoolFunction<R> commit
) {
return tx(template, ExecutorType.BATCH, function, commit);
}
/**
* 事务操作
*
* @param template 事务模板
* @param executorType 指定执行类型
* @param function 处理业务数据
* @param commit 根据业务数据返回结果过判断是否提交事务,不提交则执行事务回滚
* @return 业务数据处理结果
* @throws RuntimeException 事务回滚时
* @see ExecutorType
*/
public <R> R tx(
TransactionTemplate template,
ExecutorType executorType,
Function<SqlSession, R> function,
BoolFunction<R> commit
) {
return template.execute(tx -> {
SqlSession session = sqlSessionFactory.openSession(executorType, false);
try {
R r = function.apply(session);
boolean ok = commit.apply(r);
if (ok) {
session.commit();
} else {
session.rollback();
}
return r;
} catch (Exception e) {
session.rollback();
throw new RuntimeException(e);
} finally {
session.close();
}
});
}
public void simpleTxWithoutResult(
TransactionTemplate template,
BoolFunction<SqlSession> function
) {
txWithoutResult(template, ExecutorType.SIMPLE, function);
}
public void batchTxWithoutResult(
TransactionTemplate template,
BoolFunction<SqlSession> function
) {
txWithoutResult(template, ExecutorType.BATCH, function);
}
/**
* 事务操作 - 无返回值
*
* @param template 事务模板
* @param function 处理业务数据
* @see TransactionOperations#executeWithoutResult(Consumer)
* @since Spring 5.2
*/
public void txWithoutResult(
TransactionTemplate template,
ExecutorType executorType,
BoolFunction<SqlSession> function
) {
template.executeWithoutResult(tx -> {
SqlSession session = sqlSessionFactory.openSession(
ExecutorType.BATCH,
false
);
try {
if (function.apply(session)) {
session.commit();
} else {
session.rollback();
}
} catch (Exception e) {
session.rollback();
throw new RuntimeException(e);
} finally {
session.close();
}
});
}
@FunctionalInterface
public interface BoolFunction<T> {
/**
* Applies this function to the given argument.
*
* @param value the function argument
* @return the function result
*/
boolean apply(T value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment