Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
woshizhushiqiu
spring-framework
提交
362c59c3
S
spring-framework
项目概览
woshizhushiqiu
/
spring-framework
与 Fork 源项目一致
从无法访问的项目Fork
通知
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,发现更多精彩内容 >>
提交
362c59c3
编写于
11月 08, 2018
作者:
J
Juergen Hoeller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Consistently return empty array in case of empty batch arguments
Issue: SPR-17476
上级
b6e86747
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
69 addition
and
50 deletion
+69
-50
spring-jdbc/src/main/java/org/springframework/jdbc/core/BatchUpdateUtils.java
.../java/org/springframework/jdbc/core/BatchUpdateUtils.java
+9
-4
spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/NamedParameterBatchUpdateUtils.java
.../jdbc/core/namedparam/NamedParameterBatchUpdateUtils.java
+6
-5
spring-jdbc/src/test/java/org/springframework/jdbc/core/JdbcTemplateTests.java
...java/org/springframework/jdbc/core/JdbcTemplateTests.java
+34
-26
spring-jdbc/src/test/java/org/springframework/jdbc/core/namedparam/NamedParameterJdbcTemplateTests.java
...jdbc/core/namedparam/NamedParameterJdbcTemplateTests.java
+20
-15
未找到文件。
spring-jdbc/src/main/java/org/springframework/jdbc/core/BatchUpdateUtils.java
浏览文件 @
362c59c3
/*
* Copyright 2002-201
7
the original author or authors.
* Copyright 2002-201
8
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.
...
...
@@ -27,24 +27,29 @@ import org.springframework.lang.Nullable;
* Mainly for internal use within the framework.
*
* @author Thomas Risberg
* @author Juergen Hoeller
* @since 3.0
*/
public
abstract
class
BatchUpdateUtils
{
public
static
int
[]
executeBatchUpdate
(
String
sql
,
final
List
<
Object
[]>
batchValues
,
final
int
[]
columnTypes
,
JdbcOperations
jdbcOperations
)
{
String
sql
,
final
List
<
Object
[]>
batchArgs
,
final
int
[]
columnTypes
,
JdbcOperations
jdbcOperations
)
{
if
(
batchArgs
.
isEmpty
())
{
return
new
int
[
0
];
}
return
jdbcOperations
.
batchUpdate
(
sql
,
new
BatchPreparedStatementSetter
()
{
@Override
public
void
setValues
(
PreparedStatement
ps
,
int
i
)
throws
SQLException
{
Object
[]
values
=
batch
Value
s
.
get
(
i
);
Object
[]
values
=
batch
Arg
s
.
get
(
i
);
setStatementParameters
(
values
,
ps
,
columnTypes
);
}
@Override
public
int
getBatchSize
()
{
return
batch
Value
s
.
size
();
return
batch
Arg
s
.
size
();
}
});
}
...
...
spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/NamedParameterBatchUpdateUtils.java
浏览文件 @
362c59c3
/*
* Copyright 2002-201
7
the original author or authors.
* Copyright 2002-201
8
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.
...
...
@@ -28,15 +28,16 @@ import org.springframework.jdbc.core.JdbcOperations;
* Mainly for internal use within the framework.
*
* @author Thomas Risberg
* @author Juergen Hoeller
* @since 3.0
*/
public
abstract
class
NamedParameterBatchUpdateUtils
extends
BatchUpdateUtils
{
public
static
int
[]
executeBatchUpdateWithNamedParameters
(
final
ParsedSql
parsedSql
,
final
SqlParameterSource
[]
batchArgs
,
JdbcOperations
jdbcOperations
)
{
public
static
int
[]
executeBatchUpdateWithNamedParameters
(
final
ParsedSql
parsedSql
,
final
SqlParameterSource
[]
batchArgs
,
JdbcOperations
jdbcOperations
)
{
if
(
batchArgs
.
length
<
=
0
)
{
return
new
int
[
]
{
0
}
;
if
(
batchArgs
.
length
=
=
0
)
{
return
new
int
[
0
]
;
}
String
sqlToUse
=
NamedParameterUtils
.
substituteNamedParameters
(
parsedSql
,
batchArgs
[
0
]);
...
...
spring-jdbc/src/test/java/org/springframework/jdbc/core/JdbcTemplateTests.java
浏览文件 @
362c59c3
...
...
@@ -192,7 +192,7 @@ public class JdbcTemplateTests {
Object
argument
,
JdbcTemplateCallback
jdbcTemplateCallback
)
throws
Exception
{
String
sql
=
"SELECT FORENAME FROM CUSTMR"
;
String
[]
results
=
{
"rod"
,
"gary"
,
" portia"
};
String
[]
results
=
{
"rod"
,
"gary"
,
" portia"
};
class
StringHandler
implements
RowCallbackHandler
{
private
List
<
String
>
list
=
new
LinkedList
<>();
...
...
@@ -491,8 +491,8 @@ public class JdbcTemplateTests {
@Test
public
void
testBatchUpdateWithPreparedStatement
()
throws
Exception
{
final
String
sql
=
"UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?"
;
final
int
[]
ids
=
new
int
[]
{
100
,
200
};
final
int
[]
rowsAffected
=
new
int
[]
{
1
,
2
};
final
int
[]
ids
=
new
int
[]
{
100
,
200
};
final
int
[]
rowsAffected
=
new
int
[]
{
1
,
2
};
given
(
this
.
preparedStatement
.
executeBatch
()).
willReturn
(
rowsAffected
);
mockDatabaseMetaData
(
true
);
...
...
@@ -525,8 +525,8 @@ public class JdbcTemplateTests {
@Test
public
void
testInterruptibleBatchUpdate
()
throws
Exception
{
final
String
sql
=
"UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?"
;
final
int
[]
ids
=
new
int
[]
{
100
,
200
};
final
int
[]
rowsAffected
=
new
int
[]
{
1
,
2
};
final
int
[]
ids
=
new
int
[]
{
100
,
200
};
final
int
[]
rowsAffected
=
new
int
[]
{
1
,
2
};
given
(
this
.
preparedStatement
.
executeBatch
()).
willReturn
(
rowsAffected
);
mockDatabaseMetaData
(
true
);
...
...
@@ -566,8 +566,8 @@ public class JdbcTemplateTests {
@Test
public
void
testInterruptibleBatchUpdateWithBaseClass
()
throws
Exception
{
final
String
sql
=
"UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?"
;
final
int
[]
ids
=
new
int
[]
{
100
,
200
};
final
int
[]
rowsAffected
=
new
int
[]
{
1
,
2
};
final
int
[]
ids
=
new
int
[]
{
100
,
200
};
final
int
[]
rowsAffected
=
new
int
[]
{
1
,
2
};
given
(
this
.
preparedStatement
.
executeBatch
()).
willReturn
(
rowsAffected
);
mockDatabaseMetaData
(
true
);
...
...
@@ -603,8 +603,8 @@ public class JdbcTemplateTests {
@Test
public
void
testInterruptibleBatchUpdateWithBaseClassAndNoBatchSupport
()
throws
Exception
{
final
String
sql
=
"UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?"
;
final
int
[]
ids
=
new
int
[]
{
100
,
200
};
final
int
[]
rowsAffected
=
new
int
[]
{
1
,
2
};
final
int
[]
ids
=
new
int
[]
{
100
,
200
};
final
int
[]
rowsAffected
=
new
int
[]
{
1
,
2
};
given
(
this
.
preparedStatement
.
executeUpdate
()).
willReturn
(
rowsAffected
[
0
],
rowsAffected
[
1
]);
mockDatabaseMetaData
(
false
);
...
...
@@ -640,8 +640,8 @@ public class JdbcTemplateTests {
@Test
public
void
testBatchUpdateWithPreparedStatementAndNoBatchSupport
()
throws
Exception
{
final
String
sql
=
"UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?"
;
final
int
[]
ids
=
new
int
[]
{
100
,
200
};
final
int
[]
rowsAffected
=
new
int
[]
{
1
,
2
};
final
int
[]
ids
=
new
int
[]
{
100
,
200
};
final
int
[]
rowsAffected
=
new
int
[]
{
1
,
2
};
given
(
this
.
preparedStatement
.
executeUpdate
()).
willReturn
(
rowsAffected
[
0
],
rowsAffected
[
1
]);
...
...
@@ -671,7 +671,7 @@ public class JdbcTemplateTests {
@Test
public
void
testBatchUpdateFails
()
throws
Exception
{
final
String
sql
=
"UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?"
;
final
int
[]
ids
=
new
int
[]
{
100
,
200
};
final
int
[]
ids
=
new
int
[]
{
100
,
200
};
SQLException
sqlException
=
new
SQLException
();
given
(
this
.
preparedStatement
.
executeBatch
()).
willThrow
(
sqlException
);
...
...
@@ -702,6 +702,15 @@ public class JdbcTemplateTests {
}
}
@Test
public
void
testBatchUpdateWithEmptyList
()
throws
Exception
{
final
String
sql
=
"UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?"
;
JdbcTemplate
template
=
new
JdbcTemplate
(
this
.
dataSource
,
false
);
int
[]
actualRowsAffected
=
template
.
batchUpdate
(
sql
,
Collections
.
emptyList
());
assertTrue
(
"executed 0 updates"
,
actualRowsAffected
.
length
==
0
);
}
@Test
public
void
testBatchUpdateWithListOfObjectArrays
()
throws
Exception
{
final
String
sql
=
"UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?"
;
...
...
@@ -712,11 +721,9 @@ public class JdbcTemplateTests {
given
(
this
.
preparedStatement
.
executeBatch
()).
willReturn
(
rowsAffected
);
mockDatabaseMetaData
(
true
);
JdbcTemplate
template
=
new
JdbcTemplate
(
this
.
dataSource
,
false
);
int
[]
actualRowsAffected
=
template
.
batchUpdate
(
sql
,
ids
);
assertTrue
(
"executed 2 updates"
,
actualRowsAffected
.
length
==
2
);
assertEquals
(
rowsAffected
[
0
],
actualRowsAffected
[
0
]);
assertEquals
(
rowsAffected
[
1
],
actualRowsAffected
[
1
]);
...
...
@@ -739,10 +746,9 @@ public class JdbcTemplateTests {
given
(
this
.
preparedStatement
.
executeBatch
()).
willReturn
(
rowsAffected
);
mockDatabaseMetaData
(
true
);
this
.
template
=
new
JdbcTemplate
(
this
.
dataSource
,
false
);
int
[]
actualRowsAffected
=
this
.
template
.
batchUpdate
(
sql
,
ids
,
sqlTypes
);
int
[]
actualRowsAffected
=
this
.
template
.
batchUpdate
(
sql
,
ids
,
sqlTypes
);
assertTrue
(
"executed 2 updates"
,
actualRowsAffected
.
length
==
2
);
assertEquals
(
rowsAffected
[
0
],
actualRowsAffected
[
0
]);
assertEquals
(
rowsAffected
[
1
],
actualRowsAffected
[
1
]);
...
...
@@ -757,8 +763,8 @@ public class JdbcTemplateTests {
public
void
testBatchUpdateWithCollectionOfObjects
()
throws
Exception
{
final
String
sql
=
"UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?"
;
final
List
<
Integer
>
ids
=
Arrays
.
asList
(
100
,
200
,
300
);
final
int
[]
rowsAffected1
=
new
int
[]
{
1
,
2
};
final
int
[]
rowsAffected2
=
new
int
[]
{
3
};
final
int
[]
rowsAffected1
=
new
int
[]
{
1
,
2
};
final
int
[]
rowsAffected2
=
new
int
[]
{
3
};
given
(
this
.
preparedStatement
.
executeBatch
()).
willReturn
(
rowsAffected1
,
rowsAffected2
);
mockDatabaseMetaData
(
true
);
...
...
@@ -781,19 +787,20 @@ public class JdbcTemplateTests {
}
@Test
public
void
testCould
n
tGetConnectionForOperationOrExceptionTranslator
()
throws
SQLException
{
public
void
testCould
No
tGetConnectionForOperationOrExceptionTranslator
()
throws
SQLException
{
SQLException
sqlException
=
new
SQLException
(
"foo"
,
"07xxx"
);
this
.
dataSource
=
mock
(
DataSource
.
class
);
given
(
this
.
dataSource
.
getConnection
()).
willThrow
(
sqlException
);
JdbcTemplate
template
=
new
JdbcTemplate
(
this
.
dataSource
,
false
);
RowCountCallbackHandler
rcch
=
new
RowCountCallbackHandler
();
this
.
thrown
.
expect
(
CannotGetJdbcConnectionException
.
class
);
this
.
thrown
.
expect
(
exceptionCause
(
sameInstance
(
sqlException
)));
template
.
query
(
"SELECT ID, FORENAME FROM CUSTMR WHERE ID < 3"
,
rcch
);
}
@Test
public
void
testCould
n
tGetConnectionForOperationWithLazyExceptionTranslator
()
throws
SQLException
{
public
void
testCould
No
tGetConnectionForOperationWithLazyExceptionTranslator
()
throws
SQLException
{
SQLException
sqlException
=
new
SQLException
(
"foo"
,
"07xxx"
);
this
.
dataSource
=
mock
(
DataSource
.
class
);
given
(
this
.
dataSource
.
getConnection
()).
willThrow
(
sqlException
);
...
...
@@ -801,30 +808,31 @@ public class JdbcTemplateTests {
this
.
template
.
setDataSource
(
this
.
dataSource
);
this
.
template
.
afterPropertiesSet
();
RowCountCallbackHandler
rcch
=
new
RowCountCallbackHandler
();
this
.
thrown
.
expect
(
CannotGetJdbcConnectionException
.
class
);
this
.
thrown
.
expect
(
exceptionCause
(
sameInstance
(
sqlException
)));
this
.
template
.
query
(
"SELECT ID, FORENAME FROM CUSTMR WHERE ID < 3"
,
rcch
);
}
@Test
public
void
testCould
n
tGetConnectionInOperationWithExceptionTranslatorInitializedViaBeanProperty
()
public
void
testCould
No
tGetConnectionInOperationWithExceptionTranslatorInitializedViaBeanProperty
()
throws
SQLException
{
doTestCould
n
tGetConnectionInOperationWithExceptionTranslatorInitialized
(
true
);
doTestCould
No
tGetConnectionInOperationWithExceptionTranslatorInitialized
(
true
);
}
@Test
public
void
testCould
n
tGetConnectionInOperationWithExceptionTranslatorInitializedInAfterPropertiesSet
()
public
void
testCould
No
tGetConnectionInOperationWithExceptionTranslatorInitializedInAfterPropertiesSet
()
throws
SQLException
{
doTestCould
n
tGetConnectionInOperationWithExceptionTranslatorInitialized
(
false
);
doTestCould
No
tGetConnectionInOperationWithExceptionTranslatorInitialized
(
false
);
}
/**
* If beanProperty is true, initialize via exception translator bean property;
* if false, use afterPropertiesSet().
*/
private
void
doTestCould
n
tGetConnectionInOperationWithExceptionTranslatorInitialized
(
boolean
beanProperty
)
private
void
doTestCould
No
tGetConnectionInOperationWithExceptionTranslatorInitialized
(
boolean
beanProperty
)
throws
SQLException
{
SQLException
sqlException
=
new
SQLException
(
"foo"
,
"07xxx"
);
...
...
@@ -884,7 +892,7 @@ public class JdbcTemplateTests {
}
@Test
public
void
testCould
n
tClose
()
throws
Exception
{
public
void
testCould
No
tClose
()
throws
Exception
{
SQLException
sqlException
=
new
SQLException
(
"bar"
);
given
(
this
.
connection
.
createStatement
()).
willReturn
(
this
.
statement
);
given
(
this
.
resultSet
.
next
()).
willReturn
(
false
);
...
...
spring-jdbc/src/test/java/org/springframework/jdbc/core/namedparam/NamedParameterJdbcTemplateTests.java
浏览文件 @
362c59c3
...
...
@@ -418,12 +418,10 @@ public class NamedParameterJdbcTemplateTests {
given
(
preparedStatement
.
executeBatch
()).
willReturn
(
rowsAffected
);
given
(
connection
.
getMetaData
()).
willReturn
(
databaseMetaData
);
namedParameterTemplate
=
new
NamedParameterJdbcTemplate
(
new
JdbcTemplate
(
dataSource
,
false
));
JdbcTemplate
template
=
new
JdbcTemplate
(
dataSource
,
false
);
namedParameterTemplate
=
new
NamedParameterJdbcTemplate
(
template
);
assertSame
(
template
,
namedParameterTemplate
.
getJdbcTemplate
());
int
[]
actualRowsAffected
=
namedParameterTemplate
.
batchUpdate
(
"UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = :id"
,
ids
);
int
[]
actualRowsAffected
=
namedParameterTemplate
.
batchUpdate
(
"UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = :id"
,
ids
);
assertTrue
(
"executed 2 updates"
,
actualRowsAffected
.
length
==
2
);
assertEquals
(
rowsAffected
[
0
],
actualRowsAffected
[
0
]);
assertEquals
(
rowsAffected
[
1
],
actualRowsAffected
[
1
]);
...
...
@@ -435,6 +433,17 @@ public class NamedParameterJdbcTemplateTests {
verify
(
connection
,
atLeastOnce
()).
close
();
}
@Test
public
void
testBatchUpdateWithEmptyMap
()
throws
Exception
{
@SuppressWarnings
(
"unchecked"
)
final
Map
<
String
,
Integer
>[]
ids
=
new
Map
[
0
];
namedParameterTemplate
=
new
NamedParameterJdbcTemplate
(
new
JdbcTemplate
(
dataSource
,
false
));
int
[]
actualRowsAffected
=
namedParameterTemplate
.
batchUpdate
(
"UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = :id"
,
ids
);
assertTrue
(
"executed 0 updates"
,
actualRowsAffected
.
length
==
0
);
}
@Test
public
void
testBatchUpdateWithSqlParameterSource
()
throws
Exception
{
SqlParameterSource
[]
ids
=
new
SqlParameterSource
[
2
];
...
...
@@ -444,12 +453,10 @@ public class NamedParameterJdbcTemplateTests {
given
(
preparedStatement
.
executeBatch
()).
willReturn
(
rowsAffected
);
given
(
connection
.
getMetaData
()).
willReturn
(
databaseMetaData
);
namedParameterTemplate
=
new
NamedParameterJdbcTemplate
(
new
JdbcTemplate
(
dataSource
,
false
));
JdbcTemplate
template
=
new
JdbcTemplate
(
dataSource
,
false
);
namedParameterTemplate
=
new
NamedParameterJdbcTemplate
(
template
);
assertSame
(
template
,
namedParameterTemplate
.
getJdbcTemplate
());
int
[]
actualRowsAffected
=
namedParameterTemplate
.
batchUpdate
(
"UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = :id"
,
ids
);
int
[]
actualRowsAffected
=
namedParameterTemplate
.
batchUpdate
(
"UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = :id"
,
ids
);
assertTrue
(
"executed 2 updates"
,
actualRowsAffected
.
length
==
2
);
assertEquals
(
rowsAffected
[
0
],
actualRowsAffected
[
0
]);
assertEquals
(
rowsAffected
[
1
],
actualRowsAffected
[
1
]);
...
...
@@ -470,12 +477,10 @@ public class NamedParameterJdbcTemplateTests {
given
(
preparedStatement
.
executeBatch
()).
willReturn
(
rowsAffected
);
given
(
connection
.
getMetaData
()).
willReturn
(
databaseMetaData
);
namedParameterTemplate
=
new
NamedParameterJdbcTemplate
(
new
JdbcTemplate
(
dataSource
,
false
));
JdbcTemplate
template
=
new
JdbcTemplate
(
dataSource
,
false
);
namedParameterTemplate
=
new
NamedParameterJdbcTemplate
(
template
);
assertSame
(
template
,
namedParameterTemplate
.
getJdbcTemplate
());
int
[]
actualRowsAffected
=
namedParameterTemplate
.
batchUpdate
(
"UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = :id"
,
ids
);
int
[]
actualRowsAffected
=
namedParameterTemplate
.
batchUpdate
(
"UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = :id"
,
ids
);
assertTrue
(
"executed 2 updates"
,
actualRowsAffected
.
length
==
2
);
assertEquals
(
rowsAffected
[
0
],
actualRowsAffected
[
0
]);
assertEquals
(
rowsAffected
[
1
],
actualRowsAffected
[
1
]);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录