Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
T
test
Project
Project
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
邬友楠
test
Commits
452da19b
Commit
452da19b
authored
Sep 04, 2020
by
雍欢
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
DefaultSqlTranslator不缓存解析生成的SQL
parent
bfb705b0
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
2 additions
and
15 deletions
+2
-15
DefaultSqlTranslator.java
...java/com/topsunit/query/binding/DefaultSqlTranslator.java
+2
-15
No files found.
query-spring/src/main/java/com/topsunit/query/binding/DefaultSqlTranslator.java
View file @
452da19b
...
...
@@ -6,9 +6,7 @@ import java.lang.reflect.Method;
import
java.lang.reflect.Parameter
;
import
java.util.Arrays
;
import
java.util.Collection
;
import
java.util.Map
;
import
java.util.Objects
;
import
java.util.concurrent.ConcurrentHashMap
;
import
java.util.stream.Collectors
;
/**
...
...
@@ -16,21 +14,8 @@ import java.util.stream.Collectors;
*/
public
class
DefaultSqlTranslator
implements
SqlTranslator
{
private
final
Map
<
String
,
String
>
sqlCache
=
new
ConcurrentHashMap
<>(
200
);
@Override
public
String
translate
(
String
sql
,
Method
method
,
Object
[]
args
)
{
final
String
cacheKey
=
method
.
getDeclaringClass
().
getName
()
+
"."
+
method
.
getName
();
String
cachedSql
=
sqlCache
.
get
(
cacheKey
);
if
(
cachedSql
!=
null
)
{
return
cachedSql
;
}
sql
=
translateInternal
(
sql
,
method
,
args
);
sqlCache
.
put
(
cacheKey
,
sql
);
return
sql
;
}
private
String
translateInternal
(
String
sql
,
Method
method
,
Object
[]
args
)
{
if
(
useNamedParameter
(
method
,
args
))
{
Parameter
[]
parameters
=
method
.
getParameters
();
for
(
int
i
=
0
;
i
<
parameters
.
length
;
i
++)
{
...
...
@@ -48,6 +33,8 @@ public class DefaultSqlTranslator implements SqlTranslator {
}
}
}
// 不要试图缓存解析生成SQL,即使是同一个源SQL,不同的参数(args)也会解析生成不同的SQL
// 例如有如下SQL:select t.id,t.name from t where t.id in(:ids),ids的长度不同,替换生成占位符(?)个数也会不同
return
sql
;
}
...
...
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