MyBatis之xml配置的解析

书欣 MyBatis 2022-08-15 15:50:20 6120 1
下文笔者讲述Mybatis之xml配置解析的相关简介说明,如下所示
下文笔者讲述使用XMLConfigBuilder类
读取xml文件并生成一个DataSource
使用xml配置一个DataSource
xml格式如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<database>
    <property name="driverClassName">com.mysql.jdbc.Driver</property>
    <property name="url">jdbc:mysql://localhost:3306/mysqldb?useUnicode=true&characterEncoding=utf8</property>
    <property name="username">root</property>
    <property name="password">123456</property>
</database>

dom4j

dom4j可用于读取每一个节点的标签、属性、子节点等
而且使用较为简单,只需要输入流或文件即可解析
例: 使用dom4j,先引入dom4j相应的jar包
<dependency>
    <groupId>dom4j</groupId>
    <artifactId>dom4j</artifactId>
    <version>1.6.1</version>
</dependency>
编写相应的示例代码
// InputStream in;
SAXReader reader = new SAXReader();
Document document = reader.read(in);
Element root = document.getRootElement();
String tagName = root.getName();    // 标签名
for (Object item : root.elements("xxx")) {
    // e表示root的标签名为xxx的孩子
    Element e = (Element) item;
}

XMLConfigure之获取配置信息

private static ClassLoader loader = ClassLoader.getSystemClassLoader();

public static DataSource build(String resource)
{
    try {
        InputStream stream = loader.getResourceAsStream(resource);
        SAXReader reader = new SAXReader();
        Document document = reader.read(stream);
        Element root = document.getRootElement();
        // Element代入,生成DataSource
        return evalDataSource(root);
    } catch (Exception e) {
        throw new RuntimeException("error occured while evaling xml " + resource);
    }
}
 
//内容解析
public static DataSource evalDataSource(Element node) throws ClassNotFoundException 
{
    if (!node.getName().equals("database")) {
        throw new RuntimeException("root should be <database>");
    }
    String driverClassName = null;
    String url = null;
    String username = null;
    String password = null;
    for (Object item : node.elements("property")) {
        Element i = (Element) item;         
        String value = getValue(i);
        String name = i.attributeValue("name");
        if (name == null || value == null) 
            throw new RuntimeException("[database]: <property> should contain name and value");

        switch (name) {
            case "url" : url = value; break;
            case "username" : username = value; break;
            case "password" : password = value; break;
            case "driverClassName" : driverClassName = value; break; 
            default : throw new RuntimeException("[database]: <property> unknown name"); 
        }
    }
    return new VDataSource(driverClassName, url, username, password);
}

//测试代码
DataSource ds = XMLConfigBuilder.build("config.xml");
Session session = new VSession(data);
session.exec("select * from users where id<8");
 
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: http://www.Java265.com/JavaFramework/MyBatis/202208/4237.html

最近发表

热门文章

好文推荐

Java265.com

//www.java265.com

站长统计|USA-001

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者