1. 代码 32-34
test代码在com.powernode.xml.test
ParseXMLByDom4jTest
package com.powernode.xml.test;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import org.junit.Test;
import java.io.InputStream;
import java.util.List;
//使用dom4j解析mybatis核心配置文件 32
public class ParseXMLByDom4jTest {
//解析CarMapper.xml 34
@Test
public void testParseSqlMapperXML() throws Exception{
SAXReader reader = new SAXReader();
InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("CarMapper.xml");
Document document = reader.read(is);
// 获取namespace
String xpath = "/mapper";
Element mapper = (Element) document.selectSingleNode(xpath);
String namespace = mapper.attributeValue("namespace");
System.out.println(namespace);
// 获取mapper节点下所有的子节点
List elements = mapper.elements();
// 遍历
elements.forEach(element -> {
// 获取sqlId
String id = element.attributeValue("id");
System.out.println(id);
// 获取resultType
// 没有这个属性的话,会自动返回"null"
String resultType = element.attributeValue("resultType");
System.out.println(resultType);
// 获取标签中的sql语句(表示获取标签中的文本内容,而且去除前后空白)
String sql = element.getTextTrim();
System.out.println(sql);
// insert into t_car values(null,#{carNum},#{brand},#{guidePrice},#{produceTime},#{carType})
// insert into t_car values(null,?,?,?,?,?)
// mybaits封装了jdbc。早晚要执行带有?的sql语句。
// 转换
String newSql = sql.replaceAll("#\\{[0-9A-Za-z_$]*}", "?");
System.out.println(newSql);
});
}
//使用dom4j解析mybatis核心配置文件 32
@Test
public void testParseMyBatisConfigXML() throws Exception{
// 创建SAXReader对象
SAXReader reader = new SAXReader();
// 获取输入流
InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("mybatis-config.xml");
// 读XML文件,返回document对象。document对象是文档对象,代表了整个XML文件。
Document document = reader.read(is);
//System.out.println(document);
// // 获取文档当中的根标签
// Element rootElt = document.getRootElement();
// String rootEltName = rootElt.getName();
// System.out.println("根节点的名字:" + rootEltName);
//
//获取default默认的环境id
// xpath是做标签路径匹配的。能够让我们快速定位XML文件中的元素。
// 以下的xpath代表了:从根下开始找configuration标签,然后找configuration标签下
// 的子标签environments
String xpath = "/configuration/environments";
// Element是Node类的子类,方法更多,使用更便捷。
Element environments = (Element) document.selectSingleNode(xpath);
// 获取属性的值
String defaultEnvironmentId = environments.attributeValue("default");
System.out.println("默认环境的id:" + defaultEnvironmentId);
// 获取具体的环境environment
xpath = "/configuration/environments/environment[@id='"+defaultEnvironmentId+"']";
//System.out.println(xpath);
Element environment = (Element) document.selectSingleNode(xpath);
// 33
// 获取environment节点下的transactionManager节点(Element的element()方法用来获取孩子节点)
Element transactionManager = environment.element("transactionManager");
String transactionType = transactionManager.attributeValue("type");
System.out.println("事务管理器的类型:" + transactionType);
// 获取dataSource节点
Element dataSource = environment.element("dataSource");
String dataSourceType = dataSource.attributeValue("type");
System.out.println("数据源的类型:" + dataSourceType);
// 获取dataSource节点下的所有子节点
List propertyElts = dataSource.elements();
// 遍历
propertyElts.forEach(propertyElt -> {
String name = propertyElt.attributeValue("name");
String value = propertyElt.attributeValue("value");
System.out.println(name + "=" + value);
});
// 获取所有的mapper标签
// 不想从根下开始获取,你想从任意位置开始,获取所有的某个标签,xpath该这样写
xpath = "//mapper";
List mappers = document.selectNodes(xpath);
// 遍历
mappers.forEach(mapper -> {
Element mapperElt = (Element) mapper;
String resource = mapperElt.attributeValue("resource");
System.out.println(resource);
});
}
}
resources目录下
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="powernodeDB">
<environment id="powernodeDB">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/powernode"/>
<property name="username" value="root"/>
<property name="password" value="lzl"/>
</dataSource>
</environment>
<!--这是mybatis的另一个环境,也就是连接的数据库是另一个数据库mybatis-->
<environment id="mybatisDB">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis"/>
<property name="username" value="root"/>
<property name="password" value="lzl"/>
</dataSource>
</environment>
</environments>
<mappers>
<!--resource属性自动会从类的根路径下开始查找资源。-->
<mapper resource="CarMapper.xml"/>
</mappers>
</configuration>
CarMappper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="car">
<insert id="insertCar">
insert into t_car values(null,#{carNum},#{brand},#{guidePrice},#{produceTime},#{carType})
</insert>
<select id="selectById" resultType="com.powernode.mybatis.pojo.Car">
select
id,car_num as carNum,brand,guide_price as guidePrice,
produce_time as produceTime,
car_type as carType
from
t_car
where
id = #{id}
</select>
</mapper>
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.powernode</groupId>
<artifactId>course6</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<!--依赖-->
<dependencies>
<!--dom4j的依赖-->
<dependency>
<groupId>org.dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>2.1.3</version>
</dependency>
<!--jaxen依赖-->
<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>1.2.0</version>
</dependency>
<!--junit依赖-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<!-- 编译代码使用的jdk版本-->
<maven.compiler.source>1.8</maven.compiler.source>
<!-- 运行程序使用的jdk版本-->
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project>
标签:xpath,String,配置文件,dom4j,System,Element,println,mybatis,out
From: https://blog.51cto.com/u_15784725/6398262