Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MaxKey单点登录官方(MaxKeyTop)
Mybatis Jpa Extra
提交
bd091bff
M
Mybatis Jpa Extra
项目概览
MaxKey单点登录官方(MaxKeyTop)
/
Mybatis Jpa Extra
8 个月 前同步成功
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
Mybatis Jpa Extra
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
bd091bff
编写于
5月 16, 2021
作者:
M
MaxKey
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix query count
fix query count
上级
d8ed513b
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
62 addition
and
17 deletion
+62
-17
mybatis-jpa-extra-core/src/main/java/org/apache/mybatis/jpa/persistence/provider/SqlProviderQuery.java
...he/mybatis/jpa/persistence/provider/SqlProviderQuery.java
+23
-7
mybatis-jpa-extra-core/src/main/java/org/apache/mybatis/jpa/persistence/provider/SqlSyntax.java
...rg/apache/mybatis/jpa/persistence/provider/SqlSyntax.java
+27
-0
mybatis-jpa-extra-core/src/test/resources/config/applicationConfig.properties
...re/src/test/resources/config/applicationConfig.properties
+8
-8
mybatis-jpa-extra-core/src/test/resources/org/apache/mybatis/jpa/test/dao/persistence/xml/mysql/StudentsMapper.xml
...tis/jpa/test/dao/persistence/xml/mysql/StudentsMapper.xml
+2
-0
mybatis-jpa-extra-core/src/test/resources/spring/applicationContext.xml
...tra-core/src/test/resources/spring/applicationContext.xml
+2
-2
未找到文件。
mybatis-jpa-extra-core/src/main/java/org/apache/mybatis/jpa/persistence/provider/SqlProviderQuery.java
浏览文件 @
bd091bff
...
...
@@ -106,10 +106,12 @@ public class SqlProviderQuery <T extends JpaBaseEntity>{
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 <T extends JpaBaseEntity>{
}
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
());
...
...
mybatis-jpa-extra-core/src/main/java/org/apache/mybatis/jpa/persistence/provider/SqlSyntax.java
0 → 100644
浏览文件 @
bd091bff
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)"
;
}
}
mybatis-jpa-extra-core/src/test/resources/config/applicationConfig.properties
浏览文件 @
bd091bff
...
...
@@ -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
mybatis-jpa-extra-core/src/test/resources/org/apache/mybatis/jpa/test/dao/persistence/xml/mysql/StudentsMapper.xml
浏览文件 @
bd091bff
...
...
@@ -30,6 +30,8 @@
FROM STUDENTS
<include
refid=
"sql_condition"
/>
<!-- group by STDMAJOR -->
order by STDAGE
</select>
...
...
mybatis-jpa-extra-core/src/test/resources/spring/applicationContext.xml
浏览文件 @
bd091bff
...
...
@@ -72,9 +72,9 @@
<!--<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">-->
<bean
id=
"sqlSessionFactory"
class=
"org.apache.mybatis.jpa.MyBatisSessionFactoryBean"
>
<property
name=
"timeout"
value=
"30"
/>
<property
name=
"dialect"
value=
"
postgre
sql"
/>
<property
name=
"dialect"
value=
"
my
sql"
/>
<property
name=
"dataSource"
ref=
"dataSource"
/>
<property
name=
"mapperLocations"
value=
"classpath*:/org/apache/mybatis/jpa/test/dao/persistence/xml/
postgre
sql/*.xml"
/>
<property
name=
"mapperLocations"
value=
"classpath*:/org/apache/mybatis/jpa/test/dao/persistence/xml/
my
sql/*.xml"
/>
<property
name=
"typeAliasesPackage"
value=
"
org.apache.mybatis.jpa.test.entity,
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录