/** 根据传入的对象构建一个用于更新一条记录的SQL */ public String update(Object entity) { String sql = "UPDATE " + beanMeta.table + " SET "; for (Field field : beanMeta.fields) { Object fieldValue = Reflect.get(entity, field.name); if (!beanMeta.getId().name.equals(field.name)) { // 更新所有属性,fieldValue可能为null sql += field.column + "=" + wrap(fieldValue) + ","; } } Object id = Reflect.get(entity, beanMeta.getId().name); return sql.substring(0, sql.length() - 1) + " WHERE " + beanMeta.getId().column + "=" + wrap(id); }
/** 根据传入的对象构建一个插入一条记录的SQL */ public String insert(Object entity) { String columns = " (", values = " VALUES ("; for (Field field : beanMeta.fields) { Object fieldValue = Reflect.get(entity, field.name); columns += field.column + ","; values += wrap(fieldValue) + ","; } columns = columns.substring(0, columns.length() - 1) + ")"; values = values.substring(0, values.length() - 1) + ")"; return "INSERT INTO " + beanMeta.table + columns + values; }
/** 根据传入的对象构建一个插入一条记录的SQL,忽略为空的属性 */ public String insertIgnoreNull(Object entity) { String columns = " (", values = " VALUES ("; for (Field field : beanMeta.fields) { Object fieldValue = Reflect.get(entity, field.name); if (!Verify.isEmpty(fieldValue)) { // 略过为null的属性 columns += field.column + ","; values += wrap(fieldValue) + ","; } } columns = columns.substring(0, columns.length() - 1) + ")"; values = values.substring(0, values.length() - 1) + ")"; return "INSERT INTO " + beanMeta.table + columns + values; }
/** 解析SourceFloder下搜索到的文件名以config.xml结尾的文件,将其中配置的Bean返回 */ public List<Bean> getBeans() { List<String> fileList = Files.list(Files.root(), "^.*config.xml$", true); // 搜索以config.xml结尾的文件 log.info("Found " + fileList.size() + " Xml config files at " + Files.root()); List<Bean> beans = new ArrayList<Bean>(); for (String filePath : fileList) { NodeList beanNodes = (NodeList) Files.xpath(Files.build(filePath), "//bean", XPathConstants.NODESET); for (int length = (null == beanNodes ? -1 : beanNodes.getLength()), i = 0; i < length; i++) { Bean iocBean = new Bean(); // 一个新的Bean iocBean.name = Files.xpath(beanNodes.item(i), "@name", XPathConstants.STRING).toString(); String type = Files.xpath(beanNodes.item(i), "@class", XPathConstants.STRING).toString(); try { iocBean.type = Class.forName(type); } catch (ClassNotFoundException e) { // 配置文件中把类名写错了 throw new RuntimeException( "Class " + type + " not found , which is configured in " + filePath, e); } NodeList propertyNodes = (NodeList) Files.xpath(beanNodes.item(i), "property", XPathConstants.NODESET); for (int len = (null == propertyNodes ? -1 : propertyNodes.getLength()), m = 0; m < len; m++) { Field field = new Field(); // 一个新的Field field.name = (String) Files.xpath(propertyNodes.item(m), "@name", XPathConstants.STRING); field.type = Reflect.fieldType(iocBean.type, field.name); field.value = (String) Files.xpath(propertyNodes.item(m), "@value", XPathConstants.STRING); iocBean.fields.add(field); } beans.add(iocBean); log.debug("ADD BEAN: Xml " + iocBean.type.getName() + " " + iocBean.name); } } return beans; }