Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
7287baef
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,体验更适合开发者的 AI 搜索 >>
提交
7287baef
编写于
7月 19, 2016
作者:
J
Juergen Hoeller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
GenericSqlQuery configured with RowMapper instance
Issue: SPR-14489
上级
aae4874b
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
71 addition
and
26 deletion
+71
-26
spring-jdbc/src/main/java/org/springframework/jdbc/object/GenericSqlQuery.java
...java/org/springframework/jdbc/object/GenericSqlQuery.java
+34
-20
spring-jdbc/src/test/java/org/springframework/jdbc/object/GenericSqlQueryTests.java
...org/springframework/jdbc/object/GenericSqlQueryTests.java
+12
-5
spring-jdbc/src/test/resources/org/springframework/jdbc/object/GenericSqlQueryTests-context.xml
...ingframework/jdbc/object/GenericSqlQueryTests-context.xml
+25
-1
未找到文件。
spring-jdbc/src/main/java/org/springframework/jdbc/object/GenericSqlQuery.java
浏览文件 @
7287baef
/*
* Copyright 2002-201
2
the original author or authors.
* Copyright 2002-201
6
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.
...
...
@@ -18,43 +18,57 @@ package org.springframework.jdbc.object;
import
java.util.Map
;
import
org.springframework.
dao.InvalidDataAccessResourceUsageException
;
import
org.springframework.
beans.BeanUtils
;
import
org.springframework.jdbc.core.RowMapper
;
import
org.springframework.util.Assert
;
/**
* A concrete variant of {@link SqlQuery} which can be configured
* with a {@link RowMapper}.
*
* @author Thomas Risberg
* @author Juergen Hoeller
* @since 3.0
* @see #setRowMapper
* @see #setRowMapperClass
*/
public
class
GenericSqlQuery
<
T
>
extends
SqlQuery
<
T
>
{
Class
<?>
rowMapperClass
;
private
RowMapper
<
T
>
rowMapper
;
@SuppressWarnings
(
"rawtypes"
)
private
Class
<?
extends
RowMapper
>
rowMapperClass
;
RowMapper
<?>
rowMapper
;
/**
* Set a specific {@link RowMapper} instance to use for this query.
* @since 4.3.2
*/
public
void
setRowMapper
(
RowMapper
<
T
>
rowMapper
)
{
this
.
rowMapper
=
rowMapper
;
}
/**
* Set a {@link RowMapper} class for this query, creating a fresh
* {@link RowMapper} instance per execution.
*/
@SuppressWarnings
(
"rawtypes"
)
public
void
setRowMapperClass
(
Class
<?
extends
RowMapper
>
rowMapperClass
)
throws
IllegalAccessException
,
InstantiationException
{
public
void
setRowMapperClass
(
Class
<?
extends
RowMapper
>
rowMapperClass
)
{
this
.
rowMapperClass
=
rowMapperClass
;
if
(!
RowMapper
.
class
.
isAssignableFrom
(
rowMapperClass
))
throw
new
IllegalStateException
(
"The specified class '"
+
rowMapperClass
.
getName
()
+
" is not a sub class of "
+
"'org.springframework.jdbc.core.RowMapper'"
);
}
@Override
public
void
afterPropertiesSet
()
{
super
.
afterPropertiesSet
();
Assert
.
notNull
(
rowMapperClass
,
"The 'rowMapperClass' property is required"
);
Assert
.
isTrue
(
this
.
rowMapper
!=
null
||
this
.
rowMapperClass
!=
null
,
"'rowMapper' or 'rowMapperClass' is required"
);
}
@Override
@SuppressWarnings
(
"unchecked"
)
protected
RowMapper
<
T
>
newRowMapper
(
Object
[]
parameters
,
Map
<?,
?>
context
)
{
try
{
return
(
RowMapper
<
T
>)
rowMapperClass
.
newInstance
();
}
catch
(
InstantiationException
e
)
{
throw
new
InvalidDataAccessResourceUsageException
(
"Unable to instantiate RowMapper"
,
e
);
}
catch
(
IllegalAccessException
e
)
{
throw
new
InvalidDataAccessResourceUsageException
(
"Unable to instantiate RowMapper"
,
e
);
}
return
(
this
.
rowMapper
!=
null
?
this
.
rowMapper
:
BeanUtils
.
instantiateClass
(
this
.
rowMapperClass
));
}
}
spring-jdbc/src/test/java/org/springframework/jdbc/object/GenericSqlQueryTests.java
浏览文件 @
7287baef
...
...
@@ -16,7 +16,6 @@
package
org.springframework.jdbc.object
;
import
java.sql.Connection
;
import
java.sql.PreparedStatement
;
import
java.sql.ResultSet
;
...
...
@@ -43,11 +42,12 @@ import static org.mockito.BDDMockito.*;
/**
* @author Thomas Risberg
* @author Juergen Hoeller
*/
public
class
GenericSqlQueryTests
{
private
static
final
String
SELECT_ID_FORENAME_NAMED_PARAMETERS_PARSED
=
"select id, forename from custmr where id = ? and country = ?"
;
"select id, forename from custmr where id = ? and country = ?"
;
private
BeanFactory
beanFactory
;
...
...
@@ -57,6 +57,7 @@ public class GenericSqlQueryTests {
private
ResultSet
resultSet
;
@Before
public
void
setUp
()
throws
Exception
{
this
.
beanFactory
=
new
DefaultListableBeanFactory
();
...
...
@@ -72,17 +73,23 @@ public class GenericSqlQueryTests {
}
@Test
public
void
test
PlaceHoldersCustomerQuery
()
throws
SQLException
{
SqlQuery
<?>
query
=
(
SqlQuery
<?>)
beanFactory
.
getBean
(
"queryWithPlace
H
olders"
);
public
void
test
CustomerQueryWithPlaceholders
()
throws
SQLException
{
SqlQuery
<?>
query
=
(
SqlQuery
<?>)
beanFactory
.
getBean
(
"queryWithPlace
h
olders"
);
doTestCustomerQuery
(
query
,
false
);
}
@Test
public
void
test
NamedParameterCustomerQuery
()
throws
SQLException
{
public
void
test
CustomerQueryWithNamedParameters
()
throws
SQLException
{
SqlQuery
<?>
query
=
(
SqlQuery
<?>)
beanFactory
.
getBean
(
"queryWithNamedParameters"
);
doTestCustomerQuery
(
query
,
true
);
}
@Test
public
void
testCustomerQueryWithRowMapperInstance
()
throws
SQLException
{
SqlQuery
<?>
query
=
(
SqlQuery
<?>)
beanFactory
.
getBean
(
"queryWithRowMapperBean"
);
doTestCustomerQuery
(
query
,
true
);
}
private
void
doTestCustomerQuery
(
SqlQuery
<?>
query
,
boolean
namedParameters
)
throws
SQLException
{
given
(
resultSet
.
next
()).
willReturn
(
true
);
given
(
resultSet
.
getInt
(
"id"
)).
willReturn
(
1
);
...
...
spring-jdbc/src/test/resources/org/springframework/jdbc/object/GenericSqlQueryTests-context.xml
浏览文件 @
7287baef
...
...
@@ -6,7 +6,7 @@
<bean
id=
"dataSource"
class=
"org.springframework.jdbc.datasource.TestDataSourceWrapper"
/>
<bean
id=
"queryWithPlace
H
olders"
class=
"org.springframework.jdbc.object.GenericSqlQuery"
>
<bean
id=
"queryWithPlace
h
olders"
class=
"org.springframework.jdbc.object.GenericSqlQuery"
>
<property
name=
"dataSource"
ref=
"dataSource"
/>
<property
name=
"sql"
value=
"select id, forename from custmr where id = ? and country = ?"
/>
<property
name=
"parameters"
>
...
...
@@ -50,4 +50,28 @@
<property
name=
"rowMapperClass"
value=
"org.springframework.jdbc.object.CustomerMapper"
/>
</bean>
<bean
id=
"queryWithRowMapperBean"
class=
"org.springframework.jdbc.object.GenericSqlQuery"
>
<property
name=
"dataSource"
ref=
"dataSource"
/>
<property
name=
"sql"
value=
"select id, forename from custmr where id = :id and country = :country"
/>
<property
name=
"parameters"
>
<list>
<bean
class=
"org.springframework.jdbc.core.SqlParameter"
>
<constructor-arg
index=
"0"
value=
"id"
/>
<constructor-arg
index=
"1"
>
<util:constant
static-field=
"java.sql.Types.INTEGER"
/>
</constructor-arg>
</bean>
<bean
class=
"org.springframework.jdbc.core.SqlParameter"
>
<constructor-arg
index=
"0"
value=
"country"
/>
<constructor-arg
index=
"1"
>
<util:constant
static-field=
"java.sql.Types.VARCHAR"
/>
</constructor-arg>
</bean>
</list>
</property>
<property
name=
"rowMapper"
>
<bean
class=
"org.springframework.jdbc.object.CustomerMapper"
/>
</property>
</bean>
</beans>
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录