mybatis 的配置文件解析又XMLConfigBuilder的parseConfiguration方法来完成,解析结果都存在Configuration这个类中
private void parseConfiguration(XNode root) {//根节点 try { //解析别名 typeAliasesElement(root.evalNode("typeAliases")); //解析插件 pluginElement(root.evalNode("plugins")); //对象工厂解析 //MyBatis 每次创建结果对象的新实例时,它都会使用一个对象工厂(ObjectFactory)实例来完成 objectFactoryElement(root.evalNode("objectFactory")); // objectWrapperFactoryElement(root.evalNode("objectWrapperFactory")); //解析属性 propertiesElement(root.evalNode("properties")); //解析mybatis参数 settingsElement(root.evalNode("settings")); //解析环境 environmentsElement(root.evalNode("environments")); //解析类型处理 typeHandlerElement(root.evalNode("typeHandlers")); //解析mapper映射文件 mapperElement(root.evalNode("mappers")); } catch (Exception e) { throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e); } }
别名解析
private void typeAliasesElement(XNode parent) { if (parent != null) { for (XNode child : parent.getChildren()) { String alias = child.getStringAttribute("alias"); String type = child.getStringAttribute("type"); try { Class<?> clazz = Resources.classForName(type); if (alias == null) { //没有设别名时 typeAliasRegistry.registerAlias(clazz); } else { //有设别名 typeAliasRegistry.registerAlias(alias, clazz); } } catch (ClassNotFoundException e) { throw new BuilderException("Error registering typeAlias for '" + alias + "'. Cause: " + e, e); } } } } public void registerAlias(Class type) { String alias = type.getSimpleName(); //从注解上获取 Alias aliasAnnotation = (Alias) type.getAnnotation(Alias.class); if (aliasAnnotation != null) { alias = aliasAnnotation.value(); } registerAlias(alias, type); } public void registerAlias(String alias, Class value) { assert alias != null; String key = alias.toLowerCase(); if (TYPE_ALIASES.containsKey(key) && !TYPE_ALIASES.get(key).equals(value.getName()) && TYPE_ALIASES.get(alias) != null) { if (!value.equals(TYPE_ALIASES.get(alias))) { throw new TypeException("The alias '" + alias + "' is already mapped to the value '" + TYPE_ALIASES.get(alias).getName() + "'."); } } //存入hashMaplim TYPE_ALIASES.put(key, value); }
插件解析
private void pluginElement(XNode parent) throws Exception { if (parent != null) { for (XNode child : parent.getChildren()) { String interceptor = child.getStringAttribute("interceptor"); //获取插件属性 Properties properties = child.getChildrenAsProperties(); Interceptor interceptorInstance = (Interceptor) resolveClass(interceptor).newInstance(); interceptorInstance.setProperties(properties); //加入到configuration类的插件执行链里面,其实就是List configuration.addInterceptor(interceptorInstance); } }
z工厂对象解析
private void objectFactoryElement(XNode context) throws Exception { if (context != null) { String type = context.getStringAttribute("type"); Properties properties = context.getChildrenAsProperties(); ObjectFactory factory = (ObjectFactory) resolveClass(type).newInstance(); factory.setProperties(properties); //替换默认的DefaultObjectFactory工厂 configuration.setObjectFactory(factory); } }
解析属性
private void propertiesElement(XNode context) throws Exception { if (context != null) { Properties defaults = context.getChildrenAsProperties(); String resource = context.getStringAttribute("resource"); String url = context.getStringAttribute("url"); if (resource != null && url != null) { throw new BuilderException("The properties element cannot specify both a URL and a resource based property file reference. Please specify one or the other."); } if (resource != null) { defaults.putAll(Resources.getResourceAsProperties(resource)); } else if (url != null) { defaults.putAll(Resources.getUrlAsProperties(url)); } Properties vars = configuration.getVariables(); if (vars != null) { defaults.putAll(vars); } parser.setVariables(defaults); configuration.setVariables(defaults); } }
解析mybatis参数
private void settingsElement(XNode context) throws Exception { if (context != null) { Properties props = context.getChildrenAsProperties(); // Check that all settings are known to the configuration class MetaClass metaConfig = MetaClass.forClass(Configuration.class); for (Object key : props.keySet()) { //严重参数是否合法 if (!metaConfig.hasSetter(String.valueOf(key))) { throw new BuilderException("The setting " + key + " is not known. Make sure you spelled it correctly (case sensitive)."); } } configuration.setAutoMappingBehavior(AutoMappingBehavior.valueOf(stringValueOf(props.getProperty("autoMappingBehavior"), "PARTIAL"))); configuration.setCacheEnabled(booleanValueOf(props.getProperty("cacheEnabled"), true)); configuration.setLazyLoadingEnabled(booleanValueOf(props.getProperty("lazyLoadingEnabled"), false)); configuration.setAggressiveLazyLoading(booleanValueOf(props.getProperty("aggressiveLazyLoading"), true)); configuration.setMultipleResultSetsEnabled(booleanValueOf(props.getProperty("multipleResultSetsEnabled"), true)); configuration.setUseColumnLabel(booleanValueOf(props.getProperty("useColumnLabel"), true)); configuration.setUseGeneratedKeys(booleanValueOf(props.getProperty("useGeneratedKeys"), false)); configuration.setDefaultExecutorType(ExecutorType.valueOf(stringValueOf(props.getProperty("defaultExecutorType"), "SIMPLE"))); configuration.setDefaultStatementTimeout(integerValueOf(props.getProperty("defaultStatementTimeout"), null)); } }
环境解析
private void environmentsElement(XNode context) throws Exception { if (context != null) { if (environment == null) { environment = context.getStringAttribute("default"); } for (XNode child : context.getChildren()) { String id = child.getStringAttribute("id"); if (isSpecifiedEnvironment(id)) { TransactionFactory txFactory = transactionManagerElement(child.evalNode("transactionManager")); DataSourceFactory dsFactory = dataSourceElement(child.evalNode("dataSource")); Environment.Builder environmentBuilder = new Environment.Builder(id) .transactionFactory(txFactory) .dataSource(dsFactory.getDataSource()); configuration.setEnvironment(environmentBuilder.build()); } } } }
环境解析
rivate void environmentsElement(XNode context) throws Exception { if (context != null) { if (environment == null) { environment = context.getStringAttribute("default"); } for (XNode child : context.getChildren()) { String id = child.getStringAttribute("id"); if (isSpecifiedEnvironment(id)) { //事物管理器 TransactionFactory txFactory = transactionManagerElement(child.evalNode("transactionManager")); //数据源 DataSourceFactory dsFactory = dataSourceElement(child.evalNode("dataSource")); Environment.Builder environmentBuilder = new Environment.Builder(id) .transactionFactory(txFactory) .dataSource(dsFactory.getDataSource()); configuration.setEnvironment(environmentBuilder.build()); } } } }
类型处理器解析
private void typeHandlerElement(XNode parent) throws Exception { if (parent != null) { for (XNode child : parent.getChildren()) { String javaType = child.getStringAttribute("javaType"); String jdbcType = child.getStringAttribute("jdbcType"); String handler = child.getStringAttribute("handler"); Class<?> javaTypeClass = resolveClass(javaType); TypeHandler typeHandlerInstance = (TypeHandler) resolveClass(handler).newInstance(); //也是解析后放在hashmap里 // private final Map<Class<?>, Map<JdbcType, TypeHandler>> TYPE_HANDLER_MAP = new HashMap<Class<?>, Map<JdbcType, TypeHandler>>(); if (jdbcType == null) { typeHandlerRegistry.register(javaTypeClass, typeHandlerInstance); } else { typeHandlerRegistry.register(javaTypeClass, resolveJdbcType(jdbcType), typeHandlerInstance); } } } }
mapper映射文件
private void mapperElement(XNode parent) throws Exception { if (parent != null) { for (XNode child : parent.getChildren()) { String resource = child.getStringAttribute("resource"); String url = child.getStringAttribute("url"); InputStream inputStream; if (resource != null && url == null) { ErrorContext.instance().resource(resource); inputStream = Resources.getResourceAsStream(resource); //构建mapper xml 解析 XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, resource, configuration.getSqlFragments()); mapperParser.parse(); } else if (url != null && resource == null) { ErrorContext.instance().resource(url); inputStream = Resources.getUrlAsStream(url); XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, url, configuration.getSqlFragments()); mapperParser.parse(); } else { throw new BuilderException("A mapper element may only specify a url or resource, but not both."); } } } } public void parse() { //判断是否已经解析过 if (!configuration.isResourceLoaded(resource)) { //获取mapper 跟节点并解析 configurationElement(parser.evalNode("/mapper")); //表示此resource已经解析 configuration.addLoadedResource(resource); //根据mapper的配置文件命名空间,查找对应的mapper类 bindMapperForNamespace(); } parsePendingChacheRefs(); parsePendingStatements(); } private void configurationElement(XNode context) { try { String namespace = context.getStringAttribute("namespace"); builderAssistant.setCurrentNamespace(namespace); cacheRefElement(context.evalNode("cache-ref")); cacheElement(context.evalNode("cache")); parameterMapElement(context.evalNodes("/mapper/parameterMap")); resultMapElements(context.evalNodes("/mapper/resultMap")); sqlElement(context.evalNodes("/mapper/sql")); buildStatementFromContext(context.evalNodes("select|insert|update|delete")); } catch (Exception e) { throw new RuntimeException("Error parsing Mapper XML. Cause: " + e, e); } }
相关推荐
MyBatis源码分析.pdf
一本小小的MyBatis源码分析书,内容详细介绍MyBatis源码!对于初中级java开发工程师是必备的!
mybatis的源码分析视频,有详细的视频和文档。 视频地址亲测有效。失效了,请联系我。
Mybatis源码分析主要分三部分: 1.使用案例讲解 2.Mybatis中用的所有设计模式讲解 3.源码分析
给大家分享一套课程,Java架构师之源码分析专题SpringBoot2.x、Spring5、SpringMVC、Mybatis源码分析,希望对大家学习有帮助。
Mybatis源码分析
mybatis源码分析,包含建表语句,等等。开发者可以通过此源码分析mybatis实现原理。
- Mybatis源码分析 - 1. 解析配置文件,创建SQLSessionFactory - 2. 开启java程序和数据库之间的会话: - 3. 获取mapper代理对象: - 4. 执行mapper接口方法: - mybatis源码总结 <!-- /TOC --> Mybatis源码...
Mybatis源码分析七之Cache缓存
Spring整合Mybatis源码解析
mybtais是一款优良的框架,虽然体积相对来说比较小,但是看起来也很庞大,不过有了思维导图也能简单不少
5套完整springboot+mybatis源码下载只供学习之用,一套登录,一套权限,一套博客,一套.....
阅读mybatis源码所记笔记。 基本: 1.数据源获取 2.mapper的sql语句获取 3.怎么去获得到的resultSet 重点: 1.orm是什么:体现于resultSet的数据库类型和java类型的转换 2.mybatis的一级缓存原理(简单的ifelse判断...
阿里巴巴P7架构师纯手工打造MyBatis源码——1小时解密底层源码.txt 需要更多往期录播资料可以联系我
mybatis源码包mybatis源码包mybatis源码包mybatis源码包
适合学习mybatis源码的各位同学,搜集不易,多多支持
Mybatis源码分析
mybatis中文文档和mybatis源码
mybatis核心源码mybatis核心源码mybatis核心源码mybatis核心源码mybatis核心源码mybatis核心源码mybatis核心源码mybatis核心源码mybatis核心源码mybatis核心源码mybatis核心源码mybatis核心源码mybatis核心源码...