Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
41b58584
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,发现更多精彩内容 >>
提交
41b58584
编写于
7月 13, 2015
作者:
J
Juergen Hoeller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
BeanPropertyRowMapper uses US locale for lower-case conversion by default
Issue: SPR-13216
上级
c7fef87e
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
67 addition
and
54 deletion
+67
-54
spring-jdbc/src/main/java/org/springframework/jdbc/core/BeanPropertyRowMapper.java
.../org/springframework/jdbc/core/BeanPropertyRowMapper.java
+67
-54
未找到文件。
spring-jdbc/src/main/java/org/springframework/jdbc/core/BeanPropertyRowMapper.java
浏览文件 @
41b58584
/*
/*
* Copyright 2002-201
4
the original author or authors.
* Copyright 2002-201
5
the original author or authors.
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* you may not use this file except in compliance with the License.
...
@@ -22,6 +22,7 @@ import java.sql.ResultSetMetaData;
...
@@ -22,6 +22,7 @@ import java.sql.ResultSetMetaData;
import
java.sql.SQLException
;
import
java.sql.SQLException
;
import
java.util.HashMap
;
import
java.util.HashMap
;
import
java.util.HashSet
;
import
java.util.HashSet
;
import
java.util.Locale
;
import
java.util.Map
;
import
java.util.Map
;
import
java.util.Set
;
import
java.util.Set
;
...
@@ -136,9 +137,51 @@ public class BeanPropertyRowMapper<T> implements RowMapper<T> {
...
@@ -136,9 +137,51 @@ public class BeanPropertyRowMapper<T> implements RowMapper<T> {
}
}
}
}
/**
* Get the class that we are mapping to.
*/
public
final
Class
<
T
>
getMappedClass
()
{
return
this
.
mappedClass
;
}
/**
* Set whether we're strictly validating that all bean properties have been mapped
* from corresponding database fields.
* <p>Default is {@code false}, accepting unpopulated properties in the target bean.
*/
public
void
setCheckFullyPopulated
(
boolean
checkFullyPopulated
)
{
this
.
checkFullyPopulated
=
checkFullyPopulated
;
}
/**
* Return whether we're strictly validating that all bean properties have been
* mapped from corresponding database fields.
*/
public
boolean
isCheckFullyPopulated
()
{
return
this
.
checkFullyPopulated
;
}
/**
* Set whether we're defaulting Java primitives in the case of mapping a null value
* from corresponding database fields.
* <p>Default is {@code false}, throwing an exception when nulls are mapped to Java primitives.
*/
public
void
setPrimitivesDefaultedForNullValue
(
boolean
primitivesDefaultedForNullValue
)
{
this
.
primitivesDefaultedForNullValue
=
primitivesDefaultedForNullValue
;
}
/**
* Return whether we're defaulting Java primitives in the case of mapping a null value
* from corresponding database fields.
*/
public
boolean
isPrimitivesDefaultedForNullValue
()
{
return
this
.
primitivesDefaultedForNullValue
;
}
/**
/**
* Initialize the mapping metadata for the given class.
* Initialize the mapping metadata for the given class.
* @param mappedClass the mapped class
.
* @param mappedClass the mapped class
*/
*/
protected
void
initialize
(
Class
<
T
>
mappedClass
)
{
protected
void
initialize
(
Class
<
T
>
mappedClass
)
{
this
.
mappedClass
=
mappedClass
;
this
.
mappedClass
=
mappedClass
;
...
@@ -147,9 +190,9 @@ public class BeanPropertyRowMapper<T> implements RowMapper<T> {
...
@@ -147,9 +190,9 @@ public class BeanPropertyRowMapper<T> implements RowMapper<T> {
PropertyDescriptor
[]
pds
=
BeanUtils
.
getPropertyDescriptors
(
mappedClass
);
PropertyDescriptor
[]
pds
=
BeanUtils
.
getPropertyDescriptors
(
mappedClass
);
for
(
PropertyDescriptor
pd
:
pds
)
{
for
(
PropertyDescriptor
pd
:
pds
)
{
if
(
pd
.
getWriteMethod
()
!=
null
)
{
if
(
pd
.
getWriteMethod
()
!=
null
)
{
this
.
mappedFields
.
put
(
pd
.
getName
().
toLowerCase
(
),
pd
);
this
.
mappedFields
.
put
(
lowerCaseName
(
pd
.
getName
()
),
pd
);
String
underscoredName
=
underscoreName
(
pd
.
getName
());
String
underscoredName
=
underscoreName
(
pd
.
getName
());
if
(!
pd
.
getName
().
toLowerCase
(
).
equals
(
underscoredName
))
{
if
(!
lowerCaseName
(
pd
.
getName
()
).
equals
(
underscoredName
))
{
this
.
mappedFields
.
put
(
underscoredName
,
pd
);
this
.
mappedFields
.
put
(
underscoredName
,
pd
);
}
}
this
.
mappedProperties
.
add
(
pd
.
getName
());
this
.
mappedProperties
.
add
(
pd
.
getName
());
...
@@ -160,18 +203,20 @@ public class BeanPropertyRowMapper<T> implements RowMapper<T> {
...
@@ -160,18 +203,20 @@ public class BeanPropertyRowMapper<T> implements RowMapper<T> {
/**
/**
* Convert a name in camelCase to an underscored name in lower case.
* Convert a name in camelCase to an underscored name in lower case.
* Any upper case letters are converted to lower case with a preceding underscore.
* Any upper case letters are converted to lower case with a preceding underscore.
* @param name the
string containing
original name
* @param name the original name
* @return the converted name
* @return the converted name
* @since 4.2
* @see #lowerCaseName
*/
*/
pr
ivate
String
underscoreName
(
String
name
)
{
pr
otected
String
underscoreName
(
String
name
)
{
if
(!
StringUtils
.
hasLength
(
name
))
{
if
(!
StringUtils
.
hasLength
(
name
))
{
return
""
;
return
""
;
}
}
StringBuilder
result
=
new
StringBuilder
();
StringBuilder
result
=
new
StringBuilder
();
result
.
append
(
name
.
substring
(
0
,
1
).
toLowerCase
(
));
result
.
append
(
lowerCaseName
(
name
.
substring
(
0
,
1
)
));
for
(
int
i
=
1
;
i
<
name
.
length
();
i
++)
{
for
(
int
i
=
1
;
i
<
name
.
length
();
i
++)
{
String
s
=
name
.
substring
(
i
,
i
+
1
);
String
s
=
name
.
substring
(
i
,
i
+
1
);
String
slc
=
s
.
toLowerCase
(
);
String
slc
=
lowerCaseName
(
s
);
if
(!
s
.
equals
(
slc
))
{
if
(!
s
.
equals
(
slc
))
{
result
.
append
(
"_"
).
append
(
slc
);
result
.
append
(
"_"
).
append
(
slc
);
}
}
...
@@ -183,45 +228,14 @@ public class BeanPropertyRowMapper<T> implements RowMapper<T> {
...
@@ -183,45 +228,14 @@ public class BeanPropertyRowMapper<T> implements RowMapper<T> {
}
}
/**
/**
* Get the class that we are mapping to.
* Convert the given name to lower case.
*/
* By default, conversions will happen within the US locale.
public
final
Class
<
T
>
getMappedClass
()
{
* @param name the original name
return
this
.
mappedClass
;
* @return the converted name
}
* @since 4.2
/**
* Set whether we're strictly validating that all bean properties have been
* mapped from corresponding database fields.
* <p>Default is {@code false}, accepting unpopulated properties in the
* target bean.
*/
public
void
setCheckFullyPopulated
(
boolean
checkFullyPopulated
)
{
this
.
checkFullyPopulated
=
checkFullyPopulated
;
}
/**
* Return whether we're strictly validating that all bean properties have been
* mapped from corresponding database fields.
*/
public
boolean
isCheckFullyPopulated
()
{
return
this
.
checkFullyPopulated
;
}
/**
* Set whether we're defaulting Java primitives in the case of mapping a null value
* from corresponding database fields.
* <p>Default is {@code false}, throwing an exception when nulls are mapped to Java primitives.
*/
public
void
setPrimitivesDefaultedForNullValue
(
boolean
primitivesDefaultedForNullValue
)
{
this
.
primitivesDefaultedForNullValue
=
primitivesDefaultedForNullValue
;
}
/**
* Return whether we're defaulting Java primitives in the case of mapping a null value
* from corresponding database fields.
*/
*/
p
ublic
boolean
isPrimitivesDefaultedForNullValue
(
)
{
p
rotected
String
lowerCaseName
(
String
name
)
{
return
primitivesDefaultedForNullValue
;
return
name
.
toLowerCase
(
Locale
.
US
)
;
}
}
...
@@ -243,7 +257,7 @@ public class BeanPropertyRowMapper<T> implements RowMapper<T> {
...
@@ -243,7 +257,7 @@ public class BeanPropertyRowMapper<T> implements RowMapper<T> {
for
(
int
index
=
1
;
index
<=
columnCount
;
index
++)
{
for
(
int
index
=
1
;
index
<=
columnCount
;
index
++)
{
String
column
=
JdbcUtils
.
lookupColumnName
(
rsmd
,
index
);
String
column
=
JdbcUtils
.
lookupColumnName
(
rsmd
,
index
);
PropertyDescriptor
pd
=
this
.
mappedFields
.
get
(
column
.
replaceAll
(
" "
,
""
).
toLowerCase
(
));
PropertyDescriptor
pd
=
this
.
mappedFields
.
get
(
lowerCaseName
(
column
.
replaceAll
(
" "
,
""
)
));
if
(
pd
!=
null
)
{
if
(
pd
!=
null
)
{
try
{
try
{
Object
value
=
getColumnValue
(
rs
,
index
,
pd
);
Object
value
=
getColumnValue
(
rs
,
index
,
pd
);
...
@@ -254,15 +268,14 @@ public class BeanPropertyRowMapper<T> implements RowMapper<T> {
...
@@ -254,15 +268,14 @@ public class BeanPropertyRowMapper<T> implements RowMapper<T> {
try
{
try
{
bw
.
setPropertyValue
(
pd
.
getName
(),
value
);
bw
.
setPropertyValue
(
pd
.
getName
(),
value
);
}
}
catch
(
TypeMismatchException
e
)
{
catch
(
TypeMismatchException
ex
)
{
if
(
value
==
null
&&
primitivesDefaultedForNullValue
)
{
if
(
value
==
null
&&
this
.
primitivesDefaultedForNullValue
)
{
logger
.
debug
(
"Intercepted TypeMismatchException for row "
+
rowNumber
+
logger
.
debug
(
"Intercepted TypeMismatchException for row "
+
rowNumber
+
" and column '"
+
" and column '"
+
column
+
"' with value "
+
value
+
column
+
"' with null value when setting property '"
+
pd
.
getName
()
+
" when setting property '"
+
pd
.
getName
()
+
"' of type "
+
pd
.
getPropertyType
()
+
"' of type "
+
pd
.
getPropertyType
()
+
" on object: "
+
mappedObject
);
" on object: "
+
mappedObject
);
}
}
else
{
else
{
throw
e
;
throw
e
x
;
}
}
}
}
if
(
populatedProperties
!=
null
)
{
if
(
populatedProperties
!=
null
)
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录