1 Star 0 Fork 474

duxlei / MyBatis-Flex

forked from MyBatis-Flex / MyBatis-Flex 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
Db.java 37.11 KB
一键复制 编辑 原始数据 按行查看 历史
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168
/*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mybatisflex.core.row;
import com.mybatisflex.core.FlexGlobalConfig;
import com.mybatisflex.core.exception.FlexExceptions;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.CPI;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryTable;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.transaction.Propagation;
import com.mybatisflex.core.transaction.TransactionalManager;
import com.mybatisflex.core.util.CollectionUtil;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.util.MapUtil;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiConsumer;
import java.util.function.Supplier;
/**
* 针对 RowMapper 的静态方法进行封装
*/
public class Db {
private Db() {
}
private static final Map<String, RowMapperInvoker> INVOKER_MAP = new ConcurrentHashMap<>();
static RowMapperInvoker defaultRowMapperInvoker;
public static RowMapperInvoker invoker() {
if (defaultRowMapperInvoker == null) {
FlexGlobalConfig defaultConfig = FlexGlobalConfig.getDefaultConfig();
SqlSessionFactory sqlSessionFactory = defaultConfig.getSqlSessionFactory();
defaultRowMapperInvoker = new RowMapperInvoker(sqlSessionFactory);
}
return defaultRowMapperInvoker;
}
public static RowMapperInvoker invoker(String environmentId) {
return MapUtil.computeIfAbsent(INVOKER_MAP, environmentId, key -> {
SqlSessionFactory sqlSessionFactory = FlexGlobalConfig.getConfig(key).getSqlSessionFactory();
return new RowMapperInvoker(sqlSessionFactory);
});
}
/**
* 往 schema.tableName 插入一条 row 数据
*
* @param schema 模式
* @param tableName 表名
* @param row 数据
*/
public static int insert(String schema, String tableName, Row row) {
return invoker().insert(schema, tableName, row);
}
/**
* 往 tableName 插入一条 row 数据
*
* @param tableName 表名
* @param row 数据
*/
public static int insert(String tableName, Row row) {
return invoker().insert(null, tableName, row);
}
/**
* 直接编写 sql 插入数据
*
* @param sql sql 内容
* @param args sql 参数
*/
public static int insertBySql(String sql, Object... args) {
return invoker().insertBySql(sql, args);
}
/**
* 批量插入数据
*
* @param schema 模式
* @param tableName 表名
* @param rows 数据
*/
public static int[] insertBatch(String schema, String tableName, Collection<Row> rows) {
return insertBatch(schema, tableName, rows, rows.size());
}
/**
* 批量插入数据
*
* @param tableName 表名
* @param rows 数据
*/
public static int[] insertBatch(String tableName, Collection<Row> rows) {
return insertBatch(null, tableName, rows, rows.size());
}
/**
* 批量插入数据
*
* @param schema 模式
* @param tableName 表名
* @param rows 数据
* @param batchSize 每次提交的数据量
*/
public static int[] insertBatch(String schema, String tableName, Collection<Row> rows, int batchSize) {
return executeBatch(rows, batchSize, RowMapper.class, (mapper, row) -> {
mapper.insert(schema, tableName, row);
});
}
/**
* 批量插入数据
*
* @param tableName 表名
* @param rows 数据
* @param batchSize 每次提交的数据量
*/
public static int[] insertBatch(String tableName, Collection<Row> rows, int batchSize) {
return executeBatch(rows, batchSize, RowMapper.class, (mapper, row) -> {
mapper.insert(null, tableName, row);
});
}
/**
* 批量插入数据,根据第一条内容来构建插入的字段,效率比 {@link #insertBatch(String, String, Collection, int)} 高
*
* @param schema 模式
* @param tableName 表名
* @param rows 数据
*/
public static int insertBatchWithFirstRowColumns(String schema, String tableName, List<Row> rows) {
return invoker().insertBatchWithFirstRowColumns(schema, tableName, rows);
}
/**
* 批量插入数据,根据第一条内容来构建插入的字段,效率比 {@link #insertBatch(String, String, Collection, int)} 高
*
* @param tableName 表名
* @param rows 数据
*/
public static int insertBatchWithFirstRowColumns(String tableName, List<Row> rows) {
return invoker().insertBatchWithFirstRowColumns(null, tableName, rows);
}
/**
* 编写 delete sql 来删除数据
*
* @param sql sql 内容
* @param args 参数
*/
public static int deleteBySql(String sql, Object... args) {
return invoker().deleteBySql(sql, args);
}
/**
* 根据主键来删除数据,其中 row 是通过 {@link Row#ofKey(RowKey, Object)} 来进行构建的
*
* @param schema 模式
* @param tableName 表名
* @param row 主键 和 id值
*/
public static int deleteById(String schema, String tableName, Row row) {
return invoker().deleteById(schema, tableName, row);
}
/**
* 根据主键来删除数据,其中 row 是通过 {@link Row#ofKey(RowKey, Object)} 来进行构建的
*
* @param tableName 表名
* @param row 主键 和 id值
*/
public static int deleteById(String tableName, Row row) {
return invoker().deleteById(null, tableName, row);
}
/**
* 根据主键来删除 1 条数据
*
* @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);
}
/**
* 根据主键来删除 1 条数据
*
* @param tableName 表名
* @param primaryKey 主键字段名称
* @param id 主键值
*/
public static int deleteById(String tableName, String primaryKey, Object id) {
return invoker().deleteById(null, tableName, primaryKey, id);
}
/**
* 根据 id 集合来批量删除数据
*
* @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);
}
/**
* 根据 id 集合来批量删除数据
*
* @param tableName 表名
* @param primaryKey 主键字段名称
* @param ids id 集合
*/
public static int deleteBatchByIds(String tableName, String primaryKey, Collection<?> ids) {
return invoker().deleteBatchByIds(null, tableName, primaryKey, ids);
}
/**
* 根据 map 构建的 where 条件来删除数据
*
* @param schema 模式
* @param tableName 表名
* @param whereColumns where 条件
*/
public static int deleteByMap(String schema, String tableName, Map<String, Object> whereColumns) {
return invoker().deleteByQuery(schema, tableName, new QueryWrapper().where(whereColumns));
}
/**
* 根据 map 构建的 where 条件来删除数据
*
* @param tableName 表名
* @param whereColumns where 条件
*/
public static int deleteByMap(String tableName, Map<String, Object> whereColumns) {
return invoker().deleteByQuery(null, tableName, new QueryWrapper().where(whereColumns));
}
/**
* 根据 condition 条件删除数据
*
* @param schema 模式
* @param tableName 表名
* @param condition 条件内容
*/
public static int deleteByCondition(String schema, String tableName, QueryCondition condition) {
return invoker().deleteByQuery(schema, tableName, new QueryWrapper().where(condition));
}
/**
* 根据 condition 条件删除数据
*
* @param tableName 表名
* @param condition 条件内容
*/
public static int deleteByCondition(String tableName, QueryCondition condition) {
return invoker().deleteByQuery(null, tableName, new QueryWrapper().where(condition));
}
/**
* 根据 query 构建的条件来删除数据
*
* @param schema 模式
* @param tableName 表名
* @param queryWrapper query
*/
public static int deleteByQuery(String schema, String tableName, QueryWrapper queryWrapper) {
return invoker().deleteByQuery(schema, tableName, queryWrapper);
}
/**
* 根据 query 构建的条件来删除数据
*
* @param tableName 表名
* @param queryWrapper query
*/
public static int deleteByQuery(String tableName, QueryWrapper queryWrapper) {
return invoker().deleteByQuery(null, tableName, queryWrapper);
}
/**
* 根据原生 sql 来更新数据
*
* @param sql sql 内容
* @param args sql 参数
*/
public static int updateBySql(String sql, Object... args) {
return invoker().updateBySql(sql, args);
}
/**
* @param sql
* @param batchArgsSetter
* @return
*/
public static int[] updateBatch(String sql, BatchArgsSetter batchArgsSetter) {
int batchSize = batchArgsSetter.getBatchSize();
return invoker().executeBatch(batchSize, batchSize, RowMapper.class
, (mapper, index) -> mapper.updateBySql(sql, batchArgsSetter.getSqlArgs(index)));
}
/**
* 根据 id 来更新数据
*
* @param schema 模式
* @param tableName 表名
* @param row id 及其内容
*/
public static int updateById(String schema, String tableName, Row row) {
return invoker().updateById(schema, tableName, row);
}
/**
* 根据 id 来更新数据
*
* @param tableName 表名
* @param row id 及其内容
*/
public static int updateById(String tableName, Row row) {
return invoker().updateById(null, tableName, row);
}
/**
* 根据 map 构建的条件来更新数据
*
* @param schema 模式
* @param tableName 表名
* @param data 数据内容
* @param whereColumns where 条件
*/
public static int updateByMap(String schema, String tableName, Row data, Map<String, Object> whereColumns) {
return invoker().updateByQuery(schema, tableName, data, new QueryWrapper().where(whereColumns));
}
/**
* 根据 map 构建的条件来更新数据
*
* @param tableName 表名
* @param data 数据内容
* @param whereColumns where 条件
*/
public static int updateByMap(String tableName, Row data, Map<String, Object> whereColumns) {
return invoker().updateByQuery(null, tableName, data, new QueryWrapper().where(whereColumns));
}
/**
* 根据 condition 来更新数据
*
* @param schema 模式
* @param tableName 表名
* @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));
}
/**
* 根据 condition 来更新数据
*
* @param tableName 表名
* @param data 数据
* @param condition 条件
*/
public static int updateByCondition(String tableName, Row data, QueryCondition condition) {
return invoker().updateByQuery(null, tableName, data, new QueryWrapper().where(condition));
}
/**
* 根据 query 构建的条件来更新数据
*
* @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);
}
/**
* 根据 query 构建的条件来更新数据
*
* @param tableName 表名
* @param data 数据内容
* @param queryWrapper queryWrapper 条件
*/
public static int updateByQuery(String tableName, Row data, QueryWrapper queryWrapper) {
return invoker().updateByQuery(null, tableName, data, queryWrapper);
}
/**
* 根据主键来批量更新数据
*
* @param schema 模式
* @param tableName 表名
* @param rows 还有主键的数据
*/
public static int updateBatchById(String schema, String tableName, List<Row> rows) {
return invoker().updateBatchById(schema, tableName, rows);
}
/**
* 根据主键来批量更新数据
*
* @param tableName 表名
* @param rows 还有主键的数据
*/
public static int updateBatchById(String tableName, List<Row> rows) {
return invoker().updateBatchById(null, tableName, rows);
}
/**
* 根据主键来批量更新数据
*
* @param entities 实体
* @param batchSize 批次大小
* @return int
*/
public static <T> int updateEntitiesBatch(Collection<T> entities, int batchSize) {
List<T> list = CollectionUtil.toList(entities);
return Arrays.stream(executeBatch(list, batchSize, RowMapper.class, RowMapper::updateEntity)).sum();
}
/**
* 根据主键来批量更新数据
*
* @param entities 实体
* @return int 影响行数
*/
public static <T> int updateEntitiesBatch(Collection<T> entities) {
return updateEntitiesBatch(entities, RowMapper.DEFAULT_BATCH_SIZE);
}
/**
* 通过 update schema.table set field = field + 1 where ... 的这种方向更新数据库某个字段内容
*
* @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);
}
/**
* 通过 update table set field = field + 1 where ... 的这种方向更新数据库某个字段内容
*
* @param tableName
* @param fieldName
* @param value
* @param queryWrapper
* @return
*/
public static int updateNumberAddByQuery(String tableName, String fieldName, Number value, QueryWrapper queryWrapper) {
return invoker().updateNumberAddByQuery(null, tableName, fieldName, value, queryWrapper);
}
/**
* 批量执行工具方法
*
* @param totalSize 执行总量
* @param batchSize 每一批次的数据量
* @param mapperClass 通过那个 Mapper 来执行
* @param consumer 执行内容
* @param <M> Mapper
* @return 执行影响的行数
*/
public static <M> int[] executeBatch(int totalSize, int batchSize, Class<M> mapperClass, BiConsumer<M, Integer> consumer) {
return invoker().executeBatch(totalSize, batchSize, mapperClass, consumer);
}
/**
* 批量执行工具方法
*
* @param datas 数据
* @param mapperClass mapper 类
* @param consumer 消费者
* @param <M> mapper
* @param <D> 数据类型
* @return 返回每条执行是否成功的结果
*/
public static <M, D> int[] executeBatch(Collection<D> datas, Class<M> mapperClass, BiConsumer<M, D> consumer) {
return executeBatch(datas, RowMapper.DEFAULT_BATCH_SIZE, mapperClass, consumer);
}
/**
* 批量执行工具方法
*
* @param datas 数据
* @param batchSize 每批次执行多少条
* @param mapperClass mapper 类
* @param consumer 消费者
* @param <M> mapper
* @param <E> 数据类型
* @return 返回每条执行是否成功的结果
*/
public static <M, E> int[] executeBatch(Collection<E> datas, int batchSize, Class<M> mapperClass, BiConsumer<M, E> consumer) {
return invoker().executeBatch(datas, batchSize, mapperClass, consumer);
}
/**
* 根据 sql 来查询 1 条数据
*
* @param sql sql 内容
* @param args sql 参数
*/
public static Row selectOneBySql(String sql, Object... args) {
return invoker().selectOneBySql(sql, args);
}
/**
* 根据 id 来查询 1 条数据
*
* @param schema 模式
* @param tableName 表名
* @param row 主键和 id 值
*/
public static Row selectOneById(String schema, String tableName, Row row) {
return invoker().selectOneById(schema, tableName, row);
}
/**
* 根据 id 来查询 1 条数据
*
* @param tableName 表名
* @param row 主键和 id 值
*/
public static Row selectOneById(String tableName, Row row) {
return invoker().selectOneById(null, tableName, row);
}
/**
* 根据主键来查询 1 条数据
*
* @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);
}
/**
* 根据主键来查询 1 条数据
*
* @param tableName 表名
* @param primaryKey 主键字段名称
* @param id 主键值
*/
public static Row selectOneById(String tableName, String primaryKey, Object id) {
return invoker().selectOneById(null, tableName, primaryKey, id);
}
/**
* 根据 map 来查询 1 条数据
*
* @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).limit(1L));
}
/**
* 根据 map 来查询 1 条数据
*
* @param tableName 表名
* @param whereColumns where条件
*/
public static Row selectOneByMap(String tableName, Map whereColumns) {
return invoker().selectOneByQuery(null, tableName, new QueryWrapper().where(whereColumns).limit(1L));
}
/**
* 根据 condition 来查询数据
*
* @param schema 模式
* @param tableName 表名
* @param condition 条件
*/
public static Row selectOneByCondition(String schema, String tableName, QueryCondition condition) {
return invoker().selectOneByQuery(schema, tableName, new QueryWrapper().where(condition).limit(1L));
}
/**
* 根据 condition 来查询数据
*
* @param tableName 表名
* @param condition 条件
*/
public static Row selectOneByCondition(String tableName, QueryCondition condition) {
return invoker().selectOneByQuery(null, tableName, new QueryWrapper().where(condition).limit(1L));
}
/**
* 根据 queryWrapper 来查询 1 条数据
*
* @param schema 模式
* @param tableName 表名
* @param queryWrapper queryWrapper
*/
public static Row selectOneByQuery(String schema, String tableName, QueryWrapper queryWrapper) {
return invoker().selectOneByQuery(schema, tableName, queryWrapper);
}
/**
* 根据 queryWrapper 来查询 1 条数据
*
* @param tableName 表名
* @param queryWrapper queryWrapper
*/
public static Row selectOneByQuery(String tableName, QueryWrapper queryWrapper) {
return invoker().selectOneByQuery(null, tableName, queryWrapper);
}
/**
* 直接根据 queryWrapper 查询 1 条数据
*
* @param queryWrapper 必须带有 from 的 queryWrapper
*/
public static Row selectOneByQuery(QueryWrapper queryWrapper) {
List<QueryTable> queryTables = CPI.getQueryTables(queryWrapper);
if (queryTables == null || queryTables.isEmpty()) {
throw FlexExceptions.wrap("table must not be null or empty in Db.selectOneByQuery");
}
return invoker().selectOneByQuery(null, null, queryWrapper);
}
/**
* 通过 sql 来查询多条数据
*
* @param sql sql 内容
* @param args sql 参数
*/
public static List<Row> selectListBySql(String sql, Object... args) {
return invoker().selectListBySql(sql, args);
}
/**
* 通过 map 构建的条件来查询数据列表
*
* @param schema 模式
* @param tableName 表名
* @param whereColumns where 条件
*/
public static List<Row> selectListByMap(String schema, String tableName, Map<String, Object> whereColumns) {
return invoker().selectListByQuery(schema, tableName, new QueryWrapper().where(whereColumns));
}
/**
* 通过 map 构建的条件来查询数据列表
*
* @param tableName 表名
* @param whereColumns where 条件
*/
public static List<Row> selectListByMap(String tableName, Map<String, Object> whereColumns) {
return invoker().selectListByQuery(null, tableName, new QueryWrapper().where(whereColumns));
}
/**
* 根据 map 构建的条件来查询数据列表
*
* @param schema 模式
* @param tableName 表名
* @param whereColumns 条件
* @param count 数据量
*/
public static List<Row> selectListByMap(String schema, String tableName, Map<String, Object> whereColumns, Long count) {
return invoker().selectListByQuery(schema, tableName, new QueryWrapper().where(whereColumns).limit(count));
}
/**
* 根据 map 构建的条件来查询数据列表
*
* @param tableName 表名
* @param whereColumns 条件
* @param count 数据量
*/
public static List<Row> selectListByMap(String tableName, Map<String, Object> whereColumns, Long count) {
return invoker().selectListByQuery(null, tableName, new QueryWrapper().where(whereColumns).limit(count));
}
/**
* 通过 condition 条件来查询数据列表
*
* @param schema 模式
* @param tableName 表名
* @param condition where 条件
*/
public static List<Row> selectListByCondition(String schema, String tableName, QueryCondition condition) {
return invoker().selectListByQuery(schema, tableName, new QueryWrapper().where(condition));
}
/**
* 通过 condition 条件来查询数据列表
*
* @param tableName 表名
* @param condition where 条件
*/
public static List<Row> selectListByCondition(String tableName, QueryCondition condition) {
return invoker().selectListByQuery(null, tableName, new QueryWrapper().where(condition));
}
/**
* 根据 condition 条件来查询数据列表
*
* @param schema 模式
* @param tableName 表名
* @param condition 条件
* @param count 数据量
*/
public static List<Row> selectListByCondition(String schema, String tableName, QueryCondition condition, Long count) {
return invoker().selectListByQuery(schema, tableName, new QueryWrapper().where(condition).limit(count));
}
/**
* 根据 condition 条件来查询数据列表
*
* @param tableName 表名
* @param condition 条件
* @param count 数据量
*/
public static List<Row> selectListByCondition(String tableName, QueryCondition condition, Long count) {
return invoker().selectListByQuery(null, tableName, new QueryWrapper().where(condition).limit(count));
}
/**
* 通过 query 来查询数据列表
*
* @param schema 模式
* @param tableName 表名
* @param queryWrapper query 条件
*/
public static List<Row> selectListByQuery(String schema, String tableName, QueryWrapper queryWrapper) {
return invoker().selectListByQuery(schema, tableName, queryWrapper);
}
/**
* 通过 query 来查询数据列表
*
* @param tableName 表名
* @param queryWrapper query 条件
*/
public static List<Row> selectListByQuery(String tableName, QueryWrapper queryWrapper) {
return invoker().selectListByQuery(null, tableName, queryWrapper);
}
/**
* 通过 query 来查询数据列表
*
* @param queryWrapper 必须带有 from 的 queryWrapper
*/
public static List<Row> selectListByQuery(QueryWrapper queryWrapper) {
List<QueryTable> queryTables = CPI.getQueryTables(queryWrapper);
if (queryTables == null || queryTables.isEmpty()) {
throw FlexExceptions.wrap("table must not be null or empty in Db.selectListByQuery");
}
return invoker().selectListByQuery(null, null, queryWrapper);
}
/**
* 查询某张表的所有数据
*
* @param schema 模式
* @param tableName 表名
*/
public static List<Row> selectAll(String schema, String tableName) {
return invoker().selectAll(schema, tableName);
}
/**
* 查询某张表的所有数据
*
* @param tableName 表名
*/
public static List<Row> selectAll(String tableName) {
return invoker().selectAll(null, tableName);
}
/**
* 查询某个内容,数据返回的应该只有 1 行 1 列
*
* @param sql sql 内容
* @param args sql 参数
*/
public static Object selectObject(String sql, Object... args) {
return invoker().selectObject(sql, args);
}
/**
* 根据 queryWrapper 查询内容,数据返回的应该只有 1 行 1 列
*
* @param schema 模式
* @param tableName 表名
* @param queryWrapper query 封装
* @return 数据内容
*/
public static Object selectObject(String schema, String tableName, QueryWrapper queryWrapper) {
return invoker().selectObjectByQuery(schema, tableName, queryWrapper);
}
/**
* 根据 queryWrapper 查询内容,数据返回的应该只有 1 行 1 列
*
* @param tableName 表名
* @param queryWrapper query 封装
* @return 数据内容
*/
public static Object selectObject(String tableName, QueryWrapper queryWrapper) {
return invoker().selectObjectByQuery(null, tableName, queryWrapper);
}
/**
* 根据 queryWrapper 查询内容,数据返回的应该只有 1 行 1 列
*
* @param queryWrapper query 封装
* @return 数据内容
*/
public static Object selectObject(QueryWrapper queryWrapper) {
return invoker().selectObjectByQuery(null, null, queryWrapper);
}
/**
* 查询某列内容,数据返回应该有 多行 1 列
*
* @param sql sql 内容
* @param args sql 参数
*/
public static List<Object> selectObjectList(String sql, Object... args) {
return invoker().selectObjectList(sql, args);
}
/**
* 根据 queryWrapper 查询内容,数据返回的应该只有 1 行 1 列
*
* @param schema 模式
* @param tableName 表名
* @param queryWrapper query 封装
* @return 数据内容
*/
public static List<Object> selectObjectList(String schema, String tableName, QueryWrapper queryWrapper) {
return invoker().selectObjectListByQuery(schema, tableName, queryWrapper);
}
/**
* 根据 queryWrapper 查询内容,数据返回的应该只有 1 行 1 列
*
* @param tableName 表名
* @param queryWrapper query 封装
* @return 数据内容
*/
public static List<Object> selectObjectList(String tableName, QueryWrapper queryWrapper) {
return invoker().selectObjectListByQuery(null, tableName, queryWrapper);
}
/**
* 根据 queryWrapper 查询内容,数据返回的应该只有 1 行 1 列
*
* @param queryWrapper query 封装
* @return 数据内容
*/
public static List<Object> selectObjectList(QueryWrapper queryWrapper) {
return invoker().selectObjectListByQuery(null, null, queryWrapper);
}
/**
* 查收 count 数据,一般用于 select count(*)...
* 或者返回的内容是一行1列,且是数值类型的也可以用此方法
*
* @param sql sql 内容
* @param args sql 参数
*/
public static long selectCount(String sql, Object... args) {
return invoker().selectCount(sql, args);
}
/**
* 根据 condition 条件来查询数量
*
* @param schema 模式
* @param tableName 表名
* @param condition 条件
*/
public static long selectCountByCondition(String schema, String tableName, QueryCondition condition) {
return invoker().selectCountByQuery(schema, tableName, new QueryWrapper().where(condition));
}
/**
* 根据 condition 条件来查询数量
*
* @param tableName 表名
* @param condition 条件
*/
public static long selectCountByCondition(String tableName, QueryCondition condition) {
return invoker().selectCountByQuery(null, tableName, new QueryWrapper().where(condition));
}
/**
* 根据 query 构建的条件来查询数据量
*
* @param schema 模式
* @param tableName 表名
* @param queryWrapper query 条件
*/
public static long selectCountByQuery(String schema, String tableName, QueryWrapper queryWrapper) {
return invoker().selectCountByQuery(schema, tableName, queryWrapper);
}
/**
* 根据 query 构建的条件来查询数据量
*
* @param tableName 表名
* @param queryWrapper query 条件
*/
public static long selectCountByQuery(String tableName, QueryWrapper queryWrapper) {
return invoker().selectCountByQuery(null, tableName, queryWrapper);
}
/**
* 直接根据 query 来查询数据量
*
* @param queryWrapper 必须带有表名的 queryWrapper
* @return 数据量
*/
public static long selectCountByQuery(QueryWrapper queryWrapper) {
List<QueryTable> queryTables = CPI.getQueryTables(queryWrapper);
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);
}
/**
* 分页查询
*
* @param schema 模式
* @param tableName 表名
* @param pageNumber 当前的页码
* @param pageSize 每页的数据量
* @param condition 条件
*/
public static Page<Row> paginate(String schema, String tableName, Number pageNumber, Number pageSize, QueryCondition condition) {
return invoker().paginate(schema, tableName, new Page<>(pageNumber, pageSize), QueryWrapper.create().where(condition));
}
/**
* 分页查询
*
* @param tableName 表名
* @param pageNumber 当前的页码
* @param pageSize 每页的数据量
* @param condition 条件
*/
public static Page<Row> paginate(String tableName, Number pageNumber, Number pageSize, QueryCondition condition) {
return invoker().paginate(null, tableName, new Page<>(pageNumber, pageSize), QueryWrapper.create().where(condition));
}
/**
* 分页查询
*
* @param schema 模式
* @param tableName 表名
* @param pageNumber 当前的页码
* @param pageSize 每页的数据量
* @param totalRow 数据总量
* @param condition 条件
*/
public static Page<Row> paginate(String schema, String tableName, Number pageNumber, Number pageSize, Number totalRow, QueryCondition condition) {
return invoker().paginate(schema, tableName, new Page<>(pageNumber, pageSize, totalRow), QueryWrapper.create().where(condition));
}
/**
* 分页查询
*
* @param tableName 表名
* @param pageNumber 当前的页码
* @param pageSize 每页的数据量
* @param totalRow 数据总量
* @param condition 条件
*/
public static Page<Row> paginate(String tableName, Number pageNumber, Number pageSize, Number totalRow, QueryCondition condition) {
return invoker().paginate(null, tableName, new Page<>(pageNumber, pageSize, totalRow), QueryWrapper.create().where(condition));
}
/**
* 分页查询
*
* @param schema 模式
* @param tableName 表名
* @param pageNumber 当前的页码
* @param pageSize 每页的数据量
* @param queryWrapper 条件
*/
public static Page<Row> paginate(String schema, String tableName, Number pageNumber, Number pageSize, QueryWrapper queryWrapper) {
return invoker().paginate(schema, tableName, new Page<>(pageNumber, pageSize), queryWrapper);
}
/**
* 分页查询
*
* @param tableName 表名
* @param pageNumber 当前的页码
* @param pageSize 每页的数据量
* @param queryWrapper 条件
*/
public static Page<Row> paginate(String tableName, Number pageNumber, Number pageSize, QueryWrapper queryWrapper) {
return invoker().paginate(null, tableName, new Page<>(pageNumber, pageSize), queryWrapper);
}
/**
* 分页查询
*
* @param schema 模式
* @param tableName 表名
* @param pageNumber 当前的页码
* @param pageSize 每页的数据量
* @param totalRow 数据总量
* @param queryWrapper 条件
*/
public static Page<Row> paginate(String schema, String tableName, Number pageNumber, Number pageSize, Number totalRow, QueryWrapper queryWrapper) {
return invoker().paginate(schema, tableName, new Page<>(pageNumber, pageSize, totalRow), queryWrapper);
}
/**
* 分页查询
*
* @param tableName 表名
* @param pageNumber 当前的页码
* @param pageSize 每页的数据量
* @param totalRow 数据总量
* @param queryWrapper 条件
*/
public static Page<Row> paginate(String tableName, Number pageNumber, Number pageSize, Number totalRow, QueryWrapper queryWrapper) {
return invoker().paginate(null, tableName, new Page<>(pageNumber, pageSize, totalRow), queryWrapper);
}
/**
* 分页查询
*
* @param schema 模式
* @param tableName 表名
* @param page page 对象,若 page 有 totalCount 值,则不会再去查询分类的数据总量
* @param queryWrapper 条件
*/
public static Page<Row> paginate(String schema, String tableName, Page<Row> page, QueryWrapper queryWrapper) {
return invoker().paginate(schema, tableName, page, queryWrapper);
}
/**
* 分页查询
*
* @param tableName 表名
* @param page page 对象,若 page 有 totalCount 值,则不会再去查询分类的数据总量
* @param queryWrapper 条件
*/
public static Page<Row> paginate(String tableName, Page<Row> page, QueryWrapper queryWrapper) {
return invoker().paginate(null, tableName, page, queryWrapper);
}
/**
* 进行事务操作,返回 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, 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);
}
}
1
https://gitee.com/duxlei/mybatis-flex.git
git@gitee.com:duxlei/mybatis-flex.git
duxlei
mybatis-flex
MyBatis-Flex
main

搜索帮助