01:
CarMapper.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 sql:保存一个汽车信息-->
<insert id="insertCar">
insert into t_car
(id,car_num,brand,guide_price,produce_time,car_type)
values
(null,#{carNum},#{brand},#{guidePrice},#{produceTime},#{carType})
</insert>
</mapper>
package com.wsd;
import org.dom4j.Document;
import org.dom4j.DocumentException;
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;
/**
* @program: spring_learn
* @description:
* @author: Mr.Wang
* @create: 2023-06-20 21:48
**/
public class TestXml {
@Test
public void testParseMapperXml(){
// 创建 SAXReader 对象
SAXReader xReader = new SAXReader();
//获取xml文件的输入流
InputStream xmlInputStream = ClassLoader.getSystemClassLoader().getResourceAsStream("CarMapper.xml");
Document document = null;
try{
//利用xml的输入流来解析xml文件,返回Document对象
document = xReader.read(xmlInputStream);
//获取根节点 mapper标签
String xpath = "/mapper";
/*selectSingleNode 返回类型为 Node,
将其强转为 Element(Element 是 Node 的子类),为了使用Element更多的方法
*/
Element mapperElement = (Element) document.selectSingleNode(xpath);
System.out.println(mapperElement);
//获取属性值
String namespace = mapperElement.attributeValue("namespace");
System.out.println(namespace);
}
catch ( DocumentException e){
e.printStackTrace();
}
}
}
02:
Car pojo :
package com.wsd.pojo;
/**
* @program: spring_learn
* @description:
* @author: Mr.Wang
* @create: 2023-06-17 21:53
**/
public class Car {
private Long id;
private String carNum;
private String brand;
private Double guidePrice;
private String produceTime;
private String carType;
@Override
public String toString() {
return "Car{" +
"id=" + id +
", carNum='" + carNum + '\'' +
", brand='" + brand + '\'' +
", guidePrice=" + guidePrice +
", produceTime='" + produceTime + '\'' +
", carType='" + carType + '\'' +
'}';
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getCarNum() {
return carNum;
}
public void setCarNum(String carNum) {
this.carNum = carNum;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public Double getGuidePrice() {
return guidePrice;
}
public void setGuidePrice(Double guidePrice) {
this.guidePrice = guidePrice;
}
public String getProduceTime() {
return produceTime;
}
public void setProduceTime(String produceTime) {
this.produceTime = produceTime;
}
public String getCarType() {
return carType;
}
public void setCarType(String carType) {
this.carType = carType;
}
}
CarMapper.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 sql:保存一个汽车信息-->
<insert id="insertCar">
insert into t_car
(id,car_num,brand,guide_price,produce_time,car_type)
values
(null,#{carNum},#{brand},#{guidePrice},#{produceTime},#{carType})
</insert>
<select id="selectCarAll" resultType="com.wsd.pojo.Car">
<!--记得使用as起别名,让查询结果的字段名和java类的属性名对应上-->
select
brand, guide_price as guidePrice, produce_time as produceTime, car_type as carType
from
t_car
</select>
</mapper>
package com.wsd;
import org.dom4j.Document;
import org.dom4j.DocumentException;
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;
/**
* @program: spring_learn
* @description:
* @author: Mr.Wang
* @create: 2023-06-20 21:48
**/
public class TestXml {
@Test
public void testParseMapperXml(){
// 创建 SAXReader 对象
SAXReader xReader = new SAXReader();
//获取xml文件的输入流
InputStream xmlInputStream = ClassLoader.getSystemClassLoader().getResourceAsStream("CarMapper.xml");
Document document = null;
try{
//利用xml的输入流来解析xml文件,返回Document对象
document = xReader.read(xmlInputStream);
//获取根节点 mapper标签
String xpath = "/mapper";
/*selectSingleNode 返回类型为 Node,
将其强转为 Element(Element 是 Node 的子类),为了使用Element更多的方法
*/
Element mapperElement = (Element) document.selectSingleNode(xpath);
//获取属性值
String namespace = mapperElement.attributeValue("namespace");
//获取所有 子 元素
List<Element> elements = mapperElement.elements();
//遍历
elements.forEach(
element -> {
//获取 id 属性
String id = element.attributeValue("id");
System.out.println("id = " + id);
//获取 resultType 属性,没有该属性 返回 null
String resultType = element.attributeValue("resultType");
System.out.println("resultType = " + resultType);
//获取标签的文本内容(sql语句),并且去除前后的空格
String sql = element.getTextTrim();
System.out.println("sql :" + sql);
}
);
}
catch ( DocumentException e){
e.printStackTrace();
}
}
}
"C:\Program Files\Java\jdk-17\bin\java.exe" -ea -Didea.test.cyclic.buffer.size=1048576 -Didea.launcher.port=62271 "-Didea.launcher.bin.path=C:\Minecloud\IDEA_2019\IntelliJ IDEA 2019.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Minecloud\IDEA_2019\IntelliJ IDEA 2019.1\lib\idea_rt.jar;C:\Minecloud\IDEA_2019\IntelliJ IDEA 2019.1\plugins\junit\lib\junit-rt.jar;C:\Minecloud\IDEA_2019\IntelliJ IDEA 2019.1\plugins\junit\lib\junit5-rt.jar;C:\Minecloud\IDEA_workspace\spring_learn\parsexml\target\test-classes;C:\Minecloud\IDEA_workspace\spring_learn\parsexml\target\classes;C:\Minecloud\maven_3.9\apache-maven-3.9.0\repo\org\dom4j\dom4j\2.1.3\dom4j-2.1.3.jar;C:\Minecloud\maven_3.9\apache-maven-3.9.0\repo\jaxen\jaxen\1.2.0\jaxen-1.2.0.jar;C:\Minecloud\maven_3.9\apache-maven-3.9.0\repo\junit\junit\4.13.2\junit-4.13.2.jar;C:\Minecloud\maven_3.9\apache-maven-3.9.0\repo\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar" com.intellij.rt.execution.application.AppMainV2 com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit4 com.wsd.TestXml,testParseMapperXml
id = insertCar
resultType = null
sql :insert into t_car (id,car_num,brand,guide_price,produce_time,car_type) values (null,#{carNum},#{brand},#{guidePrice},#{produceTime},#{carType})
id = selectCarAll
resultType = com.wsd.pojo.Car
sql :select brand, guide_price as guidePrice, produce_time as produceTime, car_type as carType from t_carProcess finished with exit code 0
03:
package com.wsd;
import org.dom4j.Document;
import org.dom4j.DocumentException;
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;
/**
* @program: spring_learn
* @description:
* @author: Mr.Wang
* @create: 2023-06-20 21:48
**/
public class TestXml {
@Test
public void testParseMapperXml(){
// 创建 SAXReader 对象
SAXReader xReader = new SAXReader();
//获取xml文件的输入流
InputStream xmlInputStream = ClassLoader.getSystemClassLoader().getResourceAsStream("CarMapper.xml");
Document document = null;
try{
//利用xml的输入流来解析xml文件,返回Document对象
document = xReader.read(xmlInputStream);
//获取根节点 mapper标签
String xpath = "/mapper";
/*selectSingleNode 返回类型为 Node,
将其强转为 Element(Element 是 Node 的子类),为了使用Element更多的方法
*/
Element mapperElement = (Element) document.selectSingleNode(xpath);
//获取属性值
String namespace = mapperElement.attributeValue("namespace");
//获取所有 子 元素
List<Element> elements = mapperElement.elements();
//遍历
elements.forEach(
element -> {
//获取 id 属性
String id = element.attributeValue("id");
System.out.println("id = " + id);
//获取 resultType 属性,没有该属性 返回 null
String resultType = element.attributeValue("resultType");
System.out.println("resultType = " + resultType);
//获取标签的文本内容(sql语句),并且去除前后的空格
String mapperSql = element.getTextTrim();
System.out.println("mapperSql :" + mapperSql);
//将 #{} 替换为 ?,"#\\{[0-9A-Za-z_$]*}" 是匹配 #{} 的正则表达式
String preSql = mapperSql.replaceAll("#\\{[0-9A-Za-z_$]*}", "?");
System.out.println("preSql :" + preSql);
}
);
}
catch ( DocumentException e){
e.printStackTrace();
}
}
}
id = insertCar
resultType = null
mapperSql :insert into t_car (id,car_num,brand,guide_price,produce_time,car_type) values (null,#{carNum},#{brand},#{guidePrice},#{produceTime},#{carType})
preSql :insert into t_car (id,car_num,brand,guide_price,produce_time,car_type) values (null,?,?,?,?,?)
id = selectCarAll
resultType = com.wsd.pojo.Car
mapperSql :select brand, guide_price as guidePrice, produce_time as produceTime, car_type as carType from t_car
preSql :select brand, guide_price as guidePrice, produce_time as produceTime, car_type as carType from t_car