本文介绍了如何动态创建Java bean?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从Java中的表/列读取列后,如何动态创建bean/s(bean中的保留列数据类型)?

How to create bean/s(retaining column datatype in bean) dynamically after reading columns from table/s in java ?

推荐答案

更新先前的答案是尝试立即解决问题.因此,这不是FlexBean的最终代码.有关最终代码,请访问 https://github.com/ramazanpolat/flexbean

UPDATEPrevious answer was an attempt to solve the issue immediately.So it's not the final code for FlexBean.For the final code, visit https://github.com/ramazanpolat/flexbean

旧答案

我遇到了同样的问题,所以我写了FlexBean.

I had the same problem so I wrote FlexBean.

public class FlexBean {
    private List<String> propertyNames = new ArrayList<>();
    private Map<String, Object> propertyValueMap = new LinkedHashMap<>();
    private List<Type> propertyTypes  = new ArrayList<>();

    public FlexBean() {

    }

    public void setProperties(Map<String, Object> props){
        for (String propName:props.keySet()){
            addProperty(propName, props.get(propName));
        }
    }


    public void addPropertyNames(List<String> propertyNames){
        for (String name: propertyNames) {
            addProperty(name);
        }
    }

    public Collection<Object> getPropertyValues(){
        return propertyValueMap.values();
    }

    public List<String> getPropertyNames() {
        return propertyNames;
    }

    public Map<String, Object> getPropertyValueMap() {
        return propertyValueMap;
    }

    public List<Type> getPropertyTypes() {
        return propertyTypes;
    }

    public void addProperty(String propName, Type propType){
        propertyNames.add(propName);
        propertyTypes.add(propType);
    }

    public void addProperty(String propName){
        // default property type is String
        addProperty(propName, String.class);
    }

    public void addProperty(String propName, Object value){
        addProperty(propName);
        setProperty(propName, value);
    }

    public void addProperty(String propName, Type propType, Object value){
        addProperty(propName, propType);
        setProperty(propName, value);
    }

    public int getPropertyIndex(String propName){
        return propertyNames.indexOf(propName);
    }

    public Type getPropertyType(String propName){
        int index = getPropertyIndex(propName);
        return Iterables.get(propertyTypes,index);
    }

    public void setProperty(String propName, Object propValue){
        propertyValueMap.put(propName, propValue);
    }

    public Object getPropertyValue(String propName){
        return propertyValueMap.get(propName);
    }

    public <Any> Any getTypedPropertyValue(String propName){
        return (Any)((Any) propertyValueMap.get(propName));
    }

    public Object getProperty(int propIndex){
        return Iterables.get(propertyValueMap.entrySet(),propIndex).getValue();
    }
}

用法:

FlexBean flexBean = new FlexBean();

flexBean.addProperty("prop1", 1); // int inferred
flexBean.addProperty("prop2", "value2"); // string inferred
flexBean.addProperty("prop3", 0.1f); // float inferred

for (Object o: flexBean.getPropertyValues()) {
    System.out.println(o);
}

int prop1 = flexBean.getTypedPropertyValue("prop1");
String prop2 = flexBean.getTypedPropertyValue("prop2");
float prop3 = flexBean.getTypedPropertyValue("prop3");

这篇关于如何动态创建Java bean?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-12 06:17