SSH通过xml格式进行表数据初始化的添加
来源:互联网 发布:java的md5加密 编辑:程序博客网 时间:2024/06/06 01:40
Service业务接口类:
package com.han.service;/** * @author hanwei * @date:2016-8-24 上午10:05:03 */public interface InitService extends GenericServ {/** * 通过xml初始化数据 * @param fileName */public void addInitEntityByXml(String fileName);}
Service业务接口实现类:
package com.han.service.impl;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.util.List;import javax.inject.Inject;import org.dom4j.Attribute;import org.dom4j.Document;import org.dom4j.DocumentException;import org.dom4j.Element;import org.dom4j.io.SAXReader;import org.springframework.beans.BeansException;import org.springframework.beans.factory.BeanFactory;import org.springframework.stereotype.Service;import com.han.dao.impl.GenericDaoImpl;import com.han.error.GlobalException;import com.han.service.InitService;import com.sun.org.apache.commons.beanutils.BeanUtils;/** * @author hanwei * @date:2016-8-24 上午10:05:31 */@Service("initService")public class InitServiceImpl extends GenericDaoImpl implements InitService {// 注入Spring工厂用于加载实体类@Injectprivate BeanFactory beanFactory;/** * 获得初始化xml文件数据对象Document * @param fileName 文件名称 * @return */private Document readDocument(String fileName) {Document document = null;SAXReader saxReader = null;try {// 创建xml解析对象saxReader = new SAXReader();// 获取文件路劲 该init.xml文件放在WebRoot/init/init.xmlString filePath = InitServiceImpl.class.getClassLoader().getResource("").toString().replace("WEB-INF/classes/", "");document = saxReader.read(filePath + "init/init.xml");} catch (DocumentException e) {e.printStackTrace();}return document;}/** * 解析XMl文件进行数据的添加处理 * @param fileName 文件名称 */@Overridepublic void addInitEntityByXml(String fileName) {Document readDocument = this.readDocument(fileName);Element root = null;try {if (readDocument != null) {// 获取根元素root = readDocument.getRootElement();// 获取包名String pname = root.attributeValue("package");// 根据当前元素节点进行寻找@SuppressWarnings("unchecked")List<Element> initEntitys = (List<Element>) root.selectNodes("/entitys/initEntity");for (Element e : initEntitys) {// 判断该实体是否存在 0:不存在进行添加 1:存在排除if (e.attributeValue("exist") == "1") {continue;} else {// 获取类名String cname = e.attributeValue("class");cname = pname + "." + cname;// 获取方法String method = e.attributeValue("method");// 获取所有的添加的实体@SuppressWarnings("unchecked")List<Element> entitys = (List<Element>) e.selectNodes("entity");addElements(cname, method, entitys);}}}} catch (Exception e) {e.printStackTrace();}}/** * 解析出每个元素节点的数据进行添加 * @param cname 包名.类名 com.han.model.OrgType * @param method 方法名 <initEntity exist="0" class="OrgType" method="orgTypeService.addOrgType"> * @param entitys 添加的元素集合 <entity name="学校" sn="XX"></entity> */private void addElements(String cname, String method, List<Element> entitys) {for (Element e : entitys) {addElement(cname, method, e);}}/** * 添加元素 * @param cname 包名.类名 * @param method 方法名 * @param e 添加的元素 */private void addElement(String cname, String method, Element e) {Object obj = null;Object service = null;Method m = null;try {// 通过反射创建对象obj = Class.forName(cname).newInstance();// 拆分method="orgTypeService.addOrgType"获取业务service类名和方法String[] ms = method.split("\\.");if (ms.length != 2) {throw new GlobalException("方法格式不正确!");}String sname = ms[0];String mname = ms[1];@SuppressWarnings("unchecked")List<Attribute> attributes = (List<Attribute>) e.attributes();for (Attribute attribute : attributes) {// 元素属性名称String name = attribute.getName();// 元素属性值String value = attribute.getValue();try {// 导入的是com.sun.org.apache.commons.beanutils.BeanUtils,进行对象属性的赋值操作// 第一个参数:对象,第二个参数:属性,第三个参数:值BeanUtils.copyProperty(obj, name, value);} catch (IllegalAccessException e1) {e1.printStackTrace();} catch (InvocationTargetException e1) {e1.printStackTrace();}}// 通过Spring工厂获取业务逻辑类serviceservice = beanFactory.getBean(sname);// 获取service方法 ,第一个参数:方法名称 ,第二个:参数类型m = service.getClass().getMethod(mname, obj.getClass());// 回调方法m.invoke(service, obj);} catch (BeansException e1) {e1.printStackTrace();} catch (SecurityException e1) {e1.printStackTrace();} catch (InstantiationException e1) {e1.printStackTrace();} catch (IllegalAccessException e1) {e1.printStackTrace();} catch (ClassNotFoundException e1) {e1.printStackTrace();} catch (NoSuchMethodException e1) {e1.printStackTrace();} catch (IllegalArgumentException e1) {e1.printStackTrace();} catch (InvocationTargetException e1) {e1.printStackTrace();}}}
测试类:
package com.han.test;import javax.inject.Inject;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.junit.After;import org.junit.Before;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.orm.hibernate4.SessionHolder;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import org.springframework.transaction.support.TransactionSynchronizationManager;import com.han.service.InitService;/** * @author hanwei * @date:2016-8-24 上午10:53:49 */@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration("/beans.xml")public class CURDTest {@Injectprivate InitService initService;@Injectprivate SessionFactory sessionFactory;@Beforepublic void setUp() {// 此时最好不要使用Spring的Transactional来管理,因为dbunit是通过jdbc来处理connection,再使用spring在一些编辑操作中会造成事务shisuSession s = sessionFactory.openSession();TransactionSynchronizationManager.bindResource(sessionFactory,new SessionHolder(s));}@Afterpublic void tearDown() {SessionHolder holder = (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);Session s = holder.getSession();s.flush();TransactionSynchronizationManager.unbindResource(sessionFactory);}@Testpublic void testXml() {initService.addInitEntityByXml("init.xml");}}
XML文件:
<?xml version="1.0" encoding="UTF-8"?><entitys package="com.han.model"> <initEntity exist="0" class="OrgType" method="orgTypeService.addOrgType"> <entity name="学校" sn="XX"></entity> <entity name="分校" sn="FX"></entity> <entity name="校领导办" sn="XLDB"></entity> <entity name="行政部门" sn="XZBM"></entity> <entity name="教学部门" sn="JXBM"></entity> <entity name="专业" sn="ZY"></entity> <entity name="班级" sn="BJ"></entity> </initEntity></entitys>
0 0
- SSH通过xml格式进行表数据初始化的添加
- 【php】怎样通过php程序返回xml格式的数据
- 一个AJAX列子,通过读取后台数据库数据转换为xml格式进行输出
- winfrom如何通过http来进行通信,并且通过传递json格式的数据可接受json格式的数据
- 转 winfrom如何通过http来进行通信,并且通过传递json格式的数据可接受json格式的数据
- N1.通过XML格式传输数据
- 通过dom4j进行数据读取xml文件进行数据的读取操作
- 通过自己编写的C语言程序获取lcm正常格式的初始化数据代码
- 通过XML数据与服务器进行通信
- 以树型目录化的数据库表数据通过递归转换成xml格式的xmldoc
- 调用webservice,解析返回数据为xml格式的字符串,进行数据绑定
- 通过XSD文件对XML文件进行格式验证
- 数据库全备无法正常进行,通过在出现空块的表上添加数据的手段解决
- 向空XML自己添加数据成XML格式
- 解析XML格式的数据
- 通过simpleXML解析XML格式的数据 (两种方法)
- SSH返回Json格式的数据
- SSH返回Json格式的数据
- 企业级Nginx服务基础到架构优化详解--25条
- 达梦数据库如何创建表和表空间
- ABAP 面试大全
- iOS 解决自定义导航栏返回按钮 系统右滑返回手势失效问题 最简单方法
- 准备ipa发布app
- SSH通过xml格式进行表数据初始化的添加
- 操作系统学习笔记
- HTTP POST GET详解 --讲得比较清楚
- camel-Case (骆驼拼写法)
- EMC-EMI-EMS
- hdu4714 Tree2cycle(树形dp)
- 人脸对齐算法总结
- “J.U.C”:ReentrantLock之二lock方法分析 (r)
- 单例模式