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); } }
相关推荐
3. 数据库脚本:用于创建和初始化数据库表结构。 4. 开发环境配置文件:包括服务器配置、数据库连接配置等。 五、二次开发与定制 该系统具有较高的可扩展性和可维护性,便于二次开发与定制。开发者可以根据自己的...
项目初始化: 使用Spring Initializr创建一个Spring Boot项目,选择所需的依赖,例如Web、Security、MyBatis等。 数据库设计: 设计数据库表结构,包括患者表、医生表、挂号表、门诊表、住院表、药品表、检查检验表...
java进阶源码分析专题常用设计模式线程与并发锁的使用深度理解synchronized、volatile、cas手写ASQSpring5IOC容器设计原理及高级特性AOP设计原理FactoryBean与BeanFactorySpring事务处理机制Spring JDK动态代理...
数据库部分提供了项目所需的SQL脚本,用于初始化数据库结构。在疫情冷链追溯系统中,用户可以通过前端界面查询食品的冷链信息,如生产日期、运输时间、温度等。同时,系统还支持对食品进行追溯,以便在出现问题时...
数据库部分提供了项目所需的SQL脚本,用于创建表结构和初始化数据。下载此资源后,您可以按照以下步骤进行开发:安装Java开发环境(如JDK 8或更高版本),配置环境变量;使用Maven或Gradle构建工具导入项目依赖;编写...
2、课程中包含大量SpringBoot 原理讲解、源码分析。 3、课程中涉及很多SpringBoot 实用插件技术、监控技术; 适用人群: 1、有一定的Java基础以及SSM框架知识。 2、对目前职业有进一步提升要求,希望从事数据行业...
部署说明:本项目提供了详细的部署说明,包括环境配置、依赖安装、数据库初始化等步骤,方便用户快速搭建和运行系统。 演示视频:为了帮助用户更好地了解和使用卓越导师双选系统,我们提供了一份演示视频,展示了...
java版erp管理系统源码 华夏ERP完整开源版J2EE代码 很多人说华夏ERP(英文名:jshERP)是目前唯一完整开源的进销存系统 虽然目前只有进销存+财务的功能,但后面将会推出ERP的全部功能,大家一起努力吧 系统初始账号...
数据库: 系统可能使用MySQL、PostgreSQL或其他关系型数据库来存储数据,通过JPA (Java Persistence API) 或者MyBatis等ORM (Object-Relational Mapping) 工具与Spring Boot集成,实现数据的持久化。中间件: 为了提高...
防止3.2.1版本的反序列化漏洞子表模板默认日期格式化代码生成预览语言根据后缀名高亮显示代码生成主子表相同字段导致数据...初始化数据优化Excel导入增加空行判断修复横向菜单无法打开页签问题修复导入数据为负浮点数时...