我有一個包含字符串,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」之類的值轉換爲布爾值的最佳方式是什麼?
你最好不要寫自己的自定義的RowMapper不是試圖找出爲什麼春天不能將boolean Y轉換爲布爾值true。或者改變你的數據庫映射。 – 2013-03-14 14:56:21
感謝您的信息。如何更改數據庫映射來解決此問題,因爲oracle沒有布爾類型?我認爲使用beanpropertyrowmapper有一定的價值,因爲它有助於節省大量的樣板代碼,特別是如果你有很多域對象。它比數據訪問花費更多的時間並專注於業務邏輯。 – MickJ 2013-03-14 15:13:32
是的,我只注意到Oracle沒有布爾類型。我一直在閱讀'BeanPropertyRowMapper',顯然這應該是(而且是?)在[Spring 2.5.2](https://jira.springsource.org/browse/SPR-4355)中修復的。儘管你可能不得不使用BeanPropertyRowMapper方法。 – 2013-03-14 15:17:41