001/* 002 * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). 003 * <p> 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * <p> 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * <p> 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package com.mybatisflex.core.row; 017 018import com.mybatisflex.core.FlexGlobalConfig; 019import com.mybatisflex.core.exception.FlexExceptions; 020import com.mybatisflex.core.paginate.Page; 021import com.mybatisflex.core.query.CPI; 022import com.mybatisflex.core.query.QueryCondition; 023import com.mybatisflex.core.query.QueryTable; 024import com.mybatisflex.core.query.QueryWrapper; 025import com.mybatisflex.core.transaction.Propagation; 026import com.mybatisflex.core.transaction.TransactionalManager; 027import com.mybatisflex.core.util.CollectionUtil; 028import org.apache.ibatis.session.SqlSessionFactory; 029import org.apache.ibatis.util.MapUtil; 030 031import java.util.Arrays; 032import java.util.Collection; 033import java.util.List; 034import java.util.Map; 035import java.util.concurrent.ConcurrentHashMap; 036import java.util.function.BiConsumer; 037import java.util.function.Supplier; 038 039/** 040 * 针对 RowMapper 的静态方法进行封装 041 */ 042public class Db { 043 044 private Db() { 045 } 046 047 private static final Map<String, RowMapperInvoker> INVOKER_MAP = new ConcurrentHashMap<>(); 048 static RowMapperInvoker defaultRowMapperInvoker; 049 050 public static RowMapperInvoker invoker() { 051 if (defaultRowMapperInvoker == null) { 052 FlexGlobalConfig defaultConfig = FlexGlobalConfig.getDefaultConfig(); 053 SqlSessionFactory sqlSessionFactory = defaultConfig.getSqlSessionFactory(); 054 defaultRowMapperInvoker = new RowMapperInvoker(sqlSessionFactory); 055 } 056 return defaultRowMapperInvoker; 057 } 058 059 public static RowMapperInvoker invoker(String environmentId) { 060 return MapUtil.computeIfAbsent(INVOKER_MAP, environmentId, key -> { 061 SqlSessionFactory sqlSessionFactory = FlexGlobalConfig.getConfig(key).getSqlSessionFactory(); 062 return new RowMapperInvoker(sqlSessionFactory); 063 }); 064 } 065 066 067 /** 068 * 往 schema.tableName 插入一条 row 数据 069 * 070 * @param schema 模式 071 * @param tableName 表名 072 * @param row 数据 073 */ 074 public static int insert(String schema, String tableName, Row row) { 075 return invoker().insert(schema, tableName, row); 076 } 077 078 /** 079 * 往 tableName 插入一条 row 数据 080 * 081 * @param tableName 表名 082 * @param row 数据 083 */ 084 public static int insert(String tableName, Row row) { 085 return invoker().insert(null, tableName, row); 086 } 087 088 089 /** 090 * 直接编写 sql 插入数据 091 * 092 * @param sql sql 内容 093 * @param args sql 参数 094 */ 095 public static int insertBySql(String sql, Object... args) { 096 return invoker().insertBySql(sql, args); 097 } 098 099 100 /** 101 * 批量插入数据 102 * 103 * @param schema 模式 104 * @param tableName 表名 105 * @param rows 数据 106 */ 107 public static int[] insertBatch(String schema, String tableName, Collection<Row> rows) { 108 return insertBatch(schema, tableName, rows, rows.size()); 109 } 110 111 /** 112 * 批量插入数据 113 * 114 * @param tableName 表名 115 * @param rows 数据 116 */ 117 public static int[] insertBatch(String tableName, Collection<Row> rows) { 118 return insertBatch(null, tableName, rows, rows.size()); 119 } 120 121 /** 122 * 批量插入数据 123 * 124 * @param schema 模式 125 * @param tableName 表名 126 * @param rows 数据 127 * @param batchSize 每次提交的数据量 128 */ 129 public static int[] insertBatch(String schema, String tableName, Collection<Row> rows, int batchSize) { 130 return executeBatch(rows, batchSize, RowMapper.class, (mapper, row) -> { 131 mapper.insert(schema, tableName, row); 132 }); 133 } 134 135 /** 136 * 批量插入数据 137 * 138 * @param tableName 表名 139 * @param rows 数据 140 * @param batchSize 每次提交的数据量 141 */ 142 public static int[] insertBatch(String tableName, Collection<Row> rows, int batchSize) { 143 return executeBatch(rows, batchSize, RowMapper.class, (mapper, row) -> { 144 mapper.insert(null, tableName, row); 145 }); 146 } 147 148 /** 149 * 批量插入数据,根据第一条内容来构建插入的字段,效率比 {@link #insertBatch(String, String, Collection, int)} 高 150 * 151 * @param schema 模式 152 * @param tableName 表名 153 * @param rows 数据 154 */ 155 public static int insertBatchWithFirstRowColumns(String schema, String tableName, List<Row> rows) { 156 return invoker().insertBatchWithFirstRowColumns(schema, tableName, rows); 157 } 158 159 /** 160 * 批量插入数据,根据第一条内容来构建插入的字段,效率比 {@link #insertBatch(String, String, Collection, int)} 高 161 * 162 * @param tableName 表名 163 * @param rows 数据 164 */ 165 public static int insertBatchWithFirstRowColumns(String tableName, List<Row> rows) { 166 return invoker().insertBatchWithFirstRowColumns(null, tableName, rows); 167 } 168 169 /** 170 * 编写 delete sql 来删除数据 171 * 172 * @param sql sql 内容 173 * @param args 参数 174 */ 175 public static int deleteBySql(String sql, Object... args) { 176 return invoker().deleteBySql(sql, args); 177 } 178 179 /** 180 * 根据主键来删除数据,其中 row 是通过 {@link Row#ofKey(RowKey, Object)} 来进行构建的 181 * 182 * @param schema 模式 183 * @param tableName 表名 184 * @param row 主键 和 id值 185 */ 186 public static int deleteById(String schema, String tableName, Row row) { 187 return invoker().deleteById(schema, tableName, row); 188 } 189 190 /** 191 * 根据主键来删除数据,其中 row 是通过 {@link Row#ofKey(RowKey, Object)} 来进行构建的 192 * 193 * @param tableName 表名 194 * @param row 主键 和 id值 195 */ 196 public static int deleteById(String tableName, Row row) { 197 return invoker().deleteById(null, tableName, row); 198 } 199 200 201 /** 202 * 根据主键来删除 1 条数据 203 * 204 * @param schema 模式 205 * @param tableName 表名 206 * @param primaryKey 主键字段名称 207 * @param id 主键值 208 */ 209 public static int deleteById(String schema, String tableName, String primaryKey, Object id) { 210 return invoker().deleteById(schema, tableName, primaryKey, id); 211 } 212 213 /** 214 * 根据主键来删除 1 条数据 215 * 216 * @param tableName 表名 217 * @param primaryKey 主键字段名称 218 * @param id 主键值 219 */ 220 public static int deleteById(String tableName, String primaryKey, Object id) { 221 return invoker().deleteById(null, tableName, primaryKey, id); 222 } 223 224 225 /** 226 * 根据 id 集合来批量删除数据 227 * 228 * @param schema 模式 229 * @param tableName 表名 230 * @param primaryKey 主键字段名称 231 * @param ids id 集合 232 */ 233 public static int deleteBatchByIds(String schema, String tableName, String primaryKey, Collection<?> ids) { 234 return invoker().deleteBatchByIds(schema, tableName, primaryKey, ids); 235 } 236 237 /** 238 * 根据 id 集合来批量删除数据 239 * 240 * @param tableName 表名 241 * @param primaryKey 主键字段名称 242 * @param ids id 集合 243 */ 244 public static int deleteBatchByIds(String tableName, String primaryKey, Collection<?> ids) { 245 return invoker().deleteBatchByIds(null, tableName, primaryKey, ids); 246 } 247 248 /** 249 * 根据 map 构建的 where 条件来删除数据 250 * 251 * @param schema 模式 252 * @param tableName 表名 253 * @param whereColumns where 条件 254 */ 255 public static int deleteByMap(String schema, String tableName, Map<String, Object> whereColumns) { 256 return invoker().deleteByQuery(schema, tableName, new QueryWrapper().where(whereColumns)); 257 } 258 259 /** 260 * 根据 map 构建的 where 条件来删除数据 261 * 262 * @param tableName 表名 263 * @param whereColumns where 条件 264 */ 265 public static int deleteByMap(String tableName, Map<String, Object> whereColumns) { 266 return invoker().deleteByQuery(null, tableName, new QueryWrapper().where(whereColumns)); 267 } 268 269 /** 270 * 根据 condition 条件删除数据 271 * 272 * @param schema 模式 273 * @param tableName 表名 274 * @param condition 条件内容 275 */ 276 public static int deleteByCondition(String schema, String tableName, QueryCondition condition) { 277 return invoker().deleteByQuery(schema, tableName, new QueryWrapper().where(condition)); 278 } 279 280 /** 281 * 根据 condition 条件删除数据 282 * 283 * @param tableName 表名 284 * @param condition 条件内容 285 */ 286 public static int deleteByCondition(String tableName, QueryCondition condition) { 287 return invoker().deleteByQuery(null, tableName, new QueryWrapper().where(condition)); 288 } 289 290 291 /** 292 * 根据 query 构建的条件来删除数据 293 * 294 * @param schema 模式 295 * @param tableName 表名 296 * @param queryWrapper query 297 */ 298 public static int deleteByQuery(String schema, String tableName, QueryWrapper queryWrapper) { 299 return invoker().deleteByQuery(schema, tableName, queryWrapper); 300 } 301 302 /** 303 * 根据 query 构建的条件来删除数据 304 * 305 * @param tableName 表名 306 * @param queryWrapper query 307 */ 308 public static int deleteByQuery(String tableName, QueryWrapper queryWrapper) { 309 return invoker().deleteByQuery(null, tableName, queryWrapper); 310 } 311 312 /** 313 * 根据原生 sql 来更新数据 314 * 315 * @param sql sql 内容 316 * @param args sql 参数 317 */ 318 public static int updateBySql(String sql, Object... args) { 319 return invoker().updateBySql(sql, args); 320 } 321 322 323 /** 324 * @param sql 325 * @param batchArgsSetter 326 * @return 327 */ 328 public static int[] updateBatch(String sql, BatchArgsSetter batchArgsSetter) { 329 int batchSize = batchArgsSetter.getBatchSize(); 330 return invoker().executeBatch(batchSize, batchSize, RowMapper.class 331 , (mapper, index) -> mapper.updateBySql(sql, batchArgsSetter.getSqlArgs(index))); 332 333 } 334 335 336 /** 337 * 根据 id 来更新数据 338 * 339 * @param schema 模式 340 * @param tableName 表名 341 * @param row id 及其内容 342 */ 343 public static int updateById(String schema, String tableName, Row row) { 344 return invoker().updateById(schema, tableName, row); 345 } 346 347 348 /** 349 * 根据 id 来更新数据 350 * 351 * @param tableName 表名 352 * @param row id 及其内容 353 */ 354 public static int updateById(String tableName, Row row) { 355 return invoker().updateById(null, tableName, row); 356 } 357 358 /** 359 * 根据 map 构建的条件来更新数据 360 * 361 * @param schema 模式 362 * @param tableName 表名 363 * @param data 数据内容 364 * @param whereColumns where 条件 365 */ 366 public static int updateByMap(String schema, String tableName, Row data, Map<String, Object> whereColumns) { 367 return invoker().updateByQuery(schema, tableName, data, new QueryWrapper().where(whereColumns)); 368 } 369 370 371 /** 372 * 根据 map 构建的条件来更新数据 373 * 374 * @param tableName 表名 375 * @param data 数据内容 376 * @param whereColumns where 条件 377 */ 378 public static int updateByMap(String tableName, Row data, Map<String, Object> whereColumns) { 379 return invoker().updateByQuery(null, tableName, data, new QueryWrapper().where(whereColumns)); 380 } 381 382 /** 383 * 根据 condition 来更新数据 384 * 385 * @param schema 模式 386 * @param tableName 表名 387 * @param data 数据 388 * @param condition 条件 389 */ 390 public static int updateByCondition(String schema, String tableName, Row data, QueryCondition condition) { 391 return invoker().updateByQuery(schema, tableName, data, new QueryWrapper().where(condition)); 392 } 393 394 /** 395 * 根据 condition 来更新数据 396 * 397 * @param tableName 表名 398 * @param data 数据 399 * @param condition 条件 400 */ 401 public static int updateByCondition(String tableName, Row data, QueryCondition condition) { 402 return invoker().updateByQuery(null, tableName, data, new QueryWrapper().where(condition)); 403 } 404 405 406 /** 407 * 根据 query 构建的条件来更新数据 408 * 409 * @param schema 模式 410 * @param tableName 表名 411 * @param data 数据内容 412 * @param queryWrapper queryWrapper 条件 413 */ 414 public static int updateByQuery(String schema, String tableName, Row data, QueryWrapper queryWrapper) { 415 return invoker().updateByQuery(schema, tableName, data, queryWrapper); 416 } 417 418 /** 419 * 根据 query 构建的条件来更新数据 420 * 421 * @param tableName 表名 422 * @param data 数据内容 423 * @param queryWrapper queryWrapper 条件 424 */ 425 public static int updateByQuery(String tableName, Row data, QueryWrapper queryWrapper) { 426 return invoker().updateByQuery(null, tableName, data, queryWrapper); 427 } 428 429 430 /** 431 * 根据主键来批量更新数据 432 * 433 * @param schema 模式 434 * @param tableName 表名 435 * @param rows 还有主键的数据 436 */ 437 public static int updateBatchById(String schema, String tableName, List<Row> rows) { 438 return invoker().updateBatchById(schema, tableName, rows); 439 } 440 441 /** 442 * 根据主键来批量更新数据 443 * 444 * @param tableName 表名 445 * @param rows 还有主键的数据 446 */ 447 public static int updateBatchById(String tableName, List<Row> rows) { 448 return invoker().updateBatchById(null, tableName, rows); 449 } 450 451 452 /** 453 * 根据主键来批量更新数据 454 * 455 * @param entities 实体 456 * @param batchSize 批次大小 457 * @return int 458 */ 459 public static <T> int updateEntitiesBatch(Collection<T> entities, int batchSize) { 460 List<T> list = CollectionUtil.toList(entities); 461 return Arrays.stream(executeBatch(list, batchSize, RowMapper.class, RowMapper::updateEntity)).sum(); 462 } 463 464 465 /** 466 * 根据主键来批量更新数据 467 * 468 * @param entities 实体 469 * @return int 影响行数 470 */ 471 public static <T> int updateEntitiesBatch(Collection<T> entities) { 472 return updateEntitiesBatch(entities, RowMapper.DEFAULT_BATCH_SIZE); 473 } 474 475 476 477 /** 478 * 批量执行工具方法 479 * 480 * @param totalSize 执行总量 481 * @param batchSize 每一批次的数据量 482 * @param mapperClass 通过那个 Mapper 来执行 483 * @param consumer 执行内容 484 * @param <M> Mapper 485 * @return 执行影响的行数 486 */ 487 public static <M> int[] executeBatch(int totalSize, int batchSize, Class<M> mapperClass, BiConsumer<M, Integer> consumer) { 488 return invoker().executeBatch(totalSize, batchSize, mapperClass, consumer); 489 } 490 491 492 /** 493 * 批量执行工具方法 494 * 495 * @param datas 数据 496 * @param mapperClass mapper 类 497 * @param consumer 消费者 498 * @param <M> mapper 499 * @param <D> 数据类型 500 * @return 返回每条执行是否成功的结果 501 */ 502 public static <M, D> int[] executeBatch(Collection<D> datas, Class<M> mapperClass, BiConsumer<M, D> consumer) { 503 return executeBatch(datas, RowMapper.DEFAULT_BATCH_SIZE, mapperClass, consumer); 504 } 505 506 507 /** 508 * 批量执行工具方法 509 * 510 * @param datas 数据 511 * @param batchSize 每批次执行多少条 512 * @param mapperClass mapper 类 513 * @param consumer 消费者 514 * @param <M> mapper 515 * @param <E> 数据类型 516 * @return 返回每条执行是否成功的结果 517 */ 518 public static <M, E> int[] executeBatch(Collection<E> datas, int batchSize, Class<M> mapperClass, BiConsumer<M, E> consumer) { 519 return invoker().executeBatch(datas, batchSize, mapperClass, consumer); 520 } 521 522 /** 523 * 根据 sql 来查询 1 条数据 524 * 525 * @param sql sql 内容 526 * @param args sql 参数 527 */ 528 public static Row selectOneBySql(String sql, Object... args) { 529 return invoker().selectOneBySql(sql, args); 530 } 531 532 533 /** 534 * 根据 id 来查询 1 条数据 535 * 536 * @param schema 模式 537 * @param tableName 表名 538 * @param row 主键和 id 值 539 */ 540 public static Row selectOneById(String schema, String tableName, Row row) { 541 return invoker().selectOneById(schema, tableName, row); 542 } 543 544 /** 545 * 根据 id 来查询 1 条数据 546 * 547 * @param tableName 表名 548 * @param row 主键和 id 值 549 */ 550 public static Row selectOneById(String tableName, Row row) { 551 return invoker().selectOneById(null, tableName, row); 552 } 553 554 555 /** 556 * 根据主键来查询 1 条数据 557 * 558 * @param schema 模式 559 * @param tableName 表名 560 * @param primaryKey 主键字段名称 561 * @param id 主键值 562 */ 563 public static Row selectOneById(String schema, String tableName, String primaryKey, Object id) { 564 return invoker().selectOneById(schema, tableName, primaryKey, id); 565 } 566 567 /** 568 * 根据主键来查询 1 条数据 569 * 570 * @param tableName 表名 571 * @param primaryKey 主键字段名称 572 * @param id 主键值 573 */ 574 public static Row selectOneById(String tableName, String primaryKey, Object id) { 575 return invoker().selectOneById(null, tableName, primaryKey, id); 576 } 577 578 /** 579 * 根据 map 来查询 1 条数据 580 * 581 * @param schema 模式 582 * @param tableName 表名 583 * @param whereColumns where条件 584 */ 585 public static Row selectOneByMap(String schema, String tableName, Map whereColumns) { 586 return invoker().selectOneByQuery(schema, tableName, new QueryWrapper().where(whereColumns).limit(1L)); 587 } 588 589 590 /** 591 * 根据 map 来查询 1 条数据 592 * 593 * @param tableName 表名 594 * @param whereColumns where条件 595 */ 596 public static Row selectOneByMap(String tableName, Map whereColumns) { 597 return invoker().selectOneByQuery(null, tableName, new QueryWrapper().where(whereColumns).limit(1L)); 598 } 599 600 /** 601 * 根据 condition 来查询数据 602 * 603 * @param schema 模式 604 * @param tableName 表名 605 * @param condition 条件 606 */ 607 public static Row selectOneByCondition(String schema, String tableName, QueryCondition condition) { 608 return invoker().selectOneByQuery(schema, tableName, new QueryWrapper().where(condition).limit(1L)); 609 } 610 611 /** 612 * 根据 condition 来查询数据 613 * 614 * @param tableName 表名 615 * @param condition 条件 616 */ 617 public static Row selectOneByCondition(String tableName, QueryCondition condition) { 618 return invoker().selectOneByQuery(null, tableName, new QueryWrapper().where(condition).limit(1L)); 619 } 620 621 622 /** 623 * 根据 queryWrapper 来查询 1 条数据 624 * 625 * @param schema 模式 626 * @param tableName 表名 627 * @param queryWrapper queryWrapper 628 */ 629 public static Row selectOneByQuery(String schema, String tableName, QueryWrapper queryWrapper) { 630 return invoker().selectOneByQuery(schema, tableName, queryWrapper); 631 } 632 633 /** 634 * 根据 queryWrapper 来查询 1 条数据 635 * 636 * @param tableName 表名 637 * @param queryWrapper queryWrapper 638 */ 639 public static Row selectOneByQuery(String tableName, QueryWrapper queryWrapper) { 640 return invoker().selectOneByQuery(null, tableName, queryWrapper); 641 } 642 643 644 /** 645 * 直接根据 queryWrapper 查询 1 条数据 646 * 647 * @param queryWrapper 必须带有 from 的 queryWrapper 648 */ 649 public static Row selectOneByQuery(QueryWrapper queryWrapper) { 650 List<QueryTable> queryTables = CPI.getQueryTables(queryWrapper); 651 if (queryTables == null || queryTables.isEmpty()) { 652 throw FlexExceptions.wrap("table must not be null or empty in Db.selectOneByQuery"); 653 } 654 return invoker().selectOneByQuery(null, null, queryWrapper); 655 } 656 657 658 /** 659 * 通过 sql 来查询多条数据 660 * 661 * @param sql sql 内容 662 * @param args sql 参数 663 */ 664 public static List<Row> selectListBySql(String sql, Object... args) { 665 return invoker().selectListBySql(sql, args); 666 } 667 668 669 /** 670 * 通过 map 构建的条件来查询数据列表 671 * 672 * @param schema 模式 673 * @param tableName 表名 674 * @param whereColumns where 条件 675 */ 676 public static List<Row> selectListByMap(String schema, String tableName, Map<String, Object> whereColumns) { 677 return invoker().selectListByQuery(schema, tableName, new QueryWrapper().where(whereColumns)); 678 } 679 680 /** 681 * 通过 map 构建的条件来查询数据列表 682 * 683 * @param tableName 表名 684 * @param whereColumns where 条件 685 */ 686 public static List<Row> selectListByMap(String tableName, Map<String, Object> whereColumns) { 687 return invoker().selectListByQuery(null, tableName, new QueryWrapper().where(whereColumns)); 688 } 689 690 691 /** 692 * 根据 map 构建的条件来查询数据列表 693 * 694 * @param schema 模式 695 * @param tableName 表名 696 * @param whereColumns 条件 697 * @param count 数据量 698 */ 699 public static List<Row> selectListByMap(String schema, String tableName, Map<String, Object> whereColumns, Long count) { 700 return invoker().selectListByQuery(schema, tableName, new QueryWrapper().where(whereColumns).limit(count)); 701 } 702 703 /** 704 * 根据 map 构建的条件来查询数据列表 705 * 706 * @param tableName 表名 707 * @param whereColumns 条件 708 * @param count 数据量 709 */ 710 public static List<Row> selectListByMap(String tableName, Map<String, Object> whereColumns, Long count) { 711 return invoker().selectListByQuery(null, tableName, new QueryWrapper().where(whereColumns).limit(count)); 712 } 713 714 715 /** 716 * 通过 condition 条件来查询数据列表 717 * 718 * @param schema 模式 719 * @param tableName 表名 720 * @param condition where 条件 721 */ 722 public static List<Row> selectListByCondition(String schema, String tableName, QueryCondition condition) { 723 return invoker().selectListByQuery(schema, tableName, new QueryWrapper().where(condition)); 724 } 725 726 727 /** 728 * 通过 condition 条件来查询数据列表 729 * 730 * @param tableName 表名 731 * @param condition where 条件 732 */ 733 public static List<Row> selectListByCondition(String tableName, QueryCondition condition) { 734 return invoker().selectListByQuery(null, tableName, new QueryWrapper().where(condition)); 735 } 736 737 /** 738 * 根据 condition 条件来查询数据列表 739 * 740 * @param schema 模式 741 * @param tableName 表名 742 * @param condition 条件 743 * @param count 数据量 744 */ 745 public static List<Row> selectListByCondition(String schema, String tableName, QueryCondition condition, Long count) { 746 return invoker().selectListByQuery(schema, tableName, new QueryWrapper().where(condition).limit(count)); 747 } 748 749 /** 750 * 根据 condition 条件来查询数据列表 751 * 752 * @param tableName 表名 753 * @param condition 条件 754 * @param count 数据量 755 */ 756 public static List<Row> selectListByCondition(String tableName, QueryCondition condition, Long count) { 757 return invoker().selectListByQuery(null, tableName, new QueryWrapper().where(condition).limit(count)); 758 } 759 760 761 /** 762 * 通过 query 来查询数据列表 763 * 764 * @param schema 模式 765 * @param tableName 表名 766 * @param queryWrapper query 条件 767 */ 768 public static List<Row> selectListByQuery(String schema, String tableName, QueryWrapper queryWrapper) { 769 return invoker().selectListByQuery(schema, tableName, queryWrapper); 770 } 771 772 773 /** 774 * 通过 query 来查询数据列表 775 * 776 * @param tableName 表名 777 * @param queryWrapper query 条件 778 */ 779 public static List<Row> selectListByQuery(String tableName, QueryWrapper queryWrapper) { 780 return invoker().selectListByQuery(null, tableName, queryWrapper); 781 } 782 783 784 /** 785 * 通过 query 来查询数据列表 786 * 787 * @param queryWrapper 必须带有 from 的 queryWrapper 788 */ 789 public static List<Row> selectListByQuery(QueryWrapper queryWrapper) { 790 List<QueryTable> queryTables = CPI.getQueryTables(queryWrapper); 791 if (queryTables == null || queryTables.isEmpty()) { 792 throw FlexExceptions.wrap("table must not be null or empty in Db.selectListByQuery"); 793 } 794 return invoker().selectListByQuery(null, null, queryWrapper); 795 } 796 797 /** 798 * 查询某张表的所有数据 799 * 800 * @param schema 模式 801 * @param tableName 表名 802 */ 803 public static List<Row> selectAll(String schema, String tableName) { 804 return invoker().selectAll(schema, tableName); 805 } 806 807 /** 808 * 查询某张表的所有数据 809 * 810 * @param tableName 表名 811 */ 812 public static List<Row> selectAll(String tableName) { 813 return invoker().selectAll(null, tableName); 814 } 815 816 /** 817 * 查询某个内容,数据返回的应该只有 1 行 1 列 818 * 819 * @param sql sql 内容 820 * @param args sql 参数 821 */ 822 public static Object selectObject(String sql, Object... args) { 823 return invoker().selectObject(sql, args); 824 } 825 826 827 /** 828 * 根据 queryWrapper 查询内容,数据返回的应该只有 1 行 1 列 829 * 830 * @param schema 模式 831 * @param tableName 表名 832 * @param queryWrapper query 封装 833 * @return 数据内容 834 */ 835 public static Object selectObject(String schema, String tableName, QueryWrapper queryWrapper) { 836 return invoker().selectObjectByQuery(schema, tableName, queryWrapper); 837 } 838 839 /** 840 * 根据 queryWrapper 查询内容,数据返回的应该只有 1 行 1 列 841 * 842 * @param tableName 表名 843 * @param queryWrapper query 封装 844 * @return 数据内容 845 */ 846 public static Object selectObject(String tableName, QueryWrapper queryWrapper) { 847 return invoker().selectObjectByQuery(null, tableName, queryWrapper); 848 } 849 850 851 /** 852 * 根据 queryWrapper 查询内容,数据返回的应该只有 1 行 1 列 853 * 854 * @param queryWrapper query 封装 855 * @return 数据内容 856 */ 857 public static Object selectObject(QueryWrapper queryWrapper) { 858 return invoker().selectObjectByQuery(null, null, queryWrapper); 859 } 860 861 862 /** 863 * 查询某列内容,数据返回应该有 多行 1 列 864 * 865 * @param sql sql 内容 866 * @param args sql 参数 867 */ 868 public static List<Object> selectObjectList(String sql, Object... args) { 869 return invoker().selectObjectList(sql, args); 870 } 871 872 873 /** 874 * 根据 queryWrapper 查询内容,数据返回的应该只有 1 行 1 列 875 * 876 * @param schema 模式 877 * @param tableName 表名 878 * @param queryWrapper query 封装 879 * @return 数据内容 880 */ 881 public static List<Object> selectObjectList(String schema, String tableName, QueryWrapper queryWrapper) { 882 return invoker().selectObjectListByQuery(schema, tableName, queryWrapper); 883 } 884 885 /** 886 * 根据 queryWrapper 查询内容,数据返回的应该只有 1 行 1 列 887 * 888 * @param tableName 表名 889 * @param queryWrapper query 封装 890 * @return 数据内容 891 */ 892 public static List<Object> selectObjectList(String tableName, QueryWrapper queryWrapper) { 893 return invoker().selectObjectListByQuery(null, tableName, queryWrapper); 894 } 895 896 897 /** 898 * 根据 queryWrapper 查询内容,数据返回的应该只有 1 行 1 列 899 * 900 * @param queryWrapper query 封装 901 * @return 数据内容 902 */ 903 public static List<Object> selectObjectList(QueryWrapper queryWrapper) { 904 return invoker().selectObjectListByQuery(null, null, queryWrapper); 905 } 906 907 908 /** 909 * 查收 count 数据,一般用于 select count(*)... 910 * 或者返回的内容是一行1列,且是数值类型的也可以用此方法 911 * 912 * @param sql sql 内容 913 * @param args sql 参数 914 */ 915 public static long selectCount(String sql, Object... args) { 916 return invoker().selectCount(sql, args); 917 } 918 919 920 /** 921 * 根据 condition 条件来查询数量 922 * 923 * @param schema 模式 924 * @param tableName 表名 925 * @param condition 条件 926 */ 927 public static long selectCountByCondition(String schema, String tableName, QueryCondition condition) { 928 return invoker().selectCountByQuery(schema, tableName, new QueryWrapper().where(condition)); 929 } 930 931 /** 932 * 根据 condition 条件来查询数量 933 * 934 * @param tableName 表名 935 * @param condition 条件 936 */ 937 public static long selectCountByCondition(String tableName, QueryCondition condition) { 938 return invoker().selectCountByQuery(null, tableName, new QueryWrapper().where(condition)); 939 } 940 941 942 /** 943 * 根据 query 构建的条件来查询数据量 944 * 945 * @param schema 模式 946 * @param tableName 表名 947 * @param queryWrapper query 条件 948 */ 949 public static long selectCountByQuery(String schema, String tableName, QueryWrapper queryWrapper) { 950 return invoker().selectCountByQuery(schema, tableName, queryWrapper); 951 } 952 953 /** 954 * 根据 query 构建的条件来查询数据量 955 * 956 * @param tableName 表名 957 * @param queryWrapper query 条件 958 */ 959 public static long selectCountByQuery(String tableName, QueryWrapper queryWrapper) { 960 return invoker().selectCountByQuery(null, tableName, queryWrapper); 961 } 962 963 964 /** 965 * 直接根据 query 来查询数据量 966 * 967 * @param queryWrapper 必须带有表名的 queryWrapper 968 * @return 数据量 969 */ 970 public static long selectCountByQuery(QueryWrapper queryWrapper) { 971 List<QueryTable> queryTables = CPI.getQueryTables(queryWrapper); 972 if (queryTables == null || queryTables.isEmpty()) { 973 throw FlexExceptions.wrap("Query tables must not be null or empty in Db.selectCountByQuery"); 974 } 975 return invoker().selectCountByQuery(null, null, queryWrapper); 976 } 977 978 979 /** 980 * 分页查询 981 * 982 * @param schema 模式 983 * @param tableName 表名 984 * @param pageNumber 当前的页码 985 * @param pageSize 每页的数据量 986 * @param condition 条件 987 */ 988 public static Page<Row> paginate(String schema, String tableName, Number pageNumber, Number pageSize, QueryCondition condition) { 989 return invoker().paginate(schema, tableName, new Page<>(pageNumber, pageSize), QueryWrapper.create().where(condition)); 990 } 991 992 993 /** 994 * 分页查询 995 * 996 * @param tableName 表名 997 * @param pageNumber 当前的页码 998 * @param pageSize 每页的数据量 999 * @param condition 条件 1000 */ 1001 public static Page<Row> paginate(String tableName, Number pageNumber, Number pageSize, QueryCondition condition) { 1002 return invoker().paginate(null, tableName, new Page<>(pageNumber, pageSize), QueryWrapper.create().where(condition)); 1003 } 1004 1005 1006 /** 1007 * 分页查询 1008 * 1009 * @param schema 模式 1010 * @param tableName 表名 1011 * @param pageNumber 当前的页码 1012 * @param pageSize 每页的数据量 1013 * @param totalRow 数据总量 1014 * @param condition 条件 1015 */ 1016 public static Page<Row> paginate(String schema, String tableName, Number pageNumber, Number pageSize, Number totalRow, QueryCondition condition) { 1017 return invoker().paginate(schema, tableName, new Page<>(pageNumber, pageSize, totalRow), QueryWrapper.create().where(condition)); 1018 } 1019 1020 /** 1021 * 分页查询 1022 * 1023 * @param tableName 表名 1024 * @param pageNumber 当前的页码 1025 * @param pageSize 每页的数据量 1026 * @param totalRow 数据总量 1027 * @param condition 条件 1028 */ 1029 public static Page<Row> paginate(String tableName, Number pageNumber, Number pageSize, Number totalRow, QueryCondition condition) { 1030 return invoker().paginate(null, tableName, new Page<>(pageNumber, pageSize, totalRow), QueryWrapper.create().where(condition)); 1031 } 1032 1033 1034 /** 1035 * 分页查询 1036 * 1037 * @param schema 模式 1038 * @param tableName 表名 1039 * @param pageNumber 当前的页码 1040 * @param pageSize 每页的数据量 1041 * @param queryWrapper 条件 1042 */ 1043 public static Page<Row> paginate(String schema, String tableName, Number pageNumber, Number pageSize, QueryWrapper queryWrapper) { 1044 return invoker().paginate(schema, tableName, new Page<>(pageNumber, pageSize), queryWrapper); 1045 } 1046 1047 /** 1048 * 分页查询 1049 * 1050 * @param tableName 表名 1051 * @param pageNumber 当前的页码 1052 * @param pageSize 每页的数据量 1053 * @param queryWrapper 条件 1054 */ 1055 public static Page<Row> paginate(String tableName, Number pageNumber, Number pageSize, QueryWrapper queryWrapper) { 1056 return invoker().paginate(null, tableName, new Page<>(pageNumber, pageSize), queryWrapper); 1057 } 1058 1059 1060 /** 1061 * 分页查询 1062 * 1063 * @param schema 模式 1064 * @param tableName 表名 1065 * @param pageNumber 当前的页码 1066 * @param pageSize 每页的数据量 1067 * @param totalRow 数据总量 1068 * @param queryWrapper 条件 1069 */ 1070 public static Page<Row> paginate(String schema, String tableName, Number pageNumber, Number pageSize, Number totalRow, QueryWrapper queryWrapper) { 1071 return invoker().paginate(schema, tableName, new Page<>(pageNumber, pageSize, totalRow), queryWrapper); 1072 } 1073 1074 /** 1075 * 分页查询 1076 * 1077 * @param tableName 表名 1078 * @param pageNumber 当前的页码 1079 * @param pageSize 每页的数据量 1080 * @param totalRow 数据总量 1081 * @param queryWrapper 条件 1082 */ 1083 public static Page<Row> paginate(String tableName, Number pageNumber, Number pageSize, Number totalRow, QueryWrapper queryWrapper) { 1084 return invoker().paginate(null, tableName, new Page<>(pageNumber, pageSize, totalRow), queryWrapper); 1085 } 1086 1087 1088 /** 1089 * 分页查询 1090 * 1091 * @param schema 模式 1092 * @param tableName 表名 1093 * @param page page 对象,若 page 有 totalCount 值,则不会再去查询分类的数据总量 1094 * @param queryWrapper 条件 1095 */ 1096 public static Page<Row> paginate(String schema, String tableName, Page<Row> page, QueryWrapper queryWrapper) { 1097 return invoker().paginate(schema, tableName, page, queryWrapper); 1098 } 1099 1100 /** 1101 * 分页查询 1102 * 1103 * @param tableName 表名 1104 * @param page page 对象,若 page 有 totalCount 值,则不会再去查询分类的数据总量 1105 * @param queryWrapper 条件 1106 */ 1107 public static Page<Row> paginate(String tableName, Page<Row> page, QueryWrapper queryWrapper) { 1108 return invoker().paginate(null, tableName, page, queryWrapper); 1109 } 1110 1111 1112 /** 1113 * 进行事务操作,返回 null 或者 false 或者 抛出异常,事务回滚 1114 */ 1115 public static boolean tx(Supplier<Boolean> supplier) { 1116 return tx(supplier, Propagation.REQUIRED); 1117 } 1118 1119 /** 1120 * 进行事务操作,返回 null 或者 false 或者 抛出异常, 事务回滚 1121 */ 1122 public static boolean tx(Supplier<Boolean> supplier, Propagation propagation) { 1123 Boolean result = TransactionalManager.exec(supplier, propagation, false); 1124 return result != null && result; 1125 } 1126 1127 /** 1128 * 进行事务操作,和返回结果无关,只有抛出异常时,事务回滚 1129 */ 1130 public static <T> T txWithResult(Supplier<T> supplier) { 1131 return txWithResult(supplier, Propagation.REQUIRED); 1132 } 1133 1134 /** 1135 * 进行事务操作,和返回结果无关,只有抛出异常时,事务回滚 1136 */ 1137 public static <T> T txWithResult(Supplier<T> supplier, Propagation propagation) { 1138 return TransactionalManager.exec(supplier, propagation, true); 1139 } 1140 1141}