gongstring技术博客
最新文章
源码解读
软件安装
常见问题
大数据
常用工具
鸡汤文
备案号:鄂ICP备15015839号-1
鄂公网安备 42010202001692号
Mybatis源码分析-读取非项目中的xml文件
2018-05-17 17:28:38
作者: gongstring
源码解读
/
Mybatis源码分析-读取非项目中的xml文件
## mybatis-config.xml配置文件 ```
``` ## demo.xml文件 ```
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); 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); 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(); } ```