Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
eeb79c8d
S
spring-framework
项目概览
爱吃血肠
/
spring-framework
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
spring-framework
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
eeb79c8d
编写于
6月 11, 2019
作者:
J
Juergen Hoeller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
NamedParameterJdbcTemplate treats arrays/Iterables like Collections
Closes gh-22981
上级
9f92b42d
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
31 addition
and
16 deletion
+31
-16
spring-jdbc/src/main/java/org/springframework/jdbc/core/PreparedStatementCreatorFactory.java
...gframework/jdbc/core/PreparedStatementCreatorFactory.java
+14
-8
spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/NamedParameterUtils.java
...ngframework/jdbc/core/namedparam/NamedParameterUtils.java
+8
-4
spring-jdbc/src/test/java/org/springframework/jdbc/core/namedparam/NamedParameterJdbcTemplateTests.java
...jdbc/core/namedparam/NamedParameterJdbcTemplateTests.java
+9
-4
未找到文件。
spring-jdbc/src/main/java/org/springframework/jdbc/core/PreparedStatementCreatorFactory.java
浏览文件 @
eeb79c8d
/*
* Copyright 2002-201
8
the original author or authors.
* Copyright 2002-201
9
the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
...
...
@@ -22,7 +22,6 @@ import java.sql.ResultSet;
import
java.sql.SQLException
;
import
java.sql.Types
;
import
java.util.Arrays
;
import
java.util.Collection
;
import
java.util.Collections
;
import
java.util.HashSet
;
import
java.util.LinkedList
;
...
...
@@ -32,6 +31,7 @@ import java.util.Set;
import
org.springframework.dao.InvalidDataAccessApiUsageException
;
import
org.springframework.lang.Nullable
;
import
org.springframework.util.Assert
;
import
org.springframework.util.ObjectUtils
;
/**
* Helper class that efficiently creates multiple {@link PreparedStatementCreator}
...
...
@@ -268,13 +268,19 @@ public class PreparedStatementCreatorFactory {
}
declaredParameter
=
declaredParameters
.
get
(
i
);
}
if
(
in
instanceof
Collection
&&
declaredParameter
.
getSqlType
()
!=
Types
.
ARRAY
)
{
Collection
<?>
entries
=
(
Collection
<?>)
in
;
if
(
in
!=
null
&&
in
.
getClass
().
isArray
())
{
in
=
Arrays
.
asList
(
ObjectUtils
.
toObjectArray
(
in
));
}
if
(
in
instanceof
Iterable
&&
declaredParameter
.
getSqlType
()
!=
Types
.
ARRAY
)
{
Iterable
<?>
entries
=
(
Iterable
<?>)
in
;
for
(
Object
entry
:
entries
)
{
if
(
entry
instanceof
Object
[])
{
Object
[]
valueArray
=
((
Object
[])
entry
);
for
(
Object
argValue
:
valueArray
)
{
StatementCreatorUtils
.
setParameterValue
(
ps
,
sqlColIndx
++,
declaredParameter
,
argValue
);
if
(
entry
!=
null
&&
entry
.
getClass
().
isArray
())
{
entry
=
Arrays
.
asList
(
ObjectUtils
.
toObjectArray
(
entry
));
}
if
(
entry
instanceof
Iterable
)
{
Iterable
<?>
values
=
(
Iterable
<?>)
entry
;
for
(
Object
value
:
values
)
{
StatementCreatorUtils
.
setParameterValue
(
ps
,
sqlColIndx
++,
declaredParameter
,
value
);
}
}
else
{
...
...
spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/NamedParameterUtils.java
浏览文件 @
eeb79c8d
/*
* Copyright 2002-201
8
the original author or authors.
* Copyright 2002-201
9
the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
...
...
@@ -17,7 +17,7 @@
package
org.springframework.jdbc.core.namedparam
;
import
java.util.ArrayList
;
import
java.util.
Collection
;
import
java.util.
Arrays
;
import
java.util.HashSet
;
import
java.util.Iterator
;
import
java.util.List
;
...
...
@@ -29,6 +29,7 @@ import org.springframework.jdbc.core.SqlParameter;
import
org.springframework.jdbc.core.SqlParameterValue
;
import
org.springframework.lang.Nullable
;
import
org.springframework.util.Assert
;
import
org.springframework.util.ObjectUtils
;
/**
* Helper methods for named parameter parsing.
...
...
@@ -284,8 +285,11 @@ public abstract class NamedParameterUtils {
if
(
value
instanceof
SqlParameterValue
)
{
value
=
((
SqlParameterValue
)
value
).
getValue
();
}
if
(
value
instanceof
Collection
)
{
Iterator
<?>
entryIter
=
((
Collection
<?>)
value
).
iterator
();
if
(
value
!=
null
&&
value
.
getClass
().
isArray
())
{
value
=
Arrays
.
asList
(
ObjectUtils
.
toObjectArray
(
value
));
}
if
(
value
instanceof
Iterable
)
{
Iterator
<?>
entryIter
=
((
Iterable
<?>)
value
).
iterator
();
int
k
=
0
;
while
(
entryIter
.
hasNext
())
{
if
(
k
>
0
)
{
...
...
spring-jdbc/src/test/java/org/springframework/jdbc/core/namedparam/NamedParameterJdbcTemplateTests.java
浏览文件 @
eeb79c8d
...
...
@@ -475,11 +475,12 @@ public class NamedParameterJdbcTemplateTests {
@Test
public
void
testBatchUpdateWithInClause
()
throws
Exception
{
@SuppressWarnings
(
"unchecked"
)
Map
<
String
,
Object
>[]
parameters
=
new
Map
[
2
];
Map
<
String
,
Object
>[]
parameters
=
new
Map
[
3
];
parameters
[
0
]
=
Collections
.
singletonMap
(
"ids"
,
Arrays
.
asList
(
1
,
2
));
parameters
[
1
]
=
Collections
.
singletonMap
(
"ids"
,
Arrays
.
asList
(
3
,
4
));
parameters
[
1
]
=
Collections
.
singletonMap
(
"ids"
,
new
Integer
[]
{
3
,
4
});
parameters
[
2
]
=
Collections
.
singletonMap
(
"ids"
,
(
Iterable
<
Integer
>)
()
->
Arrays
.
asList
(
5
,
6
).
iterator
());
final
int
[]
rowsAffected
=
new
int
[]
{
1
,
2
};
final
int
[]
rowsAffected
=
new
int
[]
{
1
,
2
,
3
};
given
(
preparedStatement
.
executeBatch
()).
willReturn
(
rowsAffected
);
given
(
connection
.
getMetaData
()).
willReturn
(
databaseMetaData
);
...
...
@@ -491,7 +492,7 @@ public class NamedParameterJdbcTemplateTests {
parameters
);
assertThat
(
actualRowsAffected
.
length
).
as
(
"executed
2 updates"
).
isEqualTo
(
2
);
assertThat
(
actualRowsAffected
.
length
).
as
(
"executed
3 updates"
).
isEqualTo
(
3
);
InOrder
inOrder
=
inOrder
(
preparedStatement
);
...
...
@@ -503,6 +504,10 @@ public class NamedParameterJdbcTemplateTests {
inOrder
.
verify
(
preparedStatement
).
setObject
(
2
,
4
);
inOrder
.
verify
(
preparedStatement
).
addBatch
();
inOrder
.
verify
(
preparedStatement
).
setObject
(
1
,
5
);
inOrder
.
verify
(
preparedStatement
).
setObject
(
2
,
6
);
inOrder
.
verify
(
preparedStatement
).
addBatch
();
inOrder
.
verify
(
preparedStatement
,
atLeastOnce
()).
close
();
verify
(
connection
,
atLeastOnce
()).
close
();
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录