| 将XML结点转换成JAVABEAN并存入数据库 |
|
| |
|
|
ap = this.getCollectionPropsMap();
for (Iterator it = collectionPropsMap.keySet().iterator(); it.hasNext();) {
try {
String attName = (String) it.next();
Collection coll = (Collection) PropertyUtils.getProperty(this,
attName);
Class attType = (Class) collectionPropsMap.get(attName);
Iterator collElementsIt = this.getCollectionElementIterator(
xmlElementParent, attName);
// xmlElementParent.elementIterator(attName);
while (collElementsIt.hasNext()) {
Element collElmt = (Element) collElementsIt.next();
BusiNode sinlgeAtt = (BusiNode) attType.newInstance();
sinlgeAtt.parseFromXML(collElmt);
coll.add(sinlgeAtt);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
/**
* 从XML中解析出结点。此方法可能抛出RumtimeException
*/
public void parseFromXML(Element rootElement) {
this.parseAttributesFromXml(rootElement);
this.parseAtomicElementFromXml(rootElement);
this.parseBusiNodeElementFromXml(rootElement);
this.parseCollectionPropsFromXml(rootElement);
}
}
/**
* 入库
* JdbcUtil,MyUtils的代码欠奉
*
*/
public class BusiNodeDAO {
private Long saveBusiNode(BusiNode node, Long parentNodeId) {
// 先存储原子属性
Long id = saveBareBusiNode(node, parentNodeId);
// 再存储类集属性
Map collectionPropsMap = node.getCollectionPropsMap();
for (Iterator it = collectionPropsMap.keySet().iterator(); it.hasNext();) {
String attName = (String) it.next();
Collection coll = null;
try {
coll = (Collection) PropertyUtils.getProperty(node, attName);
} catch (Exception e) {
throw new RuntimeException("编码错误");
}
for (Iterator iitt = coll.iterator(); iitt.hasNext();) {
BusiNode subNode = (BusiNode) iitt.next();
saveBusiNode(subNode, id);
}
}
// 最后存储所有BusiNode属性
Iterator iitt = node.getBusiNodePropNames().iterator();
while (iitt.hasNext()) {
BusiNode subNode = null;
try {
subNode = (BusiNode) PropertyUtils.getProperty(node,
(String) iitt.next());
} catch (Exception e) {
throw new RuntimeException("编码错误");
}
if (subNode != null) {
saveBusiNode(subNode, id);
}
}
return id;
}
/**
* 插入某个BusiNode的根结点,此方法可能抛出RuntimeException
*
* @param node
* @return
*/
private Long saveBareBusiNode(BusiNode node, Long parentNodeId) {
StringBuffer sbForSql = new StringBuffer();
List paramValues = new ArrayList();
genInsertSqlAndParam(node, parentNodeId, node.getAtomicPropNames(),
sbForSql, paramValues);
return new Long(JdbcUtil.queryForLong(
sbForSql.toString(), paramValues.toArray()));
}
/**
* 生成某个结点的插入语句和paramValues数组,此方法可能抛出RuntimeException
*
* @param node
* @param columnNames
* @param sbForSql
* @param paramValues
*/
private void genInsertSqlAndParam(BusiNode node, Long parentNodeId,
List columnNames, StringBuffer sbForSql, List paramValues) {
sbForSql.append(" insert into ");
sbForSql.append(MyUtils.getClassBareName(node.getClass()));
List cns = new ArrayList();
cns.addAll(columnNames);
cns.add("parentNodeId");
sbForSql.append(MyUtils.encloseWithCurve(MyUtils
.joinCollectionStrings(cns, ",")));
sbForSql.append(" values ");
List qms = new ArrayList(); // 问号
for (Iterator it = columnNames.iterator(); it.hasNext();) {
qms.add("?");
String cn = (String) it.next();
try {
paramValues.add(PropertyUtils.getProperty(node, cn));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
qms.add("?"); // parentNodeId
paramValues.add(parentNodeId);
sbForSql.append(MyUtils.encloseWithCurve(MyUtil
.joinCollectionStrings(qms, ",")));
sbForSql.append(";select @@identity");
}
}
上一页 [1] [2]
 |
频道声明:本频道的文章除部分特别声明禁止转载的专稿外,可以自由转载.但请务必注明出出处和原始作者 文章版权归本频道与文章作者所有.对于被频道转载文章的个人和网站,我们表示深深的谢意。
| 原始作者:佚名 |
录入时间:2007-7-13 15:45:41 |
| 信息来源:不详 |
投稿信箱:itqoo@126.com |
|
|
 |
|
|
|
| 教程录入:itqoo 责任编辑:itqoo |
|
上一个教程: J2EE实用技巧:提升JSP应用程序的绝招
下一个教程: Java资料:Swing中的事件处理详细资料 |
| 【字体:小 大】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口】 |