Spring JDBC BeanPropertyRowMapper是否('Y','N')到布尔bean属性 [英] Spring JDBC BeanPropertyRowMapper yes no ('Y','N') to boolean bean properties

查看:45
本文介绍了Spring JDBC BeanPropertyRowMapper是否('Y','N')到布尔bean属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有一些字符串,整型和布尔型字段的类.我有为他们声明的getter和setter方法.

I have a class with some string, int and boolean fields. I have the getters and setters declared for them.

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和Oracle DB获取所有对象.

I am BeanPropertyRowMapper to get all the objects from and Oracle DB.

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

如果打开调试,我会看到:

If the debug is turned on I see:

[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

然后尝试进行活动映射失败.活动状态在数据库中定义为1字节CHAR,其值为'Y'或'N'.使用BeanPropertyRowMapper并将成功将"Y"和"N"之类的值成功转换为布尔值的最佳方法是什么?

And then it fails trying to map active. Active is defined as 1 byte CHAR in the DB with values as 'Y' or 'N'. What is the best way to use BeanPropertyRowMapper and successfully convert values such as 'Y', and 'N' to boolean?

推荐答案

所以我弄清楚了如何做到这一点.我通过一些自定义代码扩展了BeanPropertyRowMapper和处理程序布尔类型,然后将控件交给其余数据类型的beanpropertyrowmapper.

So I figured out how to do this. I extended BeanPropertyRowMapper and handler boolean types through some custom code before handing off the control to beanpropertyrowmapper for rest of the data types.

注意:它对我有用,因为我使用oracle,并且所有布尔"类型列都是带有"y","yes","n"和&"的字符串否"类型值.

Note: It works for me because I use oracle and all of the 'boolean' type columns are strings with 'y','yes','n' & 'no' type values.

那些使用数字1,0或其他格式的人可能会通过通过对象yes映射使其通用并从结果集中获取对象并在此映射中进行查找来进一步改进它.希望这对像我这样的人有帮助.

Those who use numerical 1,0 or other formats could potentially improve it further by making it generic through an object yes map and getting objects from resultset and looking them up in this map. Hope this helps someone else in a situation like mine.

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

这篇关于Spring JDBC BeanPropertyRowMapper是否('Y','N')到布尔bean属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆