Database Spring JDBC BeanPropertyRowMapper是否('Y','N')到布尔bean属性
我有一个包含字符串、int和布尔字段的类。我已经为他们宣布了接受者和接受者Database Spring JDBC BeanPropertyRowMapper是否('Y','N')到布尔bean属性,database,oracle,spring,boolean,spring-jdbc,Database,Oracle,Spring,Boolean,Spring Jdbc,我有一个包含字符串、int和布尔字段的类。我已经为他们宣布了接受者和接受者 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 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;
}
}
我是从和Oracle数据库获取所有对象的高级管理员
@Override
public List<Destination> getAll() {
List<SomeClass> objs = jdbcTemplate.query(
myQuery, new BeanPropertyRowMapper<SomeClass>(SomeClass.class));
return objs;
}
然后它尝试映射活动时失败。Active在数据库中定义为1字节字符,值为“Y”或“N”。使用BeanPropertyRowMapper并成功地将“Y”和“N”等值转换为布尔值的最佳方法是什么?因此我想出了如何做到这一点。我通过一些自定义代码扩展了BeanPropertyRowMapper和handler布尔类型,然后将其余数据类型的控件交给BeanPropertyRowMapper 注意:这对我来说很有用,因为我使用oracle,所有的“boolean”类型列都是带有“y”、“yes”、“n”和“no”类型值的字符串 那些使用数字1,0或其他格式的人可以通过对象“是”映射使其通用,并从resultset中获取对象并在此映射中查找它们,从而进一步改进它。希望这能在我这样的情况下帮助别人
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);
}
}
BeanPropertyRowMapper将值转换为0=false和1=true的布尔对象。刚试过这个,效果很好
有更多信息,以及使用OCCI的Java和C代码示例。老问题,但您可以执行以下操作
public void setIsActive(String active) {
this.active = "Y".equalsIgnoreCase(active);
}
正如Harikumar所指出的,BeanPropertyRowMapper实际上将0和1转换为布尔值。我找不到任何支持这一点的文档,但事实上这是当前的情况 因此,不需要扩展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;
}
您最好编写自己的自定义行映射器,而不是试图找出为什么Spring无法将布尔Y转换为布尔true。或者更改您的数据库映射。感谢您提供的信息。由于oracle没有布尔类型,如何更改数据库映射以解决此问题?我认为使用beanpropertyrowmapper有一定的价值,因为它有助于节省大量样板代码,特别是当您有很多域对象时。比起数据访问,它有助于花更多的时间和精力在业务逻辑上。是的,我刚刚注意到Oracle没有布尔类型。我一直在读BeanPropertyRowMapper的文章,很明显这本应该是,现在是?固定在。不过,您可能需要使用BeanPropertyRowMapper方法。您好@Sotirios,谢谢您指出了正确的方向。请参阅下面我的解决方案。欢迎反馈。最好在答案中添加信息,并保留链接以获取更多详细信息。
@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;
}