diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/Db.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/Db.java index 1c04c5cf..4d3e8df7 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/Db.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/Db.java @@ -41,7 +41,8 @@ import java.util.function.Supplier; */ public class Db { - private Db() {} + private Db() { + } private static final Map INVOKER_MAP = new ConcurrentHashMap<>(); static RowMapperInvoker defaultRowMapperInvoker; @@ -70,9 +71,10 @@ public class Db { * @param tableName 表名 * @param row 数据 */ - public static int insert(String schema,String tableName, Row row) { - return invoker().insert(schema,tableName, row); + public static int insert(String schema, String tableName, Row row) { + return invoker().insert(schema, tableName, row); } + /** * 往 tableName 插入一条 row 数据 * @@ -80,7 +82,7 @@ public class Db { * @param row 数据 */ public static int insert(String tableName, Row row) { - return invoker().insert(null,tableName, row); + return invoker().insert(null, tableName, row); } @@ -102,8 +104,8 @@ public class Db { * @param tableName 表名 * @param rows 数据 */ - public static int[] insertBatch(String schema,String tableName, Collection rows) { - return insertBatch(schema,tableName, rows, rows.size()); + public static int[] insertBatch(String schema, String tableName, Collection rows) { + return insertBatch(schema, tableName, rows, rows.size()); } /** @@ -113,7 +115,7 @@ public class Db { * @param rows 数据 */ public static int[] insertBatch(String tableName, Collection rows) { - return insertBatch(null,tableName, rows, rows.size()); + return insertBatch(null, tableName, rows, rows.size()); } /** @@ -124,13 +126,14 @@ public class Db { * @param rows 数据 * @param batchSize 每次提交的数据量 */ - public static int[] insertBatch(String schema,String tableName, Collection rows, int batchSize) { + public static int[] insertBatch(String schema, String tableName, Collection rows, int batchSize) { List list = CollectionUtil.toList(rows); return executeBatch(rows.size(), batchSize, RowMapper.class, (mapper, index) -> { Row row = list.get(index); - mapper.insert(schema,tableName, row); + mapper.insert(schema, tableName, row); }); } + /** * 批量插入数据 * @@ -142,29 +145,29 @@ public class Db { List list = CollectionUtil.toList(rows); return executeBatch(rows.size(), batchSize, RowMapper.class, (mapper, index) -> { Row row = list.get(index); - mapper.insert(null,tableName, row); + mapper.insert(null, tableName, row); }); } /** - * 批量插入数据,根据第一条内容来构建插入的字段,效率比 {@link #insertBatch(String ,String, Collection, int)} 高 + * 批量插入数据,根据第一条内容来构建插入的字段,效率比 {@link #insertBatch(String, String, Collection, int)} 高 * * @param schema 模式 * @param tableName 表名 * @param rows 数据 */ - public static int insertBatchWithFirstRowColumns(String schema,String tableName, List rows) { - return invoker().insertBatchWithFirstRowColumns(schema,tableName, rows); + public static int insertBatchWithFirstRowColumns(String schema, String tableName, List rows) { + return invoker().insertBatchWithFirstRowColumns(schema, tableName, rows); } /** - * 批量插入数据,根据第一条内容来构建插入的字段,效率比 {@link #insertBatch(String ,String, Collection, int)} 高 + * 批量插入数据,根据第一条内容来构建插入的字段,效率比 {@link #insertBatch(String, String, Collection, int)} 高 * * @param tableName 表名 * @param rows 数据 */ public static int insertBatchWithFirstRowColumns(String tableName, List rows) { - return invoker().insertBatchWithFirstRowColumns(null,tableName, rows); + return invoker().insertBatchWithFirstRowColumns(null, tableName, rows); } /** @@ -184,8 +187,8 @@ public class Db { * @param tableName 表名 * @param row 主键 和 id值 */ - public static int deleteById(String schema,String tableName, Row row) { - return invoker().deleteById(schema,tableName, row); + public static int deleteById(String schema, String tableName, Row row) { + return invoker().deleteById(schema, tableName, row); } /** @@ -195,20 +198,20 @@ public class Db { * @param row 主键 和 id值 */ public static int deleteById(String tableName, Row row) { - return invoker().deleteById(null,tableName, row); + return invoker().deleteById(null, tableName, row); } /** * 根据主键来删除 1 条数据 * - * @param schema 模式 + * @param schema 模式 * @param tableName 表名 * @param primaryKey 主键字段名称 * @param id 主键值 */ - public static int deleteById(String schema,String tableName, String primaryKey, Object id) { - return invoker().deleteById(schema,tableName, primaryKey, id); + public static int deleteById(String schema, String tableName, String primaryKey, Object id) { + return invoker().deleteById(schema, tableName, primaryKey, id); } /** @@ -219,20 +222,20 @@ public class Db { * @param id 主键值 */ public static int deleteById(String tableName, String primaryKey, Object id) { - return invoker().deleteById(null,tableName, primaryKey, id); + return invoker().deleteById(null, tableName, primaryKey, id); } /** * 根据 id 集合来批量删除数据 * - * @param schema 模式 + * @param schema 模式 * @param tableName 表名 * @param primaryKey 主键字段名称 * @param ids id 集合 */ - public static int deleteBatchByIds(String schema,String tableName, String primaryKey, Collection ids) { - return invoker().deleteBatchByIds(schema,tableName, primaryKey, ids); + public static int deleteBatchByIds(String schema, String tableName, String primaryKey, Collection ids) { + return invoker().deleteBatchByIds(schema, tableName, primaryKey, ids); } /** @@ -243,18 +246,18 @@ public class Db { * @param ids id 集合 */ public static int deleteBatchByIds(String tableName, String primaryKey, Collection ids) { - return invoker().deleteBatchByIds(null,tableName, primaryKey, ids); + return invoker().deleteBatchByIds(null, tableName, primaryKey, ids); } /** * 根据 map 构建的 where 条件来删除数据 * - * @param schema 模式 + * @param schema 模式 * @param tableName 表名 * @param whereColumns where 条件 */ - public static int deleteByMap(String schema,String tableName, Map whereColumns) { - return invoker().deleteByQuery(schema,tableName, new QueryWrapper().where(whereColumns)); + public static int deleteByMap(String schema, String tableName, Map whereColumns) { + return invoker().deleteByQuery(schema, tableName, new QueryWrapper().where(whereColumns)); } /** @@ -264,7 +267,7 @@ public class Db { * @param whereColumns where 条件 */ public static int deleteByMap(String tableName, Map whereColumns) { - return invoker().deleteByQuery(null,tableName, new QueryWrapper().where(whereColumns)); + return invoker().deleteByQuery(null, tableName, new QueryWrapper().where(whereColumns)); } /** @@ -274,8 +277,8 @@ public class Db { * @param tableName 表名 * @param condition 条件内容 */ - public static int deleteByCondition(String schema,String tableName, QueryCondition condition) { - return invoker().deleteByQuery(schema,tableName, new QueryWrapper().where(condition)); + public static int deleteByCondition(String schema, String tableName, QueryCondition condition) { + return invoker().deleteByQuery(schema, tableName, new QueryWrapper().where(condition)); } /** @@ -285,19 +288,19 @@ public class Db { * @param condition 条件内容 */ public static int deleteByCondition(String tableName, QueryCondition condition) { - return invoker().deleteByQuery(null,tableName, new QueryWrapper().where(condition)); + return invoker().deleteByQuery(null, tableName, new QueryWrapper().where(condition)); } /** * 根据 query 构建的条件来删除数据 * - * @param schema 模式 + * @param schema 模式 * @param tableName 表名 * @param queryWrapper query */ - public static int deleteByQuery(String schema,String tableName, QueryWrapper queryWrapper) { - return invoker().deleteByQuery(schema,tableName, queryWrapper); + public static int deleteByQuery(String schema, String tableName, QueryWrapper queryWrapper) { + return invoker().deleteByQuery(schema, tableName, queryWrapper); } /** @@ -307,7 +310,7 @@ public class Db { * @param queryWrapper query */ public static int deleteByQuery(String tableName, QueryWrapper queryWrapper) { - return invoker().deleteByQuery(null,tableName, queryWrapper); + return invoker().deleteByQuery(null, tableName, queryWrapper); } /** @@ -340,8 +343,8 @@ public class Db { * @param tableName 表名 * @param row id 及其内容 */ - public static int updateById(String schema,String tableName, Row row) { - return invoker().updateById(schema,tableName, row); + public static int updateById(String schema, String tableName, Row row) { + return invoker().updateById(schema, tableName, row); } @@ -352,19 +355,19 @@ public class Db { * @param row id 及其内容 */ public static int updateById(String tableName, Row row) { - return invoker().updateById(null,tableName, row); + return invoker().updateById(null, tableName, row); } /** * 根据 map 构建的条件来更新数据 * - * @param schema 模式 + * @param schema 模式 * @param tableName 表名 * @param data 数据内容 * @param whereColumns where 条件 */ - public static int updateByMap(String schema,String tableName, Row data, Map whereColumns) { - return invoker().updateByQuery(schema,tableName, data, new QueryWrapper().where(whereColumns)); + public static int updateByMap(String schema, String tableName, Row data, Map whereColumns) { + return invoker().updateByQuery(schema, tableName, data, new QueryWrapper().where(whereColumns)); } @@ -376,7 +379,7 @@ public class Db { * @param whereColumns where 条件 */ public static int updateByMap(String tableName, Row data, Map whereColumns) { - return invoker().updateByQuery(null,tableName, data, new QueryWrapper().where(whereColumns)); + return invoker().updateByQuery(null, tableName, data, new QueryWrapper().where(whereColumns)); } /** @@ -387,8 +390,8 @@ public class Db { * @param data 数据 * @param condition 条件 */ - public static int updateByCondition(String schema,String tableName, Row data, QueryCondition condition) { - return invoker().updateByQuery(schema,tableName, data, new QueryWrapper().where(condition)); + public static int updateByCondition(String schema, String tableName, Row data, QueryCondition condition) { + return invoker().updateByQuery(schema, tableName, data, new QueryWrapper().where(condition)); } /** @@ -399,20 +402,20 @@ public class Db { * @param condition 条件 */ public static int updateByCondition(String tableName, Row data, QueryCondition condition) { - return invoker().updateByQuery(null,tableName, data, new QueryWrapper().where(condition)); + return invoker().updateByQuery(null, tableName, data, new QueryWrapper().where(condition)); } /** * 根据 query 构建的条件来更新数据 * - * @param schema 模式 + * @param schema 模式 * @param tableName 表名 * @param data 数据内容 * @param queryWrapper queryWrapper 条件 */ - public static int updateByQuery(String schema,String tableName, Row data, QueryWrapper queryWrapper) { - return invoker().updateByQuery(schema,tableName, data, queryWrapper); + public static int updateByQuery(String schema, String tableName, Row data, QueryWrapper queryWrapper) { + return invoker().updateByQuery(schema, tableName, data, queryWrapper); } /** @@ -423,7 +426,7 @@ public class Db { * @param queryWrapper queryWrapper 条件 */ public static int updateByQuery(String tableName, Row data, QueryWrapper queryWrapper) { - return invoker().updateByQuery(null,tableName, data, queryWrapper); + return invoker().updateByQuery(null, tableName, data, queryWrapper); } @@ -434,8 +437,8 @@ public class Db { * @param tableName 表名 * @param rows 还有主键的数据 */ - public static int updateBatchById(String schema,String tableName, List rows) { - return invoker().updateBatchById(schema,tableName, rows); + public static int updateBatchById(String schema, String tableName, List rows) { + return invoker().updateBatchById(schema, tableName, rows); } /** @@ -445,7 +448,7 @@ public class Db { * @param rows 还有主键的数据 */ public static int updateBatchById(String tableName, List rows) { - return invoker().updateBatchById(null,tableName, rows); + return invoker().updateBatchById(null, tableName, rows); } @@ -479,16 +482,15 @@ public class Db { /** * 通过 update schema.table set field = field + 1 where ... 的这种方向更新数据库某个字段内容 * - * - * @param schema 模式 - * @param tableName 表名 - * @param fieldName 字段名 - * @param value 递增值 - * @param queryWrapper 条件 - * @return 受影响行数 + * @param schema 模式 + * @param tableName 表名 + * @param fieldName 字段名 + * @param value 递增值 + * @param queryWrapper 条件 + * @return 受影响行数 */ - public static int updateNumberAddByQuery(String schema,String tableName, String fieldName, Number value, QueryWrapper queryWrapper){ - return invoker().updateNumberAddByQuery(schema,tableName, fieldName, value, queryWrapper); + public static int updateNumberAddByQuery(String schema, String tableName, String fieldName, Number value, QueryWrapper queryWrapper) { + return invoker().updateNumberAddByQuery(schema, tableName, fieldName, value, queryWrapper); } /** @@ -500,8 +502,8 @@ public class Db { * @param queryWrapper * @return */ - public static int updateNumberAddByQuery(String tableName, String fieldName, Number value, QueryWrapper queryWrapper){ - return invoker().updateNumberAddByQuery(null,tableName, fieldName, value, queryWrapper); + public static int updateNumberAddByQuery(String tableName, String fieldName, Number value, QueryWrapper queryWrapper) { + return invoker().updateNumberAddByQuery(null, tableName, fieldName, value, queryWrapper); } @@ -537,8 +539,8 @@ public class Db { * @param tableName 表名 * @param row 主键和 id 值 */ - public static Row selectOneById(String schema,String tableName, Row row) { - return invoker().selectOneById(schema,tableName, row); + public static Row selectOneById(String schema, String tableName, Row row) { + return invoker().selectOneById(schema, tableName, row); } /** @@ -548,20 +550,20 @@ public class Db { * @param row 主键和 id 值 */ public static Row selectOneById(String tableName, Row row) { - return invoker().selectOneById(null,tableName, row); + return invoker().selectOneById(null, tableName, row); } /** * 根据主键来查询 1 条数据 * - * @param schema 模式 + * @param schema 模式 * @param tableName 表名 * @param primaryKey 主键字段名称 * @param id 主键值 */ - public static Row selectOneById(String schema,String tableName, String primaryKey, Object id) { - return invoker().selectOneById(schema,tableName, primaryKey, id); + public static Row selectOneById(String schema, String tableName, String primaryKey, Object id) { + return invoker().selectOneById(schema, tableName, primaryKey, id); } /** @@ -572,22 +574,21 @@ public class Db { * @param id 主键值 */ public static Row selectOneById(String tableName, String primaryKey, Object id) { - return invoker().selectOneById(null,tableName, primaryKey, id); + return invoker().selectOneById(null, tableName, primaryKey, id); } /** * 根据 map 来查询 1 条数据 * - * @param schema 模式 + * @param schema 模式 * @param tableName 表名 * @param whereColumns where条件 */ - public static Row selectOneByMap(String schema,String tableName, Map whereColumns) { - return invoker().selectOneByQuery(schema,tableName, new QueryWrapper().where(whereColumns)); + public static Row selectOneByMap(String schema, String tableName, Map whereColumns) { + return invoker().selectOneByQuery(schema, tableName, new QueryWrapper().where(whereColumns)); } - /** * 根据 map 来查询 1 条数据 * @@ -595,7 +596,7 @@ public class Db { * @param whereColumns where条件 */ public static Row selectOneByMap(String tableName, Map whereColumns) { - return invoker().selectOneByQuery(null,tableName, new QueryWrapper().where(whereColumns)); + return invoker().selectOneByQuery(null, tableName, new QueryWrapper().where(whereColumns)); } /** @@ -605,8 +606,8 @@ public class Db { * @param tableName 表名 * @param condition 条件 */ - public static Row selectOneByCondition(String schema,String tableName, QueryCondition condition) { - return invoker().selectOneByQuery(schema,tableName, new QueryWrapper().where(condition)); + public static Row selectOneByCondition(String schema, String tableName, QueryCondition condition) { + return invoker().selectOneByQuery(schema, tableName, new QueryWrapper().where(condition)); } /** @@ -616,19 +617,19 @@ public class Db { * @param condition 条件 */ public static Row selectOneByCondition(String tableName, QueryCondition condition) { - return invoker().selectOneByQuery(null,tableName, new QueryWrapper().where(condition)); + return invoker().selectOneByQuery(null, tableName, new QueryWrapper().where(condition)); } /** * 根据 queryWrapper 来查询 1 条数据 * - * @param schema 模式 + * @param schema 模式 * @param tableName 表名 * @param queryWrapper queryWrapper */ - public static Row selectOneByQuery(String schema,String tableName, QueryWrapper queryWrapper) { - return invoker().selectOneByQuery(schema,tableName, queryWrapper); + public static Row selectOneByQuery(String schema, String tableName, QueryWrapper queryWrapper) { + return invoker().selectOneByQuery(schema, tableName, queryWrapper); } /** @@ -638,7 +639,7 @@ public class Db { * @param queryWrapper queryWrapper */ public static Row selectOneByQuery(String tableName, QueryWrapper queryWrapper) { - return invoker().selectOneByQuery(null,tableName, queryWrapper); + return invoker().selectOneByQuery(null, tableName, queryWrapper); } @@ -652,7 +653,7 @@ public class Db { if (queryTables == null || queryTables.isEmpty()) { throw FlexExceptions.wrap("table must not be null or empty in Db.selectOneByQuery"); } - return invoker().selectOneByQuery(null,null, queryWrapper); + return invoker().selectOneByQuery(null, null, queryWrapper); } @@ -674,8 +675,8 @@ public class Db { * @param tableName 表名 * @param whereColumns where 条件 */ - public static List selectListByMap(String schema,String tableName, Map whereColumns) { - return invoker().selectListByQuery(schema,tableName, new QueryWrapper().where(whereColumns)); + public static List selectListByMap(String schema, String tableName, Map whereColumns) { + return invoker().selectListByQuery(schema, tableName, new QueryWrapper().where(whereColumns)); } /** @@ -685,20 +686,20 @@ public class Db { * @param whereColumns where 条件 */ public static List selectListByMap(String tableName, Map whereColumns) { - return invoker().selectListByQuery(null,tableName, new QueryWrapper().where(whereColumns)); + return invoker().selectListByQuery(null, tableName, new QueryWrapper().where(whereColumns)); } /** * 根据 map 构建的条件来查询数据列表 * - * @param schema 模式 + * @param schema 模式 * @param tableName 表名 * @param whereColumns 条件 * @param count 数据量 */ - public static List selectListByMap(String schema,String tableName, Map whereColumns, int count) { - return invoker().selectListByQuery(schema,tableName, new QueryWrapper().where(whereColumns).limit(count)); + public static List selectListByMap(String schema, String tableName, Map whereColumns, int count) { + return invoker().selectListByQuery(schema, tableName, new QueryWrapper().where(whereColumns).limit(count)); } /** @@ -709,7 +710,7 @@ public class Db { * @param count 数据量 */ public static List selectListByMap(String tableName, Map whereColumns, int count) { - return invoker().selectListByQuery(null,tableName, new QueryWrapper().where(whereColumns).limit(count)); + return invoker().selectListByQuery(null, tableName, new QueryWrapper().where(whereColumns).limit(count)); } @@ -720,8 +721,8 @@ public class Db { * @param tableName 表名 * @param condition where 条件 */ - public static List selectListByCondition(String schema,String tableName, QueryCondition condition) { - return invoker().selectListByQuery(schema,tableName, new QueryWrapper().where(condition)); + public static List selectListByCondition(String schema, String tableName, QueryCondition condition) { + return invoker().selectListByQuery(schema, tableName, new QueryWrapper().where(condition)); } @@ -732,8 +733,9 @@ public class Db { * @param condition where 条件 */ public static List selectListByCondition(String tableName, QueryCondition condition) { - return invoker().selectListByQuery(null,tableName, new QueryWrapper().where(condition)); + return invoker().selectListByQuery(null, tableName, new QueryWrapper().where(condition)); } + /** * 根据 condition 条件来查询数据列表 * @@ -742,8 +744,8 @@ public class Db { * @param condition 条件 * @param count 数据量 */ - public static List selectListByCondition(String schema,String tableName, QueryCondition condition, int count) { - return invoker().selectListByQuery(schema,tableName, new QueryWrapper().where(condition).limit(count)); + public static List selectListByCondition(String schema, String tableName, QueryCondition condition, int count) { + return invoker().selectListByQuery(schema, tableName, new QueryWrapper().where(condition).limit(count)); } /** @@ -754,19 +756,19 @@ public class Db { * @param count 数据量 */ public static List selectListByCondition(String tableName, QueryCondition condition, int count) { - return invoker().selectListByQuery(null,tableName, new QueryWrapper().where(condition).limit(count)); + return invoker().selectListByQuery(null, tableName, new QueryWrapper().where(condition).limit(count)); } /** * 通过 query 来查询数据列表 * - * @param schema 模式 + * @param schema 模式 * @param tableName 表名 * @param queryWrapper query 条件 */ - public static List selectListByQuery(String schema,String tableName, QueryWrapper queryWrapper) { - return invoker().selectListByQuery(schema,tableName, queryWrapper); + public static List selectListByQuery(String schema, String tableName, QueryWrapper queryWrapper) { + return invoker().selectListByQuery(schema, tableName, queryWrapper); } @@ -777,7 +779,7 @@ public class Db { * @param queryWrapper query 条件 */ public static List selectListByQuery(String tableName, QueryWrapper queryWrapper) { - return invoker().selectListByQuery(null,tableName, queryWrapper); + return invoker().selectListByQuery(null, tableName, queryWrapper); } @@ -791,7 +793,7 @@ public class Db { if (queryTables == null || queryTables.isEmpty()) { throw FlexExceptions.wrap("table must not be null or empty in Db.selectListByQuery"); } - return invoker().selectListByQuery(null,null, queryWrapper); + return invoker().selectListByQuery(null, null, queryWrapper); } /** @@ -800,8 +802,8 @@ public class Db { * @param schema 模式 * @param tableName 表名 */ - public static List selectAll(String schema,String tableName) { - return invoker().selectAll(schema,tableName); + public static List selectAll(String schema, String tableName) { + return invoker().selectAll(schema, tableName); } /** @@ -810,7 +812,7 @@ public class Db { * @param tableName 表名 */ public static List selectAll(String tableName) { - return invoker().selectAll(null,tableName); + return invoker().selectAll(null, tableName); } /** @@ -827,13 +829,13 @@ public class Db { /** * 根据 queryWrapper 查询内容,数据返回的应该只有 1 行 1 列 * - * @param schema 模式 + * @param schema 模式 * @param tableName 表名 * @param queryWrapper query 封装 * @return 数据内容 */ - public static Object selectObject(String schema,String tableName, QueryWrapper queryWrapper) { - return invoker().selectObjectByQuery(schema,tableName, queryWrapper); + public static Object selectObject(String schema, String tableName, QueryWrapper queryWrapper) { + return invoker().selectObjectByQuery(schema, tableName, queryWrapper); } /** @@ -844,7 +846,7 @@ public class Db { * @return 数据内容 */ public static Object selectObject(String tableName, QueryWrapper queryWrapper) { - return invoker().selectObjectByQuery(null,tableName, queryWrapper); + return invoker().selectObjectByQuery(null, tableName, queryWrapper); } @@ -859,7 +861,6 @@ public class Db { } - /** * 查询某列内容,数据返回应该有 多行 1 列 * @@ -874,13 +875,13 @@ public class Db { /** * 根据 queryWrapper 查询内容,数据返回的应该只有 1 行 1 列 * - * @param schema 模式 + * @param schema 模式 * @param tableName 表名 * @param queryWrapper query 封装 * @return 数据内容 */ - public static Object selectObjectList(String schema,String tableName, QueryWrapper queryWrapper) { - return invoker().selectObjectListByQuery(schema,tableName, queryWrapper); + public static Object selectObjectList(String schema, String tableName, QueryWrapper queryWrapper) { + return invoker().selectObjectListByQuery(schema, tableName, queryWrapper); } /** @@ -891,7 +892,7 @@ public class Db { * @return 数据内容 */ public static Object selectObjectList(String tableName, QueryWrapper queryWrapper) { - return invoker().selectObjectListByQuery(null,tableName, queryWrapper); + return invoker().selectObjectListByQuery(null, tableName, queryWrapper); } @@ -925,8 +926,8 @@ public class Db { * @param tableName 表名 * @param condition 条件 */ - public static long selectCountByCondition(String schema,String tableName, QueryCondition condition) { - return invoker().selectCountByQuery(schema,tableName, new QueryWrapper().where(condition)); + public static long selectCountByCondition(String schema, String tableName, QueryCondition condition) { + return invoker().selectCountByQuery(schema, tableName, new QueryWrapper().where(condition)); } /** @@ -936,19 +937,19 @@ public class Db { * @param condition 条件 */ public static long selectCountByCondition(String tableName, QueryCondition condition) { - return invoker().selectCountByQuery(null,tableName, new QueryWrapper().where(condition)); + return invoker().selectCountByQuery(null, tableName, new QueryWrapper().where(condition)); } /** * 根据 query 构建的条件来查询数据量 * - * @param schema 模式 + * @param schema 模式 * @param tableName 表名 * @param queryWrapper query 条件 */ - public static long selectCountByQuery(String schema,String tableName, QueryWrapper queryWrapper) { - return invoker().selectCountByQuery(schema,tableName, queryWrapper); + public static long selectCountByQuery(String schema, String tableName, QueryWrapper queryWrapper) { + return invoker().selectCountByQuery(schema, tableName, queryWrapper); } /** @@ -958,7 +959,7 @@ public class Db { * @param queryWrapper query 条件 */ public static long selectCountByQuery(String tableName, QueryWrapper queryWrapper) { - return invoker().selectCountByQuery(null,tableName, queryWrapper); + return invoker().selectCountByQuery(null, tableName, queryWrapper); } @@ -973,21 +974,21 @@ public class Db { if (queryTables == null || queryTables.isEmpty()) { throw FlexExceptions.wrap("Query tables must not be null or empty in Db.selectCountByQuery"); } - return invoker().selectCountByQuery(null,null, queryWrapper); + return invoker().selectCountByQuery(null, null, queryWrapper); } /** * 分页查询 * - * @param schema 模式 + * @param schema 模式 * @param tableName 表名 * @param pageNumber 当前的页码 * @param pageSize 每页的数据量 * @param condition 条件 */ - public static Page paginate(String schema,String tableName, int pageNumber, int pageSize, QueryCondition condition) { - return invoker().paginate(schema,tableName, new Page<>(pageNumber, pageSize), QueryWrapper.create().where(condition)); + public static Page paginate(String schema, String tableName, int pageNumber, int pageSize, QueryCondition condition) { + return invoker().paginate(schema, tableName, new Page<>(pageNumber, pageSize), QueryWrapper.create().where(condition)); } @@ -1000,22 +1001,22 @@ public class Db { * @param condition 条件 */ public static Page paginate(String tableName, int pageNumber, int pageSize, QueryCondition condition) { - return invoker().paginate(null,tableName, new Page<>(pageNumber, pageSize), QueryWrapper.create().where(condition)); + return invoker().paginate(null, tableName, new Page<>(pageNumber, pageSize), QueryWrapper.create().where(condition)); } /** * 分页查询 * - * @param schema 模式 + * @param schema 模式 * @param tableName 表名 * @param pageNumber 当前的页码 * @param pageSize 每页的数据量 * @param totalRow 数据总量 * @param condition 条件 */ - public static Page paginate(String schema,String tableName, int pageNumber, int pageSize, int totalRow, QueryCondition condition) { - return invoker().paginate(schema,tableName, new Page<>(pageNumber, pageSize, totalRow), QueryWrapper.create().where(condition)); + public static Page paginate(String schema, String tableName, int pageNumber, int pageSize, int totalRow, QueryCondition condition) { + return invoker().paginate(schema, tableName, new Page<>(pageNumber, pageSize, totalRow), QueryWrapper.create().where(condition)); } /** @@ -1028,21 +1029,21 @@ public class Db { * @param condition 条件 */ public static Page paginate(String tableName, int pageNumber, int pageSize, int totalRow, QueryCondition condition) { - return invoker().paginate(null,tableName, new Page<>(pageNumber, pageSize, totalRow), QueryWrapper.create().where(condition)); + return invoker().paginate(null, tableName, new Page<>(pageNumber, pageSize, totalRow), QueryWrapper.create().where(condition)); } /** * 分页查询 * - * @param schema 模式 + * @param schema 模式 * @param tableName 表名 * @param pageNumber 当前的页码 * @param pageSize 每页的数据量 * @param queryWrapper 条件 */ - public static Page paginate(String schema,String tableName, int pageNumber, int pageSize, QueryWrapper queryWrapper) { - return invoker().paginate(schema,tableName, new Page<>(pageNumber, pageSize), queryWrapper); + public static Page paginate(String schema, String tableName, int pageNumber, int pageSize, QueryWrapper queryWrapper) { + return invoker().paginate(schema, tableName, new Page<>(pageNumber, pageSize), queryWrapper); } /** @@ -1054,22 +1055,22 @@ public class Db { * @param queryWrapper 条件 */ public static Page paginate(String tableName, int pageNumber, int pageSize, QueryWrapper queryWrapper) { - return invoker().paginate(null,tableName, new Page<>(pageNumber, pageSize), queryWrapper); + return invoker().paginate(null, tableName, new Page<>(pageNumber, pageSize), queryWrapper); } /** * 分页查询 * - * @param schema 模式 + * @param schema 模式 * @param tableName 表名 * @param pageNumber 当前的页码 * @param pageSize 每页的数据量 * @param totalRow 数据总量 * @param queryWrapper 条件 */ - public static Page paginate(String schema,String tableName, int pageNumber, int pageSize, int totalRow, QueryWrapper queryWrapper) { - return invoker().paginate(schema,tableName, new Page<>(pageNumber, pageSize, totalRow), queryWrapper); + public static Page paginate(String schema, String tableName, int pageNumber, int pageSize, int totalRow, QueryWrapper queryWrapper) { + return invoker().paginate(schema, tableName, new Page<>(pageNumber, pageSize, totalRow), queryWrapper); } /** @@ -1082,20 +1083,20 @@ public class Db { * @param queryWrapper 条件 */ public static Page paginate(String tableName, int pageNumber, int pageSize, int totalRow, QueryWrapper queryWrapper) { - return invoker().paginate(null,tableName, new Page<>(pageNumber, pageSize, totalRow), queryWrapper); + return invoker().paginate(null, tableName, new Page<>(pageNumber, pageSize, totalRow), queryWrapper); } /** * 分页查询 * - * @param schema 模式 + * @param schema 模式 * @param tableName 表名 * @param page page 对象,若 page 有 totalCount 值,则不会再去查询分类的数据总量 * @param queryWrapper 条件 */ - public static Page paginate(String schema,String tableName, Page page, QueryWrapper queryWrapper) { - return invoker().paginate(schema,tableName, page, queryWrapper); + public static Page paginate(String schema, String tableName, Page page, QueryWrapper queryWrapper) { + return invoker().paginate(schema, tableName, page, queryWrapper); } /** @@ -1106,22 +1107,36 @@ public class Db { * @param queryWrapper 条件 */ public static Page paginate(String tableName, Page page, QueryWrapper queryWrapper) { - return invoker().paginate(null,tableName, page, queryWrapper); + return invoker().paginate(null, tableName, page, queryWrapper); } /** - * 进行事务操作 - * - * @param supplier + * 进行事务操作,返回 null 或者 false 或者 抛出异常,事务回滚 */ public static boolean tx(Supplier supplier) { return tx(supplier, Propagation.REQUIRED); } - + /** + * 进行事务操作,返回 null 或者 false 或者 抛出异常, 事务回滚 + */ public static boolean tx(Supplier supplier, Propagation propagation) { - Boolean result = TransactionalManager.exec(supplier, propagation); + Boolean result = TransactionalManager.exec(supplier, propagation, false); return result != null && result; } + + /** + * 进行事务操作,和返回结果无关,只有抛出异常时,事务回滚 + */ + public static T txWithResult(Supplier supplier) { + return txWithResult(supplier, Propagation.REQUIRED); + } + + /** + * 进行事务操作,和返回结果无关,只有抛出异常时,事务回滚 + */ + public static T txWithResult(Supplier supplier, Propagation propagation) { + return TransactionalManager.exec(supplier, propagation, true); + } } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/transaction/TransactionalManager.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/transaction/TransactionalManager.java index f5cb0389..598ee768 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/transaction/TransactionalManager.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/transaction/TransactionalManager.java @@ -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 supplier, Propagation propagation) { + public static T exec(Supplier 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 supplier) { + private static T execNewTransactional(Supplier 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) { - commit(xid); - } else if (!rollbacked) { - rollback(xid); + if (!isRollback) { + if (!withResult) { + if (result instanceof Boolean && (Boolean) result) { + commit(xid); + } + //null or false + else { + rollback(xid); + } + } else { + commit(xid); + } } } - return success; + return result; } diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/common/ReflectTest.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/common/ReflectTest.java index e1d3bea1..b2f83cc5 100644 --- a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/common/ReflectTest.java +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/common/ReflectTest.java @@ -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); - Type type = TypeParameterResolver.resolveFieldType(field, Account.class); - System.out.println(type); + List allFields = ClassUtil.getAllFields(Account.class); + for (Field field : allFields) { + Type type = TypeParameterResolver.resolveFieldType(field, Account.class); + System.out.println("field: " + field+"----->Type:" + type); + } + } } \ No newline at end of file