Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
gjjs-bd-common
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
s_guodong
gjjs-bd-common
Commits
f2b9429f
Commit
f2b9429f
authored
Aug 24, 2023
by
s_guodong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
多表分页问题
parent
8be5ce1f
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
45 additions
and
43 deletions
+45
-43
MybatisArgumentAdapter.java
...rilliance/mda/support/mybatis/MybatisArgumentAdapter.java
+45
-43
No files found.
gjjs-bd-mybatis-support/src/main/java/com/brilliance/mda/support/mybatis/MybatisArgumentAdapter.java
View file @
f2b9429f
...
...
@@ -91,7 +91,7 @@ public class MybatisArgumentAdapter {
this
.
init
();
}
public
MybatisArgumentAdapter
(
AdaType
type
,
List
<
Class
<?
extends
IModule
>>
clazzList
,
String
whereSql
,
Object
[]
params
,
String
selectTable
)
{
public
MybatisArgumentAdapter
(
AdaType
type
,
List
<
Class
<?
extends
IModule
>>
clazzList
,
String
whereSql
,
Object
[]
params
,
String
selectTable
)
{
this
.
clazzList
=
clazzList
;
this
.
pattern
=
type
.
getValue
();
this
.
whereSql
=
whereSql
;
...
...
@@ -168,66 +168,68 @@ public class MybatisArgumentAdapter {
String
sql
=
""
;
if
(
join
)
{
sql
=
String
.
format
(
pattern
,
conditions
);
//如果是多表查询
if
(
null
!=
clazzList
&&
clazzList
.
size
()>
1
)
{
StringBuffer
selectColumn
=
new
StringBuffer
();
//获取SQL中别名和表明的对应关系
Map
<
String
,
String
>
tableNameMap
=
parseTableNameAndAlias
(
String
.
format
(
pattern
,
whereSql
));
for
(
Class
<?
extends
IModule
>
clazz
:
clazzList
)
{
//根据模型注解中的@Table 获取当前模型对应的表名
String
tableNam
=
clazz
.
getAnnotation
(
Table
.
class
).
value
();
//根据表明获取表别名
String
aliasName
=
tableNameMap
.
get
(
tableNam
);
//当前模型属性
Field
[]
fields
=
clazz
.
getDeclaredFields
();
//遍历模型中的属性 ,拼接SQL中 SELECT 列部分的内容
//拼接规则 别名.属性名(字段名) 表名_属性名(字段名)
//后续对结果集进行解析 根据表名和属性名解析
for
(
Field
field
:
fields
)
{
//@Ignore 的字段不做拼接
if
(
MdaUtils
.
isEmpty
(
field
.
getAnnotation
(
Ignore
.
class
)))
{
if
(!
MdaUtils
.
isEmpty
(
selectColumn
))
{
selectColumn
.
append
(
","
);
}
selectColumn
.
append
(
" "
);
selectColumn
.
append
(
aliasName
).
append
(
"."
).
append
(
field
.
getName
());
selectColumn
.
append
(
" "
);
selectColumn
.
append
(
tableNam
).
append
(
"_"
).
append
(
field
.
getName
());
}
else
{
sql
=
String
.
format
(
pattern
,
tableName
,
conditions
);
}
//如果是多表查询
if
(
null
!=
clazzList
&&
clazzList
.
size
()
>
1
)
{
StringBuffer
selectColumn
=
new
StringBuffer
();
//获取SQL中别名和表明的对应关系
Map
<
String
,
String
>
tableNameMap
=
parseTableNameAndAlias
(
sql
);
for
(
Class
<?
extends
IModule
>
clazz
:
clazzList
)
{
//根据模型注解中的@Table 获取当前模型对应的表名
String
tableNam
=
clazz
.
getAnnotation
(
Table
.
class
).
value
();
//根据表明获取表别名
String
aliasName
=
tableNameMap
.
get
(
tableNam
);
//当前模型属性
Field
[]
fields
=
clazz
.
getDeclaredFields
();
//遍历模型中的属性 ,拼接SQL中 SELECT 列部分的内容
//拼接规则 别名.属性名(字段名) 表名_属性名(字段名)
//后续对结果集进行解析 根据表名和属性名解析
for
(
Field
field
:
fields
)
{
//@Ignore 的字段不做拼接
if
(
MdaUtils
.
isEmpty
(
field
.
getAnnotation
(
Ignore
.
class
)))
{
if
(!
MdaUtils
.
isEmpty
(
selectColumn
))
{
selectColumn
.
append
(
","
);
}
selectColumn
.
append
(
" "
);
selectColumn
.
append
(
aliasName
).
append
(
"."
).
append
(
field
.
getName
());
selectColumn
.
append
(
" "
);
selectColumn
.
append
(
tableNam
).
append
(
"_"
).
append
(
field
.
getName
());
}
}
//拼接完成之后替换 “*”
sql
=
sql
.
replace
(
"*"
,
selectColumn
.
toString
());
}
}
else
{
sql
=
String
.
format
(
pattern
,
tableName
,
conditions
);
//拼接完成之后替换 “*”
sql
=
sql
.
replace
(
"*"
,
selectColumn
.
toString
()
);
}
this
.
sqlTemplate
=
sql
;
this
.
sqlParams
.
put
(
HOLDER_SQL
,
this
.
sqlTemplate
);
this
.
sqlParams
.
put
(
HOLDER_CONDS
,
conditions
);
}
// 获取表及对应的别名
public
Map
<
String
,
String
>
parseTableNameAndAlias
(
String
sql
)
{
Map
<
String
,
String
>
map
=
new
HashMap
<>();
public
Map
<
String
,
String
>
parseTableNameAndAlias
(
String
sql
)
{
Map
<
String
,
String
>
map
=
new
HashMap
<>();
try
{
Select
select
=
(
Select
)
CCJSqlParserUtil
.
parse
(
sql
);
SelectBody
selectBody
=
select
.
getSelectBody
();
PlainSelect
plainSelect
=
(
PlainSelect
)
selectBody
;
net
.
sf
.
jsqlparser
.
schema
.
Table
table
=
(
net
.
sf
.
jsqlparser
.
schema
.
Table
)
plainSelect
.
getFromItem
();
if
(
table
.
getAlias
()
!=
null
)
{
map
.
put
(
table
.
getName
(),
table
.
getAlias
().
getName
());
PlainSelect
plainSelect
=
(
PlainSelect
)
selectBody
;
net
.
sf
.
jsqlparser
.
schema
.
Table
table
=
(
net
.
sf
.
jsqlparser
.
schema
.
Table
)
plainSelect
.
getFromItem
();
if
(
table
.
getAlias
()
!=
null
)
{
map
.
put
(
table
.
getName
(),
table
.
getAlias
().
getName
());
}
for
(
Join
join
:
plainSelect
.
getJoins
())
{
net
.
sf
.
jsqlparser
.
schema
.
Table
table1
=
(
net
.
sf
.
jsqlparser
.
schema
.
Table
)
join
.
getRightItem
();
if
(
table1
.
getAlias
()!=
null
)
{
map
.
put
(
table1
.
getName
(),
table1
.
getAlias
().
getName
());
for
(
Join
join
:
plainSelect
.
getJoins
())
{
net
.
sf
.
jsqlparser
.
schema
.
Table
table1
=
(
net
.
sf
.
jsqlparser
.
schema
.
Table
)
join
.
getRightItem
();
if
(
table1
.
getAlias
()
!=
null
)
{
map
.
put
(
table1
.
getName
(),
table1
.
getAlias
().
getName
());
}
}
}
catch
(
Exception
e
)
{
log
.
error
(
"解析SQL:/n{} ---------error"
,
sql
);
throw
new
RuleExecuteException
(
"parseTableNameAndAlias:解析SQL时出错"
,
e
);
}
catch
(
Exception
e
)
{
log
.
error
(
"解析SQL:/n{} ---------error"
,
sql
);
throw
new
RuleExecuteException
(
"parseTableNameAndAlias:解析SQL时出错"
,
e
);
}
return
map
;
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment