码迷,mamicode.com
首页 > 其他好文 > 详细

拉钩:解析xml

时间:2020-04-01 00:57:22      阅读:73      评论:0      收藏:0      [点我收藏+]

标签:lis   sqlt   oid   mic   this   type   src   word   col   

技术图片

 

 技术图片

 技术图片

 

 

 

 1 public class XMLConfigBuilder {
 2 
 3     private Configuration configuration;
 4 
 5     public XMLConfigBuilder() {
 6         this.configuration = new Configuration();
 7     }
 8 
 9     /**
10      * 该方法就是使用dom4j对配置文件进行解析,封装Configuration
11      */
12     public Configuration parseConfig(InputStream inputStream) throws DocumentException, PropertyVetoException {
13 
14         Document document = new SAXReader().read(inputStream);
15         //<configuration>
16         Element rootElement = document.getRootElement();
17         List<Element> list = rootElement.selectNodes("//property");
18         Properties properties = new Properties();
19         for (Element element : list) {
20             String name = element.attributeValue("name");
21             String value = element.attributeValue("value");
22             properties.setProperty(name,value);
23         }
24 
25         ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
26         comboPooledDataSource.setDriverClass(properties.getProperty("driverClass"));
27         comboPooledDataSource.setJdbcUrl(properties.getProperty("jdbcUrl"));
28         comboPooledDataSource.setUser(properties.getProperty("username"));
29         comboPooledDataSource.setPassword(properties.getProperty("password"));
30 
31         configuration.setDataSource(comboPooledDataSource);
32 
33         //mapper.xml解析: 拿到路径--字节输入流---dom4j进行解析
34         List<Element> mapperList = rootElement.selectNodes("//mapper");
35 
36         for (Element element : mapperList) {
37             String mapperPath = element.attributeValue("resource");
38             InputStream resourceAsSteam = Resources.getResourceAsSteam(mapperPath);
39             XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(configuration);
40             xmlMapperBuilder.parse(resourceAsSteam);
41         }
42         return configuration;
43     }
44 
45 
46 }
 1 package com.lagou.config;
 2 
 3 import com.lagou.pojo.Configuration;
 4 import com.lagou.pojo.MappedStatement;
 5 import org.dom4j.Document;
 6 import org.dom4j.DocumentException;
 7 import org.dom4j.Element;
 8 import org.dom4j.io.SAXReader;
 9 
10 import java.io.InputStream;
11 import java.util.List;
12 
13 public class XMLMapperBuilder {
14 
15     private Configuration configuration;
16 
17     public XMLMapperBuilder(Configuration configuration) {
18         this.configuration =configuration;
19     }
20 
21     public void parse(InputStream inputStream) throws DocumentException {
22 
23         Document document = new SAXReader().read(inputStream);
24         Element rootElement = document.getRootElement();
25 
26         String namespace = rootElement.attributeValue("namespace");
27 
28         List<Element> list = rootElement.selectNodes("//select");
29         for (Element element : list) {
30             String id = element.attributeValue("id");
31             String resultType = element.attributeValue("resultType");
32             String paramterType = element.attributeValue("paramterType");
33             String sqlText = element.getTextTrim();
34             MappedStatement mappedStatement = new MappedStatement();
35             mappedStatement.setId(id);
36             mappedStatement.setResultType(resultType);
37             mappedStatement.setParamterType(paramterType);
38             mappedStatement.setSql(sqlText);
39             String key = namespace+"."+id;
40             configuration.getMappedStatementMap().put(key,mappedStatement);
41 
42         }
43 
44     }
45 
46 
47 }

 

拉钩:解析xml

标签:lis   sqlt   oid   mic   this   type   src   word   col   

原文地址:https://www.cnblogs.com/xjatj/p/12609741.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!