java源码中xml是什么,Mybatis源码分析-读取非项目中的xml文件

mybatis-config.xml配置文件

/p>

PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-config.dtd">

demo.xml文件

/p>

PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >

select * from base_patient where s_code = #{sCode}

and d_code = #{patientNo}

java调用代码

Map params = new HashMap<>();

params.put("sCode","system");

// params.put("patientNo","111");

String resource = "D:\\tmp\\mybatis\\mybatis\\mybatis-config.xml";

try (InputStream inputStream = new FileInputStream(resource)){

Configuration configuration = new Configuration();

SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();

SqlSessionFactory factory = builder.build(inputStream);

datas = factory.openSession().selectList("demo.findBySql",params);

System.out.println(JSON.toJSONString(datas));

}catch (Exception e){

e.printStackTrace();

}

源码分析

在Mybatis的SqlSessionFactory启动时,会先读取mybatis-config.xml配置文件进行初始化。mappers标签里面配置的xml文件,在XMLConfigBuilder.parseConfiguration(XNode root)方法中进行加载,而mybatis底层提供了几种xml加载方式,支持项目中的xml读取,以及URL的方式。

private void parseConfiguration(XNode root) {

try {

propertiesElement(root.evalNode("properties")); //issue #117 read properties first

typeAliasesElement(root.evalNode("typeAliases"));

pluginElement(root.evalNode("plugins"));

objectFactoryElement(root.evalNode("objectFactory"));

objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));

settingsElement(root.evalNode("settings"));

environmentsElement(root.evalNode("environments"));

databaseIdProviderElement(root.evalNode("databaseIdProvider"));

typeHandlerElement(root.evalNode("typeHandlers"));

mapperElement(root.evalNode("mappers")); //加载Mappers中的配置文件

} catch (Exception e) {

throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e);

}

}

mapperElement方法:

private void mapperElement(XNode parent) throws Exception {

if (parent != null) {

for (XNode child : parent.getChildren()) {

if ("package".equals(child.getName())) {

String mapperPackage = child.getStringAttribute("name");

configuration.addMappers(mapperPackage);

} else {

String resource = child.getStringAttribute("resource");

String url = child.getStringAttribute("url");

String mapperClass = child.getStringAttribute("class");

if (resource != null && url == null && mapperClass == null) {

ErrorContext.instance().resource(resource);

InputStream inputStream = Resources.getResourceAsStream(resource);/ 读取本地类路径下面的xml

XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, resource, configuration.getSqlFragments());

mapperParser.parse();

} else if (resource == null && url != null && mapperClass == null) {

ErrorContext.instance().resource(url);

InputStream inputStream = Resources.getUrlAsStream(url);/ 读取URL资源中的xml

XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, url, configuration.getSqlFragments());

mapperParser.parse();

} else if (resource == null && url == null && mapperClass != null) {

Class> mapperInterface = Resources.classForName(mapperClass);

configuration.addMapper(mapperInterface);

} else {

throw new BuilderException("A mapper element may only specify a url, resource or class, but not more than one.");

}

}

}

}

}

Resources.getUrlAsStream(url)方法:

public static InputStream getUrlAsStream(String urlString) throws IOException {

URL url = new URL(urlString); // 说明支持多种文件读取方式

URLConnection conn = url.openConnection();

return conn.getInputStream();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值