2013-03-14 113 views
2

我有一个包含字符串,int和布尔字段的类。我有为他们宣布的获得者和接受者。Spring Bean BeanPropertyRowMapper是否('Y','N')为布尔bean属性

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; 
    } 


} 

我是BeanPropertyRowMapper从Oracle DB获取所有对象。

@Override 
public List<Destination> getAll() { 
    List<SomeClass> objs = jdbcTemplate.query(
       myQuery, new BeanPropertyRowMapper<SomeClass>(SomeClass.class)); 
    return objs; 
} 

如果调试已打开我看到:

[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 

然后它没有试图映射活跃。 Active被定义为DB中1个字节的CHAR,其值为'Y'或'N'。使用BeanPropertyRowMapper并成功将诸如“Y”和“N”之类的值转换为布尔值的最佳方式是什么?

+0

你最好不要写自己的自定义的RowMapper不是试图找出为什么春天不能将boolean Y转换为布尔值true。或者改变你的数据库映射。 – 2013-03-14 14:56:21

+0

感谢您的信息。如何更改数据库映射来解决此问题,因为oracle没有布尔类型?我认为使用beanpropertyrowmapper有一定的价值,因为它有助于节省大量的样板代码,特别是如果你有很多域对象。它比数据访问花费更多的时间并专注于业务逻辑。 – MickJ 2013-03-14 15:13:32

+1

是的,我只注意到Oracle没有布尔类型。我一直在阅读'BeanPropertyRowMapper',显然这应该是(而且是?)在[Spring 2.5.2](https://jira.springsource.org/browse/SPR-4355)中修复的。尽管你可能不得不使用BeanPropertyRowMapper方法。 – 2013-03-14 15:17:41

回答

7

所以我想出了如何做到这一点。在将控制权交给beanpropertyrowmapper以处理其余数据类型之前,我通过一些自定义代码扩展了BeanPropertyRowMapper和处理程序布尔类型。

注意:它适用于我,因为我使用oracle,所有'boolean'类型列都是'y','yes','n'&'no'类型值的字符串。

那些使用数字1,0或其他格式的人可以通过使其通过对象yes映射并通过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); 
    } 
} 
+0

看起来不错,你为什么不立即返回价值? – 2013-03-14 19:41:11

+0

谢谢。清理。 – MickJ 2013-03-14 20:01:00

2

BeanPropertyRowMapper将值转换成一个Boolean对象与0=false1=true。刚试过这个,它的工作原理。

This blog post有更多信息,以及Java和C与OCCI中的代码示例。

+0

最好将信息添加到答案中,并保留链接以获取更多详细信息。 – Ram 2015-02-27 15:32:20

0

老问题,但你可以这样做

public void setIsActive(String active) { 
    this.active = "Y".equalsIgnoreCase(active); 
} 
0

正如哈库马尔指出,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; 
} 
相关问题