public void visit(Attribute attr) { try { if (null == attr.getText() || attr.getText().isEmpty()) { BeanUtils.copyProperty(object, attr.getName(), attr.getValue()); } else { BeanUtils.copyProperty(object, attr.getName(), attr.getText()); } } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } }
/** * copy * * @param dest * @param orig * @param ignoreNull 忽略null,即orig为空的字段不做copy * @author Joeson */ public static void copy(Object dest, Object orig, boolean ignoreNull) { if (null == dest || null == orig) { return; } Class destClz = dest.getClass(); Class origClz = orig.getClass(); Field[] destFields = destClz.getDeclaredFields(); Field[] origFields = origClz.getDeclaredFields(); for (Field origField : origFields) { try { String fieldName = origField.getName(); origField.setAccessible(true); if (!ignoreNull || null != origField.get(orig)) { for (Field destField : destFields) { if (destField.getName().equals(origField.getName()) && destField.getType().equals(origField.getType())) { destField.setAccessible(true); Object o = origField.get(orig); BeanUtils.copyProperty(dest, fieldName, o); break; } } } } catch (Exception e) { logger.error("copyNonNull exception.", e); throw new RuntimeException("copyNonNull exception.." + e.getMessage()); } } }
public void visit(Element node) { try { BeanUtils.copyProperty(object, node.getName(), node.getText()); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } }
public List<Object> readExcel(Workbook wb, Class clz, int readLine, int tailLine) { Sheet sheet = wb.getSheetAt(0); // 取第一张表 List<Object> objs = null; try { Row row = sheet.getRow(readLine); // 开始行,主题栏 objs = new ArrayList<Object>(); Map<Integer, String> maps = getHeaderMap(row, clz); // 设定对应的字段顺序与方法名 if (maps == null || maps.size() <= 0) throw new RuntimeException("要读取的Excel的格式不正确,检查是否设定了合适的行"); // 与order顺序不符 for (int i = readLine + 1; i <= sheet.getLastRowNum() - tailLine; i++) { // 取数据 row = sheet.getRow(i); Object obj = clz.newInstance(); // 调用无参结构 for (Cell c : row) { int ci = c.getColumnIndex(); String mn = maps.get(ci).substring(3); // 消除get mn = mn.substring(0, 1).toLowerCase() + mn.substring(1); Map<String, Object> params = new HashMap<String, Object>(); if (!"enterDate".equals(mn)) c.setCellType(Cell.CELL_TYPE_STRING); // 设置单元格格式 else c.setCellType(Cell.CELL_TYPE_NUMERIC); if (this.getCellValue(c).trim().equals("是")) { BeanUtils.copyProperty(obj, mn, 1); } else if (this.getCellValue(c).trim().equals("否")) { BeanUtils.copyProperty(obj, mn, 0); } else BeanUtils.copyProperty(obj, mn, this.getCellValue(c)); } objs.add(obj); } } catch (InstantiationException e) { e.printStackTrace(); logger.error(e); } catch (IllegalAccessException e) { e.printStackTrace(); logger.error(e); } catch (InvocationTargetException e) { e.printStackTrace(); logger.error(e); } catch (NumberFormatException e) { e.printStackTrace(); logger.error(e); } return objs; }
public OperatorView mapRow(ResultSet rs, int arg1) throws SQLException { // TODO Auto-generated method stub OperatorView tmp = new OperatorView(); for (int i = 0; i < OperatorView.JDBC_FIELDS.length; i++) { try { BeanUtils.copyProperty(tmp, OperatorView.JDBC_FIELDS[i], rs.getObject(i + 1)); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return tmp; }
private void addEntity( String pkg, Element entityElement, Object parentEntity, String callString) { try { // 根据包名和类名,得到全路径类名 String clz = pkg + "." + entityElement.attributeValue("class"); // 根据全路径类名,创建实体对象 Object entity = Class.forName(clz).newInstance(); // 给entity对象赋值 // 即提取出当前Element中的所有属性,并用反射机制给entity对象赋值 Iterator iterator = entityElement.attributeIterator(); while (iterator.hasNext()) { Attribute attribute = (Attribute) iterator.next(); String propertyName = attribute.getName(); if (!propertyName.equals("class") && !propertyName.equals("call") && !propertyName.equals("parentName")) { String propertyValue = attribute.getValue(); // 给entity相应的属性赋值,这里使用的是apache-commons-beanutils工具包,所以需要加入这个依赖包 BeanUtils.copyProperty(entity, propertyName, propertyValue); } } /** * 提取出当前Element下面所有的非entity元素,这些元素也当作是本Entity属性的一部分 举个例子: <entity class="Form" name="请假单" * content="这里是请假单的内容" ...></entity> 像上面这样定义当然是可以的,但是有些属性的值可能会包含特殊字符,或者内容比较庞大,所以,可能 * 像下面这样来定义更加合理,如: <entity class="Form" name="请假单" ...> <content> <![CDATA[ * * <p>这是一个请假单 请假者姓名:<input type="text" name="leaverName" > .... ]]> </content> </entity> * 这样,比较复杂的文本属性,也可以定义 */ // 查找本元素下所有名称不是entity的元素 List subPropertyElements = entityElement.selectNodes("*[name()!='entity']"); if (subPropertyElements != null && !subPropertyElements.isEmpty()) for (Iterator iterator2 = subPropertyElements.iterator(); iterator2.hasNext(); ) { Element e = (Element) iterator2.next(); String propertyName = e.getName(); String propertyValue = e.getText(); BeanUtils.copyProperty(entity, propertyName, propertyValue); } // 判断parentEntity是否为空,如果不是空,则给parent对象赋值 if (parentEntity != null) { String parentName = entityElement.attributeValue("parentName"); if (parentName == null) { // 如果不配置父属性的名称,默认为parent parentName = "parent"; } parentName = StringUtils.capitalize(parentName); // 首字母变成大写 String setParentName = "set" + parentName; Method[] ms = entity.getClass().getMethods(); for (Method m : ms) { if (m.getName().equals(setParentName)) { m.invoke(entity, parentEntity); } } } // 要调用哪个服务对象的哪个方法 String call = entityElement.attributeValue("call"); if (call != null) { callString = call; } if (callString == null) { throw new RuntimeException("没有找到call属性,无法获知要调用哪个服务对象的哪个方法!请配置call属性!"); } // 得到服务对象的ID String serviceId = callString.substring(0, callString.indexOf(".")); // 得到要调用的方法名称 String methodName = callString.substring(callString.indexOf(".") + 1); // 通过BeanFactory得到服务对象 Object service = factory.getBean(serviceId); // 得到service对象的所有方法 Method[] ms = service.getClass().getMethods(); for (Method m : ms) { if (m.getName().equals(methodName)) { // 调用其中我们想要调用的方法 m.invoke(service, entity); } } // 判断当前entity下面是否还有其他的entity元素 List subEntityElements = entityElement.elements("entity"); for (Iterator iterator2 = subEntityElements.iterator(); iterator2.hasNext(); ) { Element e = (Element) iterator2.next(); // 递归插入本entity元素下面的其它entity对象 addEntity(pkg, e, entity, callString); } } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } }