2013-03-14 111 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; 
} 
相關問題