mybatis 工具类MetaObject(将Propertie
2018-12-28 本文已影响0人
全都是泡沫啦
import org.apache.ibatis.cache.CacheException;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;
import java.util.Map;
import java.util.Properties;
/**
* @author paul
* @description
* @date 2018/12/27
*/
public class TestSystemMetaObject {
private int age;
private String name;
@Override
public String toString() {
return "TestSystemMetaObject{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}
public static void setter(Object bean, Properties properties){
if (properties != null) {
MetaObject metaCache = SystemMetaObject.forObject(bean);
for (Map.Entry<Object, Object> entry : properties.entrySet()) {
String name = (String) entry.getKey();
String value = (String) entry.getValue();
if (metaCache.hasSetter(name)) {
Class<?> type = metaCache.getSetterType(name);
if (String.class == type) {
metaCache.setValue(name, value);
} else if (int.class == type
|| Integer.class == type) {
metaCache.setValue(name, Integer.valueOf(value));
} else if (long.class == type
|| Long.class == type) {
metaCache.setValue(name, Long.valueOf(value));
} else if (short.class == type
|| Short.class == type) {
metaCache.setValue(name, Short.valueOf(value));
} else if (byte.class == type
|| Byte.class == type) {
metaCache.setValue(name, Byte.valueOf(value));
} else if (float.class == type
|| Float.class == type) {
metaCache.setValue(name, Float.valueOf(value));
} else if (boolean.class == type
|| Boolean.class == type) {
metaCache.setValue(name, Boolean.valueOf(value));
} else if (double.class == type
|| Double.class == type) {
metaCache.setValue(name, Double.valueOf(value));
} else {
throw new CacheException("Unsupported property type for cache: '" + name + "' of type " + type);
}
}
}
}
}
public static void main(String[] args) throws Exception {
//插件
Properties properties = new Properties();
properties.setProperty("age","18");
properties.setProperty("name","paulzhangcc");
TestSystemMetaObject testSystemMetaObject = new TestSystemMetaObject();
setter(testSystemMetaObject,properties);
System.out.println(testSystemMetaObject);
}
}