bean拷贝
bean拷贝有不少开源框架都提供了工具类,比如:
org.springframework.beans.BeanUtils
org.apache.commons.beanutils.PropertyUtils
net.sf.cglib.beans.BeanCopier
性能上来讲最佳的是BeanCopier。
如果在拷贝时,目标对象所属类上有注解,用cglib拷贝过去后,是否会受到影响?答案是不会影响。
列表数组可变参数
一个方法接受的参数是可变参数,如果有一个列表List,能否直接传入?
这是不能的,编译不通过。
把数组先转为可变参数,就可以直接传入了。
下边是测试代码:
public class T {
/**
* <p>Title: main</p>
* <p>Description: </p>
* @param args
* @throws NoSuchMethodException
* @throws InvocationTargetException
* @throws IllegalAccessException
*/
public static void main(String[] args) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
// TODO Auto-generated method stub
DeptDetailVO vo = new DeptDetailVO();
vo.setDeptName("科室");
DeptVO d1 = new DeptVO();
d1.setDeptName("药房1");
DeptVO d2 = new DeptVO();
d1.setDeptName("药房1");
d2.setDeptName("药房2");
List<DeptVO> list = new ArrayList<DeptVO>();
list.add(d1);
list.add(d2);
vo.setPharmacyList(list);
DeptDetailModel m = new DeptDetailModel();
// StopWatch s = new StopWatch();
// s.start();
// BeanUtils.copyProperties(vo, m, (String[]) null);
// s.stop();
// System.out.println(s.getTotalTimeMillis());
// org.apache.commons.lang.time.StopWatch s = new org.apache.commons.lang.time.StopWatch();
// s.start();
long start = System.currentTimeMillis();
// BeanUtils.copyProperties(vo, m, (String[]) null);
// PropertyUtils.copyProperties(m, vo);
// final BeanCopier copier = BeanCopier.create(vo.getClass(), m.getClass(), false);
// copier.copy(vo, m, null);
BeanCopierUtil.copy(vo, m);
//BeanMapUtil.copyPropertiesIgnoreNull(vo, m);
long end = System.currentTimeMillis();
// s.stop();
// System.out.println(s.getTime());
System.out.println(end-start);
System.out.println(vo);
System.out.println(m);
//取对应的表名
// String tableName = getTableName(m);
List<Map> list2 = getColumns(m);
// System.out.println(tableName);
System.out.println(list2);
ss("12","2","2");
String[] ddd = {"trtr","t22rtr","fsdf"};
ss(ddd);
}
/**
* <p>Title: getColumns</p>
* <p>Description: </p>
* @param m
* @return
*/
private static List<Map> getColumns(DeptDetailModel entity) {
List<Map> list = new ArrayList<Map>();
//取出所有的属性(包括父类
Field[] fields = new Field[0];//entity.getClass().getDeclaredFields();
for(Class<?> clazz = entity.getClass() ; clazz != Object.class ; clazz = clazz.getSuperclass())
{
if(clazz.getDeclaredFields() != null && clazz.getDeclaredFields().length > 0)
{
Field[] fieldsTemp = new Field[fields.length+clazz.getDeclaredFields().length];
System.arraycopy(clazz.getDeclaredFields(), 0, fieldsTemp, 0, clazz.getDeclaredFields().length);
System.arraycopy(fields, 0, fieldsTemp, clazz.getDeclaredFields().length, fields.length);
fields = fieldsTemp;
}
}
for(Field f : fields)
{
Map<String, Object> map = new HashMap<String, Object>();
Column column = f.getAnnotation(Column.class);
if(column != null)
{
try
{
String methodName = "get" + f.getName().substring(0,1).toUpperCase() + f.getName().substring(1, f.getName().length());
Method m = entity.getClass().getMethod(methodName);
if(m.invoke(entity) == null)
{
continue;
}
if(column.primaryKeyId())
{
map.put("isPrimaryKeyId", true);
}
map.put("name", column.name());
map.put("valueName", f.getName());
list.add(map);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
return list;
}
/**
* <p>Title: getTableName</p>
* <p>Description: </p>
* @param m
* @return
*/
private static String getTableName(DeptDetailModel m) {
//取对应的表名
Table table = m.getClass().getAnnotation(Table.class);
return table.name();
}
public static void ss(String ... keys) {
System.out.println(keys.length);
String[] s = keys;
System.out.println(s);
}