8

string、int、および boolean フィールドを持つクラスがあります。私はゲッターとセッターを宣言しました。

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

そして、アクティブなマップに失敗します。アクティブは、DB で 1 バイトの CHAR として定義され、値は「Y」または「N」です。BeanPropertyRowMapper を使用して、「Y」や「N」などの値をブール値に正常に変換する最良の方法は何ですか?

4

4 に答える 4

12

だから私はこれを行う方法を考え出しました。残りのデータ型のコントロールをbeanpropertyrowmapperに渡す前に、いくつかのカスタムコードを使用してBeanPropertyRowMapperとハンドラーのブール型を拡張しました。

注:私はoracleを使用しており、すべての「ブール」型の列は「y」、「yes」、「n」、「no」型の値を持つ文字列であるため、機能します。

数値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);
    }
}
于 2013-03-14T19:20:01.890 に答える
3

BeanPropertyRowMapperBooleanと で値をオブジェクトに変換し0=falseます1=true。これを試しただけで動作します。

このブログ投稿には、より多くの情報と、OCCI を使用した Java および C のコード例が記載されています。

于 2015-02-27T15:25:39.563 に答える