1.首先是我的项目结构以及我所引入的依赖:
2.引入的依赖:jdk用的是17
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--做xml的解析-->
<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
3.测试代码如下:
public class TestMybatis {
@Test
public void testPorerXml() throws DocumentException {
SAXReader saxReader=new SAXReader();
InputStream resourceAsStream = ClassLoader.getSystemClassLoader().getResourceAsStream("mybatis-conf.xml");
Document read = saxReader.read(resourceAsStream);
System.out.println("read = " + read);
String xPath="/configuration/environments";
Element node =(Element) read.selectSingleNode(xPath);
String node1 = node.attributeValue("default");
System.out.println("node1 = " + node1);
String xPath1="/configuration/environments/environment[@id='"+node1+"']";
System.out.println("xPath1 = " + xPath1);
Element ele = (Element) read.selectSingleNode(xPath1);
System.out.println("ele = " + ele);
Element transactionManager = ele.element("transactionManager");
String transactionManager1 = transactionManager.attributeValue("type");
System.out.println("transactionManager1 = " + transactionManager1);
Element dataSource = ele.element("dataSource");
String pooled = dataSource.attributeValue("type");
System.out.println("pooled = " + pooled);
List<Element> elements = dataSource.elements();
elements.forEach(elem->{
String name = elem.attributeValue("name");
String value = elem.attributeValue("value");
System.out.println("name = " + name+"-----"+"value = " + value);
});
String xpaht2="//mapper";
List<Element> list = read.selectNodes(xpaht2);
for (Element l:list) {
System.out.println("l = " + l);
String resource = l.attributeValue("resource");
System.out.println("resource = " + resource);
}
}
@Test
public void testAdminXml() throws DocumentException {
SAXReader saxReader=new SAXReader();
InputStream resourceAsStream = ClassLoader.getSystemClassLoader().getResourceAsStream("AdminMapper.xml");
Document document = saxReader.read(resourceAsStream);
String path="/mapper";
Element element = (Element) document.selectSingleNode(path);
String namespace = element.attributeValue("namespace");
System.out.println("namespace = " + namespace);
List<Element> elements = element.elements();
elements.forEach(elem->{
String id = elem.attributeValue("id");
System.out.println("id = " + id);
String useGeneratedKeys = elem.attributeValue("useGeneratedKeys");
System.out.println("useGeneratedKeys = " + useGeneratedKeys);
String keyProperty = elem.attributeValue("keyProperty");
System.out.println("keyProperty = " + keyProperty);
String resultType = elem.attributeValue("resultType");
System.out.println("resultType = " + resultType);
String resultMapper = elem.attributeValue("resultMap");
System.out.println("resultMapper = " + resultMapper);
String textTrim = elem.getTextTrim();
System.out.println("textTrim = " + textTrim);
String s = textTrim.replaceAll("#\\{[0-9A-Za-z_$]*}", "?");
System.out.println("s = " + s);
});
}
}
4.说说每一个方法用来做什么,具体的作用:
SAXReader saxReader=new SAXReader(); InputStream resourceAsStream = ClassLoader.getSystemClassLoader().getResourceAsStream("mybatis-conf.xml"); Document read = saxReader.read(resourceAsStream); 0.SAX解析器读取位于类路径下的名为"mybatis-conf.xml"的XML文件,并将其解析为一个Document对象。其中,SAXReader是一个SAX解析器, InputStream resourceAsStream = ClassLoader.getSystemClassLoader().getResourceAsStream("mybatis-conf.xml")用于获取mybatis-conf.xml文件的输入流, Document read = saxReader.read(resourceAsStream)则是将输入流解析为一个Document对象:
1. Element node =(Element) read.selectSingleNode(xPath);:根据XPath表达式获取mybatis-conf.xml文件中的根节点,即<configuration>节点。
2. String node1 = node.attributeValue("default");:获取<configuration>节点的default属性值,该属性值用于指定默认的环境(即数据库)。
3. String xPath1="/configuration/environments/environment[@id='"+node1+"']";:根据default属性值构造XPath表达式,用于获取指定id的<environment>节点。
4. Element ele = (Element) read.selectSingleNode(xPath1);:根据XPath表达式获取指定id的<environment>节点。
5. Element transactionManager = ele.element("transactionManager");:获取<environment>节点下的<transactionManager>节点。
6. String transactionManager1 = transactionManager.attributeValue("type");:获取<transactionManager>节点的type属性值,即事务管理器的类型。
7. Element dataSource = ele.element("dataSource");:获取<environment>节点下的<dataSource>节点,即数据库连接池配置信息。
8. String pooled = dataSource.attributeValue("type");:获取<dataSource>节点的type属性值,即数据库连接池的类型。
9. List<Element> elements = dataSource.elements();:获取<dataSource>节点下的所有子节点,即数据库连接池的配置信息。
10. elements.forEach(elem->{}:遍历数据库连接池的配置信息,获取每个子节点的name和value属性值。
11. String xpaht2="//mapper";:构造XPath表达式,用于获取所有<mapper>节点。
12. List<Element> list = read.selectNodes(xpaht2);:根据XPath表达式获取所有<mapper>节点。
13. for (Element l:list) {}:遍历所有<mapper>节点,获取每个节点的resource属性值,即mapper文件的路径。