在mybatis中手写解析xml文件代码(java)

创建两个xml文件

        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>

    <settings>
        <setting name="logImpl" value="SLF4J"/>
    </settings>

    <environments default="development">
        <environment id="development">

            <transactionManager type="JDBC"/>

            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/powerNode"/>
                <property name="username" value="root"/>
                <property name="password" value="WJH5201314"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="carMapper.xml"/>
        <mapper resource="UserMapper.xml"/>
    </mappers>
</configuration>

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="carName">
<!--    指定结果集要封装的java对象-->
    <select id="selectById" resultType="com.powerNode.mybatis.pojo.Car">
        select id,car_num as carNum,brand,guide_price as guidePrice from t_car where id = #{id}
    </select>
<!--   insert id是唯一标识-->
    <insert id="insertCar">
        insert into t_car(car_num,brand,guide_price,produce_time,car_type)
        values (#{carNum},#{brand},#{guidePrice},#{produceTime},#{carType})
    </insert>
</mapper>

解析mybatis-config.xml

创建一个测试类开始解析

public void test() throws Exception{
    //创建SAXReader对象
        SAXReader reader = new SAXReader();
    //获取流
        URL resource = ClassLoader.getSystemClassLoader().getResource("mybatis-config.xml");
        //读xml文件,返回document对象
        Document document = reader.read(resource);
//        System.out.println(document);


        //获取default默认的环境id
        String xpath="/configuration/environments";//做标签路径匹配
        Element elemente = (Element) document.selectSingleNode(xpath);//是node类的子类方法更多
        //获取属性的值
        String defaultEnvironment = elemente.attributeValue("default");

        //获取具体的环境
        xpath="/configuration/environments/environment[@id='"+defaultEnvironment+"']";
        Element environment = (Element) document.selectSingleNode(xpath);

        //获取子节点
        Element transactionManager = environment.element("transactionManager");
        String type = transactionManager.attributeValue("type");

        //获取dataSource
        Element dataSource = environment.element("dataSource");
        String dataSourceType = dataSource.attributeValue("type");

        //获取dataSource下的所有子节点
        List<Element> elements = dataSource.elements();
        elements.forEach(element -> {
            String name = element.attributeValue("name");
            String value = element.attributeValue("value");
            System.out.println(name+"="+value);
        });

        //获取所有的mapper标签
        xpath="//mapper";
        List<Node> mappers = document.selectNodes(xpath);
        mappers.forEach(mapper ->{
            Element mapperElement = (Element) mapper;
            String res = mapperElement.attributeValue("resource");
            System.out.println(res);
        });

    }

运行结果

        

解析carMapper.xml

        

  public void testParseXmlByDom4j() throws DocumentException {
        SAXReader reader = new SAXReader();
        URL resource = ClassLoader.getSystemClassLoader().getResource("carMapper.xml");
        Document document = reader.read(resource);

        //获取namespace
        String xpath ="/mapper";
        Element mapper = (Element) document.selectSingleNode(xpath);
        String namespace = mapper.attributeValue("namespace");

        //获取mapper子节点下所有节点
        List<Element> elements = mapper.elements();
        elements.forEach(element -> {

            //获取id
            String id = element.attributeValue("id");
            System.out.println("id:"+id);

            //获取resultType
            String resultType = element.attributeValue("resultType");
            System.out.println(resultType);//没有这个属性的话,会自动返回字符串

            //获取sql语句,获取文本内容并去掉前后空白
            String sql = element.getTextTrim();
            //正则表达式替换 #{...}使他变成带问好的sql语句
            String newSql = sql.replaceAll("#\\{[0-9A-Za-z_$]*}", "?");
            System.out.println("sql语句:"+newSql);
        });
    }

运行结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值