oracle Spring JDBC BeanPropertyRowMapper yes no ('Y','N') 到布尔 bean 属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15411843/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring JDBC BeanPropertyRowMapper yes no ('Y','N') to boolean bean properties
提问by MickJ
I have a class with some string, int and boolean fields. I have the getters and setters declared for them.
我有一个包含一些字符串、整数和布尔字段的类。我为他们声明了 getter 和 setter。
public class SomeClass {
private int id;
private String description;
private boolean active;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
}
}
I am BeanPropertyRowMapper to get all the objects from and Oracle DB.
我是 BeanPropertyRowMapper,用于从 Oracle DB 获取所有对象。
@Override
public List<Destination> getAll() {
List<SomeClass> objs = jdbcTemplate.query(
myQuery, new BeanPropertyRowMapper<SomeClass>(SomeClass.class));
return objs;
}
If the debug is turned on I see:
如果调试打开,我会看到:
[3/14/13 10:02:09:202 EDT] 00000018 SystemOut O DEBUG BeanPropertyRowMapper - Mapping column 'ID' to property 'id' of type int
[3/14/13 10:02:09:202 EDT] 00000018 SystemOut O DEBUG BeanPropertyRowMapper - Mapping column 'DESCRIPTION' to property 'description' of type class java.lang.String
And then it fails trying to map active. Active is defined as 1 byte CHAR in the DB with values as 'Y' or 'N'. What is the best way to use BeanPropertyRowMapper and successfully convert values such as 'Y', and 'N' to boolean?
然后尝试映射活动失败。活动定义为 DB 中的 1 字节 CHAR,值为“Y”或“N”。使用 BeanPropertyRowMapper 并将“Y”和“N”等值成功转换为布尔值的最佳方法是什么?
回答by MickJ
So I figured out how to do this. I extended BeanPropertyRowMapper and handler boolean types through some custom code before handing off the control to beanpropertyrowmapper for rest of the data types.
所以我想出了如何做到这一点。在将控件交给 beanpropertyrowmapper 以获取其余数据类型之前,我通过一些自定义代码扩展了 BeanPropertyRowMapper 和处理程序布尔类型。
Note: It works for me because I use oracle and all of the 'boolean' type columns are strings with 'y','yes','n' & 'no' type values.
注意:它对我有用,因为我使用 oracle 并且所有 'boolean' 类型列都是带有 'y'、'yes'、'n' 和 'no' 类型值的字符串。
Those who use numerical 1,0 or other formats could potentially improve it further by making it generic through an object yes map and getting objects from resultset and looking them up in this map. Hope this helps someone else in a situation like mine.
那些使用数字 1,0 或其他格式的人可以通过对象 yes 映射使其通用并从结果集中获取对象并在此映射中查找它们来进一步改进它。希望这可以帮助其他人在像我这样的情况下。
import java.beans.PropertyDescriptor;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
/**
* Extends BeanPropertyRowMapper to allow for boolean fields
* mapped to 'Y,'N' type column to get set correctly. Using stock BeanPropertyRowMapper
* would throw a SQLException.
*
*/
public class ExtendedBeanPropertyRowMapper<T> extends BeanPropertyRowMapper<T> {
//Contains valid true values
public static final Set<String> TRUE_SET = new HashSet<String>(Arrays.asList("y", "yes", "true"));
public ExtendedBeanPropertyRowMapper(Class<T> class1) {
super(class1);
}
@Override
/**
* Override <code>getColumnValue</code> to add ability to map 'Y','N' type columns to
* boolean properties.
*
* @param rs is the ResultSet holding the data
* @param index is the column index
* @param pd the bean property that each result object is expected to match
* (or <code>null</code> if none specified)
* @return the Object value
* @throws SQLException in case of extraction failure
* @see org.springframework.jdbc.core.BeanPropertyRowMapper#getColumnValue(java.sql.ResultSet, int, PropertyDescriptor)
*/
protected Object getColumnValue(ResultSet rs, int index,
PropertyDescriptor pd) throws SQLException {
Class<?> requiredType = pd.getPropertyType();
if (boolean.class.equals(requiredType) || Boolean.class.equals(requiredType)) {
String stringValue = rs.getString(index);
if(!StringUtils.isEmpty(stringValue) && TRUE_SET.contains(stringValue.toLowerCase())){
return true;
}
else return false;
}
return super.getColumnValue(rs, index, pd);
}
}
回答by Harikumar
BeanPropertyRowMapper
will convert values into a Boolean
object with 0=false
and 1=true
. Just tried this and it works.
BeanPropertyRowMapper
将值转换为Boolean
带有0=false
和的对象1=true
。刚试过这个,它的工作原理。
This blog posthas more information, as well as code examples in Java and C with OCCI.
这篇博文包含更多信息,以及带有 OCCI 的 Java 和 C 代码示例。
回答by Apurv
Old question, but you can do something like
老问题,但你可以做类似的事情
public void setIsActive(String active) {
this.active = "Y".equalsIgnoreCase(active);
}
回答by Jo?o Almeida
As pointed out by Harikumar, BeanPropertyRowMapper
does in fact convert 0 and 1 to boolean values. I couldn't find any documentation to support this, but this is in fact the current case.
正如 Harikumar 所指出的,BeanPropertyRowMapper
实际上确实将 0 和 1 转换为布尔值。我找不到任何文档来支持这一点,但这实际上是当前的情况。
So, a solution that doesn't require you to extend BeanPropertyRowMapper
would be to decode your column into these values:
因此,不需要您扩展的解决方案是BeanPropertyRowMapper
将您的列解码为这些值:
@Override
public List<Destination> getAll() {
List<SomeClass> objs = jdbcTemplate.query(
"SELECT ID, DESCRIPTION, " +
" DECODE(ACTIVE, 'Y', 1,'N', 0) as ACTIVE " +
" FROM YOUR_TABLE",
new BeanPropertyRowMapper<SomeClass>(SomeClass.class));
return objs;
}