diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/BaseMapper.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/BaseMapper.java index 4413d60a..78d773b1 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/BaseMapper.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/BaseMapper.java @@ -29,6 +29,9 @@ import com.mybatisflex.core.util.*; import org.apache.ibatis.annotations.*; import org.apache.ibatis.builder.annotation.ProviderContext; import org.apache.ibatis.cursor.Cursor; +import org.apache.ibatis.session.ExecutorType; +import org.apache.ibatis.session.SqlSession; +import org.apache.ibatis.session.SqlSessionFactory; import java.io.Serializable; import java.util.*; @@ -364,6 +367,7 @@ public interface BaseMapper { /** * 执行类似 {@code update table set field = field + 1 where ... } 的场景。 * TODO: 2023/7/27 该方法将在 v1.6.0 被删除 + * * @param fieldName 字段名 * @param value 值(大于等于 0 加,小于 0 减) * @param queryWrapper 条件 @@ -377,6 +381,7 @@ public interface BaseMapper { /** * 执行类似 {@code update table set field = field + 1 where ... } 的场景。 * TODO: 该方法将在 v1.6.0 被删除 + * * @param column 字段名 * @param value 值(大于等于 0 加,小于 0 减) * @param queryWrapper 条件 @@ -392,6 +397,7 @@ public interface BaseMapper { /** * 执行类似 {@code update table set field = field + 1 where ... } 的场景。 * TODO: 该方法将在 v1.6.0 被删除 + * * @param fn 字段名 * @param value 值(大于等于 0 加,小于 0 减) * @param queryWrapper 条件 @@ -505,7 +511,8 @@ public interface BaseMapper { /** * 根据主表主键来查询 1 条数据。 - * @param id 表主键 + * + * @param id 表主键 * @param asType 接收数据类型 * @return 实体类数据 */ @@ -517,6 +524,7 @@ public interface BaseMapper { MappedStatementTypes.clear(); } } + /** * 根据查询条件来查询 1 条数据。 * @@ -1117,4 +1125,44 @@ public interface BaseMapper { return MapperUtil.doPaginate(this, page, queryWrapper, asType, true, consumers); } + + default Page xmlPaginate(String dataSelectId, Page page, QueryWrapper queryWrapper) { + return xmlPaginate(dataSelectId, dataSelectId + "_COUNT", page, queryWrapper, null); + } + + default Page xmlPaginate(String dataSelectId, Page page, Map otherParams) { + return xmlPaginate(dataSelectId, dataSelectId + "_COUNT", page, null, otherParams); + } + + default Page xmlPaginate(String dataSelectId, Page page, QueryWrapper queryWrapper, Map otherParams) { + return xmlPaginate(dataSelectId, dataSelectId + "_COUNT", page, queryWrapper, otherParams); + } + + default Page xmlPaginate(String dataSelectId, String countSelectId, Page page, QueryWrapper queryWrapper, Map otherParams) { + SqlSessionFactory sqlSessionFactory = FlexGlobalConfig.getDefaultConfig().getSqlSessionFactory(); + ExecutorType executorType = FlexGlobalConfig.getDefaultConfig().getConfiguration().getDefaultExecutorType(); + String mapperClassName = ClassUtil.getUsefulClass(this.getClass()).getName(); + + Map preparedParams = MapperUtil.preparedParams(page, queryWrapper, otherParams); + if (!dataSelectId.contains(".")) { + dataSelectId = mapperClassName + "." + dataSelectId; + } + + try (SqlSession sqlSession = sqlSessionFactory.openSession(executorType, false)) { + if (page.getTotalRow() < 0) { + if (!countSelectId.contains(".")) { + countSelectId = mapperClassName + "." + countSelectId; + } + Number number = sqlSession.selectOne(countSelectId, preparedParams); + page.setTotalRow(number); + } + + if (!page.isEmpty()) { + List entities = sqlSession.selectList(dataSelectId, preparedParams); + page.setRecords(entities); + } + } + return page; + } + } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/DbType.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/DbType.java index f876c03e..b32468a5 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/DbType.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/DbType.java @@ -196,5 +196,7 @@ public enum DbType { this.remarks = remarks; } - + public String getName() { + return name; + } } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/paginate/Page.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/paginate/Page.java index 8592e53e..567e043d 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/paginate/Page.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/paginate/Page.java @@ -193,7 +193,7 @@ public class Page implements Serializable { * @param pageSize 每页数据数量 */ public void setPageSize(Number pageSize) { - if (pageSize.longValue() < 0) { + if (pageSize == null || pageSize.longValue() < 0) { throw new IllegalArgumentException("pageSize must greater than or equal 0,current value is: " + pageSize); } this.pageSize = pageSize.longValue(); @@ -233,7 +233,7 @@ public class Page implements Serializable { * @param totalRow 数据总数 */ public void setTotalRow(Number totalRow) { - this.totalRow = totalRow.longValue(); + this.totalRow = totalRow == null ? INIT_VALUE : totalRow.longValue(); this.calcTotalPage(); } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/util/MapperUtil.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/util/MapperUtil.java index 6ea824cd..8cda6c6c 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/util/MapperUtil.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/util/MapperUtil.java @@ -16,7 +16,10 @@ package com.mybatisflex.core.util; import com.mybatisflex.core.BaseMapper; +import com.mybatisflex.core.FlexGlobalConfig; import com.mybatisflex.core.constant.SqlConsts; +import com.mybatisflex.core.dialect.DbType; +import com.mybatisflex.core.dialect.DialectFactory; import com.mybatisflex.core.exception.FlexExceptions; import com.mybatisflex.core.field.FieldQuery; import com.mybatisflex.core.field.FieldQueryBuilder; @@ -254,4 +257,60 @@ public class MapperUtil { } } + + public static Map preparedParams(Page page, QueryWrapper queryWrapper, Map params) { + Map newParams = new HashMap<>(); + + if (params != null) { + newParams.putAll(params); + } + + newParams.put("pageOffset", page.offset()); + newParams.put("pageNumber", page.getPageNumber()); + newParams.put("pageSize", page.getPageSize()); + + DbType dbType = DialectFactory.getHintDbType(); + newParams.put("dbType", dbType != null ? dbType : FlexGlobalConfig.getDefaultConfig().getDbType()); + + if (queryWrapper != null) { + preparedQueryWrapper(newParams, queryWrapper); + } + + return newParams; + } + + + private static void preparedQueryWrapper(Map params, QueryWrapper queryWrapper) { + String sql = DialectFactory.getDialect().buildNoSelectSql(queryWrapper); + StringBuilder sqlBuilder = new StringBuilder(); + char quote = 0; + int index = 0; + for (int i = 0; i < sql.length(); ++i) { + char ch = sql.charAt(i); + if (ch == '\'') { + if (quote == 0) { + quote = ch; + } else if (quote == '\'') { + quote = 0; + } + } else if (ch == '"') { + if (quote == 0) { + quote = ch; + } else if (quote == '"') { + quote = 0; + } + } + if (quote == 0 && ch == '?') { + sqlBuilder.append("#{qwParams_").append(index++).append("}"); + } else { + sqlBuilder.append(ch); + } + } + params.put("qwSql", sqlBuilder.toString()); + Object[] valueArray = CPI.getValueArray(queryWrapper); + for (int i = 0; i < valueArray.length; i++) { + params.put("qwParams_" + i, valueArray[i]); + } + } + } diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/controller/AccountController.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/controller/AccountController.java index 159b1d5b..ec520abf 100644 --- a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/controller/AccountController.java +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/controller/AccountController.java @@ -56,8 +56,15 @@ public class AccountController { @GetMapping("/account/byName/{name}") - AccountDto selectName(@PathVariable("name") String name) { - return myAccountMapper.selectByName(name); + Page selectName(@PathVariable("name") String name) { +// return myAccountMapper.selectByName(name); + + QueryWrapper qw = QueryWrapper.create() +// .where(Account::getAge).eq("18") + .and(Account::getId).ge(0); + + Page accountPage = myAccountMapper.xmlPaginate("selectByName", Page.of(1, 10), qw); + return accountPage; } @@ -106,7 +113,6 @@ public class AccountController { } - @GetMapping("/ds") @UseDataSource("ds2222") public String ds() { diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/mapper/MyAccountMapper.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/mapper/MyAccountMapper.java index d05038eb..7d867b01 100644 --- a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/mapper/MyAccountMapper.java +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/mapper/MyAccountMapper.java @@ -17,14 +17,13 @@ package com.mybatisflex.test.mapper; import com.mybatisflex.test.model.Account; -import com.mybatisflex.test.model.AccountDto; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; public interface MyAccountMapper extends AccountMapper { - AccountDto selectByName(@Param("name") String name); +// AccountDto selectByName(@Param("name") String name); @Select("select * from tb_account where id = #{id} and id =#{id}") Account selectById(@Param("id") Object id); diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/application.yml b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/application.yml index 92988471..36c7ddb6 100644 --- a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/application.yml +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/application.yml @@ -7,7 +7,7 @@ spring: # driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/flex_test username: root - password: 12345678 + password: 123456 # driver-class-name: # datasource: # driver-class-name: org.h2.Driver diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/mapper/accountMapper.xml b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/mapper/accountMapper.xml index 26631fae..43229d97 100644 --- a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/mapper/accountMapper.xml +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/mapper/accountMapper.xml @@ -19,7 +19,11 @@ + +