diff --git a/mybatis-jpa-extra-core/src/main/java/org/apache/mybatis/jpa/persistence/provider/SqlProviderQuery.java b/mybatis-jpa-extra-core/src/main/java/org/apache/mybatis/jpa/persistence/provider/SqlProviderQuery.java index 7ff298ed8cc23485223c6c237ce801519a4ddc94..c68a3f938fc7f3add8e80273a400ddb9c8fab987 100644 --- a/mybatis-jpa-extra-core/src/main/java/org/apache/mybatis/jpa/persistence/provider/SqlProviderQuery.java +++ b/mybatis-jpa-extra-core/src/main/java/org/apache/mybatis/jpa/persistence/provider/SqlProviderQuery.java @@ -106,10 +106,12 @@ public class SqlProviderQuery { JpaPagination pagination=(JpaPagination)entity; //获取缓存数据 PageResultsSqlCache pageResultsSqlCache=JpaBaseService.pageResultsBoundSqlCache.get(pagination.getPageResultSelectUUID()); - String selectSql=pageResultsSqlCache.getSql(); + //多个空格 tab 替换成1个空格 + String selectSql=pageResultsSqlCache.getSql().replaceAll("\t", " ").replaceAll(" +"," "); BoundSql boundSql=(BoundSql)pageResultsSqlCache.getBoundSql(); + _logger.trace("Count original SQL :\n" + selectSql); - StringBuffer sql=new StringBuffer(); + StringBuffer sql=new StringBuffer(SqlSyntax.SELECT +" "+ SqlSyntax.Functions.COUNT_ONE +" countrows_ "); StringBuffer countSql=new StringBuffer(); if(boundSql.getParameterMappings()==null ||boundSql.getParameterMappings().isEmpty()) { @@ -122,13 +124,27 @@ public class SqlProviderQuery { } countSql.append(selectSql); } + String countSqlLowerCase = countSql.toString().toLowerCase(); + _logger.trace("Count SQL LowerCase :\n" + countSqlLowerCase); - if(countSql.toString().toLowerCase().indexOf("distinct")>0) { - sql.append("select count(1) countrows_ from (").append(countSql).append(" ) count_table_"); + if(countSqlLowerCase.indexOf(SqlSyntax.DISTINCT + " ")>0 //去重 + ||countSqlLowerCase.indexOf(" " + SqlSyntax.GROUPBY + " ")>0 //分组 + ||countSqlLowerCase.indexOf(" " + SqlSyntax.HAVING + " ")>0 //聚合函数 + ||(countSqlLowerCase.indexOf(" " + SqlSyntax.FROM + " ") + != countSqlLowerCase.lastIndexOf(" " + SqlSyntax.FROM + " ") + ) //嵌套 + ) { + _logger.trace("Count SQL Complex "); + sql.append(SqlSyntax.FROM).append(" (").append(countSql).append(" ) count_table_"); }else { - sql.append("select count(1) countrows_ ").append( - countSql.substring(countSql.toString().toLowerCase().indexOf("from")) - ); + int fromIndex = countSqlLowerCase.indexOf(" " + SqlSyntax.FROM + " "); + int orderByIndex = countSqlLowerCase.indexOf(" " + SqlSyntax.ORDERBY + " "); + _logger.trace("Count SQL from Index "+ fromIndex +" , order by " +orderByIndex); + if(orderByIndex > -1) { + sql.append(countSql.substring(fromIndex,orderByIndex)); + }else { + sql.append(countSql.substring(fromIndex)); + } } //删除缓存 JpaBaseService.pageResultsBoundSqlCache.remove(pagination.getPageResultSelectUUID()); diff --git a/mybatis-jpa-extra-core/src/main/java/org/apache/mybatis/jpa/persistence/provider/SqlSyntax.java b/mybatis-jpa-extra-core/src/main/java/org/apache/mybatis/jpa/persistence/provider/SqlSyntax.java new file mode 100644 index 0000000000000000000000000000000000000000..57ce781f4ccad41cdcca2d6df2552a0ac23878a9 --- /dev/null +++ b/mybatis-jpa-extra-core/src/main/java/org/apache/mybatis/jpa/persistence/provider/SqlSyntax.java @@ -0,0 +1,27 @@ +package org.apache.mybatis.jpa.persistence.provider; + +public class SqlSyntax { + + public final static String SELECT = "select"; + + public final static String DISTINCT = "distinct"; + + public final static String FROM = "from"; + + public final static String GROUPBY = "group by"; + + public final static String ORDERBY = "order by"; + + public final static String HAVING = "having"; + + public class Functions{ + + public static final String COUNT_ALL = "count(*)"; + + public static final String COUNT_ONE = "count(1)"; + } + + + + +} diff --git a/mybatis-jpa-extra-core/src/test/resources/config/applicationConfig.properties b/mybatis-jpa-extra-core/src/test/resources/config/applicationConfig.properties index 7e033c9c21e6445b20226842b4d51b7ab3d81c81..2364c63798de6635fa35e5e580ef29f994513081 100644 --- a/mybatis-jpa-extra-core/src/test/resources/config/applicationConfig.properties +++ b/mybatis-jpa-extra-core/src/test/resources/config/applicationConfig.properties @@ -21,15 +21,15 @@ config.datasource.database=postgresql # for SyBase jdbc:sybase:Tds:hostname:port/secdb # for Derby jdbc:derby://localhost:1527/secdb # -config.datasource.driverclass=org.postgresql.Driver -config.datasource.url=jdbc:postgresql://localhost:5432/postgres -config.datasource.username=postgres -config.datasource.password=postgresql +#config.datasource.driverclass=org.postgresql.Driver +#config.datasource.url=jdbc:postgresql://localhost:5432/postgres +#config.datasource.username=postgres +#config.datasource.password=postgresql -#config.datasource.driverclass=com.mysql.jdbc.Driver -#config.datasource.url=jdbc:mysql://localhost/test?autoReconnect=false&characterEncoding=UTF-8&serverTimezone=UTC -#config.datasource.username=root -#config.datasource.password=maxkey +config.datasource.driverclass=com.mysql.jdbc.Driver +config.datasource.url=jdbc:mysql://localhost/test?autoReconnect=false&characterEncoding=UTF-8&serverTimezone=UTC +config.datasource.username=root +config.datasource.password=maxkey ############################################################################ \ No newline at end of file diff --git a/mybatis-jpa-extra-core/src/test/resources/org/apache/mybatis/jpa/test/dao/persistence/xml/mysql/StudentsMapper.xml b/mybatis-jpa-extra-core/src/test/resources/org/apache/mybatis/jpa/test/dao/persistence/xml/mysql/StudentsMapper.xml index ca6ad1d4ed29357ab9884f77985b70d96a176362..bdc107cc5d47c168f2a7ad3e7520d36477ae0b31 100644 --- a/mybatis-jpa-extra-core/src/test/resources/org/apache/mybatis/jpa/test/dao/persistence/xml/mysql/StudentsMapper.xml +++ b/mybatis-jpa-extra-core/src/test/resources/org/apache/mybatis/jpa/test/dao/persistence/xml/mysql/StudentsMapper.xml @@ -30,6 +30,8 @@ FROM STUDENTS + + order by STDAGE diff --git a/mybatis-jpa-extra-core/src/test/resources/spring/applicationContext.xml b/mybatis-jpa-extra-core/src/test/resources/spring/applicationContext.xml index 448427d8a79f9e0cd0fc7dbe7fe66181ef91dffe..e80b87e4af65fa2a92fe9b87feb31f41abfa1b18 100644 --- a/mybatis-jpa-extra-core/src/test/resources/spring/applicationContext.xml +++ b/mybatis-jpa-extra-core/src/test/resources/spring/applicationContext.xml @@ -72,9 +72,9 @@ - + - +