Search in sources :

Example 1 with Property

use of org.eweb4j.orm.dao.config.bean.Property in project eweb4j-framework by laiweiwei.

the class DAOConfigBeanCreator method getDAOBean.

public static DBInfoConfigBean getDAOBean() {
    DBInfoConfigBean dcb = new DBInfoConfigBean();
    List<Property> properties = new ArrayList<Property>();
    Property p = new Property();
    p.setKey("");
    p.setValue("");
    properties.add(p);
    dcb.setProperty(properties);
    return dcb;
}
Also used : DBInfoConfigBean(org.eweb4j.orm.dao.config.bean.DBInfoConfigBean) ArrayList(java.util.ArrayList) Property(org.eweb4j.orm.dao.config.bean.Property)

Example 2 with Property

use of org.eweb4j.orm.dao.config.bean.Property in project eweb4j-framework by laiweiwei.

the class DataSourceCreator method create.

public static DataSource create(final DBInfoConfigBean dbInfo) throws Exception {
    ConfigBean cb = (ConfigBean) SingleBeanCache.get(ConfigBean.class.getName());
    Class<?> cls = Thread.currentThread().getContextClassLoader().loadClass(cb.getOrm().getDataSource());
    DataSource ds = (DataSource) cls.newInstance();
    List<Property> properties = dbInfo.getProperty();
    // 通过反射将配置信息注入到数据源对象中
    for (Property property : properties) {
        String name = property.getKey();
        String value = property.getValue();
        if (null == name || name.trim().length() == 0)
            continue;
        if (null == value || value.trim().length() == 0)
            continue;
        ReflectUtil ru2 = new ReflectUtil(ds);
        Method m2 = ru2.getSetter(name);
        if (m2 == null)
            continue;
        Class<?> clazz = m2.getParameterTypes()[0];
        if (int.class.isAssignableFrom(clazz) || Integer.class.isAssignableFrom(clazz) || long.class.isAssignableFrom(clazz) || Long.class.isAssignableFrom(clazz))
            m2.invoke(ds, Integer.parseInt(value));
        else if (float.class.isAssignableFrom(clazz) || Float.class.isAssignableFrom(clazz) || double.class.isAssignableFrom(clazz) || Double.class.isAssignableFrom(clazz))
            m2.invoke(ds, Float.parseFloat(value));
        else if (boolean.class.isAssignableFrom(clazz) || Boolean.class.isAssignableFrom(clazz))
            m2.invoke(ds, Boolean.parseBoolean(value));
        else
            m2.invoke(ds, value);
    }
    return ds;
}
Also used : ReflectUtil(org.eweb4j.util.ReflectUtil) DBInfoConfigBean(org.eweb4j.orm.dao.config.bean.DBInfoConfigBean) ConfigBean(org.eweb4j.config.bean.ConfigBean) Method(java.lang.reflect.Method) Property(org.eweb4j.orm.dao.config.bean.Property) DataSource(javax.sql.DataSource)

Aggregations

DBInfoConfigBean (org.eweb4j.orm.dao.config.bean.DBInfoConfigBean)2 Property (org.eweb4j.orm.dao.config.bean.Property)2 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 DataSource (javax.sql.DataSource)1 ConfigBean (org.eweb4j.config.bean.ConfigBean)1 ReflectUtil (org.eweb4j.util.ReflectUtil)1