From 9ca850eaaed2a0eee1d87e2fe0e06fc16900096b Mon Sep 17 00:00:00 2001 From: hejian <405705277@qq.com> Date: Wed, 15 Feb 2023 10:56:16 +0800 Subject: [PATCH 1/3] =?UTF-8?q?RefMapper=20=E5=A2=9E=E5=8A=A0=E9=BB=98?= =?UTF-8?q?=E8=AE=A4xml=E8=B7=AF=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/net/hasor/dbvisitor/dal/repository/DalRegistry.java | 5 +++++ .../java/net/hasor/dbvisitor/dal/repository/RefMapper.java | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/dbvisitor/src/main/java/net/hasor/dbvisitor/dal/repository/DalRegistry.java b/dbvisitor/src/main/java/net/hasor/dbvisitor/dal/repository/DalRegistry.java index cad68e34..fecde32e 100644 --- a/dbvisitor/src/main/java/net/hasor/dbvisitor/dal/repository/DalRegistry.java +++ b/dbvisitor/src/main/java/net/hasor/dbvisitor/dal/repository/DalRegistry.java @@ -206,6 +206,11 @@ public class DalRegistry extends MappingRegistry { RefMapper refMapper = refRepository.getAnnotation(RefMapper.class); if (refMapper != null) { String resource = refMapper.value(); + + if (StringUtils.isBlank(resource)) { + resource = refRepository.getName().replace('.', '/') + ".xml"; + } + if (resource.startsWith("/")) { resource = resource.substring(1); } diff --git a/dbvisitor/src/main/java/net/hasor/dbvisitor/dal/repository/RefMapper.java b/dbvisitor/src/main/java/net/hasor/dbvisitor/dal/repository/RefMapper.java index 4910a8b3..9a16b142 100644 --- a/dbvisitor/src/main/java/net/hasor/dbvisitor/dal/repository/RefMapper.java +++ b/dbvisitor/src/main/java/net/hasor/dbvisitor/dal/repository/RefMapper.java @@ -26,5 +26,5 @@ import java.lang.annotation.*; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface RefMapper { - String value(); + String value() default ""; } -- Gitee From 7aff0dd71085c1ee9cfab2d0a222cbfdfc6e6ad6 Mon Sep 17 00:00:00 2001 From: hejian <405705277@qq.com> Date: Wed, 15 Feb 2023 10:59:54 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20Page=20=E6=96=B9?= =?UTF-8?q?=E6=B3=95=EF=BC=8C=E5=8F=AF=E4=BB=A5=E8=AE=BE=E7=BD=AE=E6=80=BB?= =?UTF-8?q?=E8=AE=B0=E5=BD=95=E6=95=B0=E3=80=81=E6=98=AF=E5=90=A6=E8=BF=94?= =?UTF-8?q?=E5=9B=9E=E6=80=BB=E8=AE=B0=E5=BD=95=E6=95=B0=E7=AD=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dal/execute/PreparedStatementExecute.java | 12 +++++++++++- .../java/net/hasor/dbvisitor/page/Page.java | 9 +++++++++ .../net/hasor/dbvisitor/page/PageObject.java | 18 ++++++++++++++++-- 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/dbvisitor/src/main/java/net/hasor/dbvisitor/dal/execute/PreparedStatementExecute.java b/dbvisitor/src/main/java/net/hasor/dbvisitor/dal/execute/PreparedStatementExecute.java index f65d8984..a5b0518f 100644 --- a/dbvisitor/src/main/java/net/hasor/dbvisitor/dal/execute/PreparedStatementExecute.java +++ b/dbvisitor/src/main/java/net/hasor/dbvisitor/dal/execute/PreparedStatementExecute.java @@ -60,7 +60,7 @@ public class PreparedStatementExecute extends AbstractStatementExecute { long position = executeInfo.pageInfo.getFirstRecordPosition(); long pageSize = executeInfo.pageInfo.getPageSize(); boundSql = dialect.pageSql(sqlBuilder, position, pageSize); - if (executeInfo.pageResult) { + if (executeInfo.pageResult || executeInfo.pageInfo.isCountTotalRows()) { countSql = dialect.countSql(sqlBuilder); } } @@ -86,6 +86,16 @@ public class PreparedStatementExecute extends AbstractStatementExecute { throw e; } + if (usingPage(executeInfo)) { + if (executeInfo.pageInfo.isCountTotalRows()) { + executeInfo.pageInfo.setTotalCount(resultCount); + } + + if (!executeInfo.pageResult) { + return resultData; + } + } + PageResult pageResult = new PageResult<>(executeInfo.pageInfo, resultCount); pageResult.setData((resultData instanceof List) ? (List) resultData : Collections.singletonList(resultData)); return pageResult; diff --git a/dbvisitor/src/main/java/net/hasor/dbvisitor/page/Page.java b/dbvisitor/src/main/java/net/hasor/dbvisitor/page/Page.java index 0c066f80..9f6128a4 100644 --- a/dbvisitor/src/main/java/net/hasor/dbvisitor/page/Page.java +++ b/dbvisitor/src/main/java/net/hasor/dbvisitor/page/Page.java @@ -51,6 +51,15 @@ public interface Page { /** 获取记录总数 */ long getTotalCount() throws SQLException; + /** 设置记录总数 */ + void setTotalCount(long totalCount); + + /** 是否返回总记录数 */ + boolean isCountTotalRows(); + + /** 设置是否返回总记录数 */ + void setCountTotalRows(boolean isCountTotalRows); + /** 移动到第一页 */ default void firstPage() { setCurrentPage(0); diff --git a/dbvisitor/src/main/java/net/hasor/dbvisitor/page/PageObject.java b/dbvisitor/src/main/java/net/hasor/dbvisitor/page/PageObject.java index 7cf6808d..94691fc2 100644 --- a/dbvisitor/src/main/java/net/hasor/dbvisitor/page/PageObject.java +++ b/dbvisitor/src/main/java/net/hasor/dbvisitor/page/PageObject.java @@ -36,6 +36,16 @@ public class PageObject implements Page { private long currentPage = 0; /** 起始页码的偏移量 */ private long pageNumberOffset = 0; + /** 是否返回总记录数 */ + private boolean isCountTotalRows = false; + + public boolean isCountTotalRows() { + return isCountTotalRows; + } + + public void setCountTotalRows(boolean isCountTotalRows) { + this.isCountTotalRows = isCountTotalRows; + } public PageObject() { this.totalCountSupplier = () -> 0L; @@ -43,8 +53,7 @@ public class PageObject implements Page { public PageObject(long pageSize, long totalCount) { this.pageSize = pageSize; - this.totalCount = totalCount; - this.totalCountInited.set(true); + this.setTotalCount(totalCount); } public PageObject(long pageSize, ESupplier totalCountSupplier) { @@ -127,4 +136,9 @@ public class PageObject implements Page { } return this.totalCount; } + + public void setTotalCount(long totalCount) { + this.totalCount = totalCount; + this.totalCountInited.set(true); + } } -- Gitee From 6d324332f304e0326455c523876a63cd5dd91ef6 Mon Sep 17 00:00:00 2001 From: hejian <405705277@qq.com> Date: Wed, 15 Feb 2023 14:54:40 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E7=A9=BA=E6=8C=87=E9=92=88=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E4=BF=AE=E5=A4=8D=EF=BC=8C=E5=A2=9E=E5=8A=A0=20result?= =?UTF-8?q?Type=20=E5=AF=B9=20Map=20=E5=AD=90=E7=B1=BB=E5=AF=B9=E8=B1=A1?= =?UTF-8?q?=E7=9A=84=E6=94=AF=E6=8C=81=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../resolve/ClassTableMappingResolve.java | 49 ++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/dbvisitor/src/main/java/net/hasor/dbvisitor/mapping/resolve/ClassTableMappingResolve.java b/dbvisitor/src/main/java/net/hasor/dbvisitor/mapping/resolve/ClassTableMappingResolve.java index c8bdc20b..10324f2b 100644 --- a/dbvisitor/src/main/java/net/hasor/dbvisitor/mapping/resolve/ClassTableMappingResolve.java +++ b/dbvisitor/src/main/java/net/hasor/dbvisitor/mapping/resolve/ClassTableMappingResolve.java @@ -60,7 +60,54 @@ public class ClassTableMappingResolve extends AbstractTableMappingResolve resolveTableMapping(Class entityType, ClassLoader classLoader, TypeHandlerRegistry typeRegistry) { Table tableInfo = fetchDefaultInfoByEntity(classLoader, entityType, this.options, Collections.emptyMap()); - return resolveTableAndColumn(tableInfo, entityType, typeRegistry); + if (Map.class.isAssignableFrom(entityType)) { + return resolveTableAndColumnToMap(tableInfo, entityType, typeRegistry); + } else { + return resolveTableAndColumn(tableInfo, entityType, typeRegistry); + } + } + + protected TableDef resolveTableAndColumnToMap(Table tableInfo, Class entityType, TypeHandlerRegistry typeRegistry) { + String table = StringUtils.isNotBlank(tableInfo.table()) ? tableInfo.table() : StringUtils.isNotBlank(tableInfo.value()) ? tableInfo.value() : ""; + SqlDialect dialect = (tableInfo instanceof TableDefaultInfo) ? ((TableDefaultInfo) tableInfo).getSqlDialect() : DefaultSqlDialect.DEFAULT; + TableDef tableDef = new TableDef( + tableInfo.catalog(), + tableInfo.schema(), + table, + entityType, + tableInfo.autoMapping(), + tableInfo.useDelimited(), + tableInfo.caseInsensitive(), + tableInfo.mapUnderscoreToCamelCase(), + dialect) { + + @Override + public ColumnMapping getPropertyByColumn(String column) { + Class javaType = Object.class; + int jdbcType = TypeHandlerRegistry.toSqlType(javaType); + TypeHandler typeHandler = typeRegistry.getTypeHandler(javaType, jdbcType); + Property handler = new Property() { + @Override + public boolean isReadOnly() { + return false; + } + + @Override + public Object get(Object instance) { + return ((Map) instance).get(column); + } + + @Override + public void set(Object instance, Object value) { + ((Map) instance).put(column, value); + } + }; + return new ColumnDef(column, column, jdbcType, javaType, typeHandler, handler, true, true, false); + } + }; + + tableDef.setDescription(parseDesc(entityType.getAnnotation(TableDescribe.class))); + return tableDef; } protected TableDef resolveTable(Table tableInfo, Class entityType) { -- Gitee