BeanUtils的使用
作用
用于简化[JavaBean]的封装,也就是简化数据的封装
工具类
** import org.springframework.beans.BeanUtils;**
对于JavaBean进行各种操作,比如对象,属性复制等等
1、
BeanUtil.copyProperties(input, clueEntity);
将对象input拷贝到对象clueEntity.
ConvertUtils
这个工具类的职能是在字符串和指定类型的实例之间进行转换。
- convert(Object value, Class<?> targetType)
- 将给定的value转换成指定的Class类型
1、将整型转为字符串类型
ConvertUtils.convert(123,String.class);
2、自定义类型转换
class MyConverter implements Converter {
private static SimpleDateFormat format;
public MyConverter(String pattern) {
format = new SimpleDateFormat(pattern);
}
@Override
public Object convert(Class type, Object value) {
if (value == null) {
return null;
}
if (value instanceof String) {
String tmp = (String) value;
if (tmp.trim().length() == 0) {
return null;
} else {
try {
return format.parse(tmp);
} catch (ParseException e) {
e.printStackTrace();
}
}
} else {
throw new ConversionException("not String");
}
return value;
}
}