mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
add Db.txWithResult method; close #I7DJBD
This commit is contained in:
parent
ee0e3f7465
commit
ddb239176a
@ -41,7 +41,8 @@ import java.util.function.Supplier;
|
||||
*/
|
||||
public class Db {
|
||||
|
||||
private Db() {}
|
||||
private Db() {
|
||||
}
|
||||
|
||||
private static final Map<String, RowMapperInvoker> INVOKER_MAP = new ConcurrentHashMap<>();
|
||||
static RowMapperInvoker defaultRowMapperInvoker;
|
||||
@ -73,6 +74,7 @@ public class Db {
|
||||
public static int insert(String schema, String tableName, Row row) {
|
||||
return invoker().insert(schema, tableName, row);
|
||||
}
|
||||
|
||||
/**
|
||||
* 往 tableName 插入一条 row 数据
|
||||
*
|
||||
@ -131,6 +133,7 @@ public class Db {
|
||||
mapper.insert(schema, tableName, row);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量插入数据
|
||||
*
|
||||
@ -479,7 +482,6 @@ public class Db {
|
||||
/**
|
||||
* 通过 update schema.table set field = field + 1 where ... 的这种方向更新数据库某个字段内容
|
||||
*
|
||||
*
|
||||
* @param schema 模式
|
||||
* @param tableName 表名
|
||||
* @param fieldName 字段名
|
||||
@ -587,7 +589,6 @@ public class Db {
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 根据 map 来查询 1 条数据
|
||||
*
|
||||
@ -734,6 +735,7 @@ public class Db {
|
||||
public static List<Row> selectListByCondition(String tableName, QueryCondition condition) {
|
||||
return invoker().selectListByQuery(null, tableName, new QueryWrapper().where(condition));
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 condition 条件来查询数据列表
|
||||
*
|
||||
@ -859,7 +861,6 @@ public class Db {
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 查询某列内容,数据返回应该有 多行 1 列
|
||||
*
|
||||
@ -1111,17 +1112,31 @@ public class Db {
|
||||
|
||||
|
||||
/**
|
||||
* 进行事务操作
|
||||
*
|
||||
* @param supplier
|
||||
* 进行事务操作,返回 null 或者 false 或者 抛出异常,事务回滚
|
||||
*/
|
||||
public static boolean tx(Supplier<Boolean> supplier) {
|
||||
return tx(supplier, Propagation.REQUIRED);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 进行事务操作,返回 null 或者 false 或者 抛出异常, 事务回滚
|
||||
*/
|
||||
public static boolean tx(Supplier<Boolean> supplier, Propagation propagation) {
|
||||
Boolean result = TransactionalManager.exec(supplier, propagation);
|
||||
Boolean result = TransactionalManager.exec(supplier, propagation, false);
|
||||
return result != null && result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 进行事务操作,和返回结果无关,只有抛出异常时,事务回滚
|
||||
*/
|
||||
public static <T> T txWithResult(Supplier<T> supplier) {
|
||||
return txWithResult(supplier, Propagation.REQUIRED);
|
||||
}
|
||||
|
||||
/**
|
||||
* 进行事务操作,和返回结果无关,只有抛出异常时,事务回滚
|
||||
*/
|
||||
public static <T> T txWithResult(Supplier<T> supplier, Propagation propagation) {
|
||||
return TransactionalManager.exec(supplier, propagation, true);
|
||||
}
|
||||
}
|
||||
|
||||
@ -30,7 +30,8 @@ import java.util.function.Supplier;
|
||||
*/
|
||||
public class TransactionalManager {
|
||||
|
||||
private TransactionalManager() {}
|
||||
private TransactionalManager() {
|
||||
}
|
||||
|
||||
private static final Log log = LogFactory.getLog(TransactionalManager.class);
|
||||
|
||||
@ -55,7 +56,7 @@ public class TransactionalManager {
|
||||
}
|
||||
|
||||
|
||||
public static Boolean exec(Supplier<Boolean> supplier, Propagation propagation) {
|
||||
public static <T> T exec(Supplier<T> supplier, Propagation propagation, boolean withResult) {
|
||||
//上一级事务的id,支持事务嵌套
|
||||
String currentXID = TransactionContext.getXID();
|
||||
try {
|
||||
@ -65,7 +66,7 @@ public class TransactionalManager {
|
||||
if (currentXID != null) {
|
||||
return supplier.get();
|
||||
} else {
|
||||
return execNewTransactional(supplier);
|
||||
return execNewTransactional(supplier, withResult);
|
||||
}
|
||||
|
||||
|
||||
@ -85,7 +86,7 @@ public class TransactionalManager {
|
||||
|
||||
//始终以新事务的方式运行,若存在当前事务,则暂停(挂起)当前事务。
|
||||
case REQUIRES_NEW:
|
||||
return execNewTransactional(supplier);
|
||||
return execNewTransactional(supplier, withResult);
|
||||
|
||||
|
||||
//以非事务的方式运行,若存在当前事务,则暂停(挂起)当前事务。
|
||||
@ -118,24 +119,32 @@ public class TransactionalManager {
|
||||
}
|
||||
}
|
||||
|
||||
private static Boolean execNewTransactional(Supplier<Boolean> supplier) {
|
||||
private static <T> T execNewTransactional(Supplier<T> supplier, boolean withResult) {
|
||||
String xid = startTransactional();
|
||||
Boolean success = false;
|
||||
boolean rollbacked = false;
|
||||
T result = null;
|
||||
boolean isRollback = false;
|
||||
try {
|
||||
success = supplier.get();
|
||||
result = supplier.get();
|
||||
} catch (Throwable e) {
|
||||
rollbacked = true;
|
||||
isRollback = true;
|
||||
rollback(xid);
|
||||
throw new TransactionException(e.getMessage(), e);
|
||||
} finally {
|
||||
if (success != null && success) {
|
||||
if (!isRollback) {
|
||||
if (!withResult) {
|
||||
if (result instanceof Boolean && (Boolean) result) {
|
||||
commit(xid);
|
||||
} else if (!rollbacked) {
|
||||
}
|
||||
//null or false
|
||||
else {
|
||||
rollback(xid);
|
||||
}
|
||||
} else {
|
||||
commit(xid);
|
||||
}
|
||||
return success;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -23,6 +23,7 @@ import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author 王帅
|
||||
@ -32,9 +33,12 @@ class ReflectTest {
|
||||
|
||||
@Test
|
||||
void test() {
|
||||
Field field = ClassUtil.getAllFields(Account.class, f -> f.getName().equals("list")).get(0);
|
||||
List<Field> allFields = ClassUtil.getAllFields(Account.class);
|
||||
for (Field field : allFields) {
|
||||
Type type = TypeParameterResolver.resolveFieldType(field, Account.class);
|
||||
System.out.println(type);
|
||||
System.out.println("field: " + field+"----->Type:" + type);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user