dom4j 解析 mybatis mapper xml 文件

这篇具有很好参考价值的文章主要介绍了dom4j 解析 mybatis mapper xml 文件。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

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();
        }

    }


}

 文章来源地址https://www.toymoban.com/news/detail-496612.html

dom4j 解析 mybatis mapper xml 文件

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();
        }

    }

}

dom4j 解析 mybatis mapper xml 文件 

 

 

"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_car

Process 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();
        }

    }
}

 dom4j 解析 mybatis mapper xml 文件

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
 

 

到了这里,关于dom4j 解析 mybatis mapper xml 文件的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Dom4j的下载安装(保姆级教程)

    1.下载 方法① 官网下载:dom4j(打开速度可能比较慢) 方法②:在网上搜索dom4j安装包 2.安装 (1)下载完成后,创建一个文件存放好dom4j压缩包( 注意!!!不要解压!!!乖,咱听话!!! ) (2)打开你的Java编程软件Eclipse或者IDEA Eclipse 版: ① IDEA 版: ①“右击项目名

    2024年02月13日
    浏览(46)
  • Mybatis|mapper配置文件xml位置

    在核心配置文件mybatis-config.xml中设置映射文件位置 application.yml文件中添加配置: mybatis案例中和springboot中都是一样的,只要目录名和包名相同 需要在pom.xml中添加如下内容 越努力,越幸运! codefishyyf与你一起努力!

    2024年02月06日
    浏览(60)
  • Mapper.xml文件解析

     最近在做一个分布式项目,看到xml文件原先只是上网CV,还是要搞清楚吧! 下面是一个Mybatis的SQL映射文件的配置 这个元素定义了命名空间,用于标识这个映射文件对应的接口。在这里,命名空间指向了 cn.itedus.lottery.infrastructure.dao.IActivityDao 这个接口,表示这个映射文件用

    2024年02月10日
    浏览(34)
  • MyBatis Plus Mapper.xml映射文件常用标签<if>、<foreach>、#{}、${}等

    一、判断 Integer、Long 等常数类参数 注意:判断常数类参数,只能判断 != null,不能判断 != \\\'\\\' 否则判断不会生效 二、判断 String 字符串类参数 注意:判断字符串类参数可以判断 != \\\'\\\' 三、判断参数值与指定的值,是否相等或不相等 注意: 1、判断 Integer、Long 等常数类型等于

    2024年02月09日
    浏览(48)
  • 【IDEA优化】:解决MyBatis Mapper 的XML文件SQL语句无法自动提示问题(独家方案,亲测可用)

    IDEA中MyBatis编写mapper的SQL语句的时候无法提示SQL和数据库 无法正常方便的使用IDEA的提示功能,更准确无误的编写代码 亲测可用,一劳永逸(IDEA版本 IntelliJ IDEA 2021.1.3 ) 目的在于对 Mybatis的Mapper.XML中sql语句进行提示 网上的各种解决方案本质上其实都是配置SQL方言和SQL解析范

    2024年02月16日
    浏览(56)
  • 【196】JAVA8 用 DOM 方式的 API 解析XML文件,无需引入第三方依赖。

    JAVA8 可以使用标准的 DOM 方式的 API 来编写代码解析 XML 文件,无需引入第三方依赖。下面是两个测试用的 XML 文件: test.xml test2.xml 负责接收所有信息的 AllDataDTO.java 负责接收用户信息的 AllDataUserDTO.java 处理 XML 文件的工具类,XmlUtils.java 用于测试效果的类 Main.java 输出结果:

    2024年01月22日
    浏览(36)
  • Mybatis mapper.xml 判断条件写法注意

    1.判断String是否为空 if test=\\\"stringParam != null and stringParam != \\\'\\\'\\\"/if 2.判断Integer是否大于0 判断等于  when test=\\\"item.mark == 1\\\"\\\" 3.判断List是否不为空 5.判断字符串是否等于特定字符(比如此处的user)

    2024年02月16日
    浏览(44)
  • (MVC)SpringBoot+Mybatis+Mapper.xml

    前言:本篇博客主要对MVC架构、Mybatis工程加深下理解,前面写过一篇博客:SprintBoot+html/css/js+mybatis的demo,里面涉及到了Mybatis的应用,此篇博客主要介绍一种将sql语句写到了配置文件里的方法,即Mybatis里Mapper.xml文件配置,其主要用于定义sql语句和映射关系 目录 MVC架构流程图

    2024年02月13日
    浏览(36)
  • 深入实现 MyBatis 底层机制的任务阶段4 - 开发 Mapper 接口和 Mapper.xml

    😀前言 在我们的自定义 MyBatis 底层机制实现过程中,我们已经深入研究了多个任务阶段,包括配置文件的读取、数据库连接的建立、执行器的编写,以及 SqlSession 的封装。每个任务阶段都为我们揭示了 MyBatis 内部工作原理的一部分,为构建完整的底层框架打下了坚实的基础

    2024年02月09日
    浏览(38)
  • mybatis中的mapper.xml中如何使用in方法

    提示:mapper.xml中如何使用in方法一般都是like或= 提示:使用foreach 注意,传入的参数是List ,如果传入的是array 则需要修改 collection部分定义为 collection=“array” 在MyBatis中使用in参数为集合时,需要使用到foreach标签。 下面详细介绍以下foreach标签的几个参数

    2024年02月15日
    浏览(36)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包