首页 > 其他分享 >mybatis解析settings标签

mybatis解析settings标签

时间:2023-12-07 22:33:26浏览次数:31  
标签:getProperty settings 标签 booleanValueOf props mybatis configuration

settings标签也是一个很重要的标签,虽然我们在使用的时候,没怎么配置settings标签里面的内容。好像一开始为了看sql语句,我们在settings标签里面配置了日志。

<settings>
        <setting name="logImpl" value="SLF4J"/>
    </settings>

其他的好像就没干什么了。其实settings标签里面的配置,还是跟mybatis的全局对象Configuration有关。直接看下解析这个标签的代码

XMLConfigBuilder:
private Properties settingsAsProperties(XNode context) {
    if (context == null) {
      return new Properties();
    }
    //将settings标签里面的子标签解析出来,并且放到一个Properties对象中
    Properties props = context.getChildrenAsProperties();
    // Check that all settings are known to the configuration class
    //localReflectorFactory->DefaultReflectorFactory
    MetaClass metaConfig = MetaClass.forClass(Configuration.class, localReflectorFactory);
    //这个for循环就是在判断settings标签内部的元素是不是在Configuration对象中,就看Configuration是否有set方法
    for (Object key : props.keySet()) {
      //如果settings标签里面配置的key在Configuration中找不到对应的方法,那么报错
      if (!metaConfig.hasSetter(String.valueOf(key))) {
        throw new BuilderException("The setting " + key + " is not known.  Make sure you spelled it correctly (case sensitive).");
      }
    }
    return props;
  }

这段代码也不难,就是将settings里面配置的标签转化为Prop对象。然后循环遍历key,如果key作为Configuration对象里面成员变量,如果这个成员变量没有对应的set方法,则报错。所以这里我们推断,settings里面配置的key和value,最终通过Configuration里面的set方法设置给对应的成员变量。比如上面我们配置的日志,就设置给了Configuration里面的日志对象。

Configuration里面的成员变量非常多,对应的set方法也非常多,所以settings里面的配置也非常多。那么接下来我们继续看看他把settings标签解析完后,获取Prop对象,又干了什么,看这个方法。

XMLConfigBuilder#  
private void settingsElement(Properties props) {
//configuration就是全局配置对象,看看有这么多成员变量需要set的,如果你没有在settings里面配,还给了默认值。
    configuration.setAutoMappingBehavior(AutoMappingBehavior.valueOf(props.getProperty("autoMappingBehavior", "PARTIAL")));
    configuration.setAutoMappingUnknownColumnBehavior(AutoMappingUnknownColumnBehavior.valueOf(props.getProperty("autoMappingUnknownColumnBehavior", "NONE")));
    configuration.setCacheEnabled(booleanValueOf(props.getProperty("cacheEnabled"), true));
    configuration.setProxyFactory((ProxyFactory) createInstance(props.getProperty("proxyFactory")));
    configuration.setLazyLoadingEnabled(booleanValueOf(props.getProperty("lazyLoadingEnabled"), false));
    configuration.setAggressiveLazyLoading(booleanValueOf(props.getProperty("aggressiveLazyLoading"), false));
    configuration.setMultipleResultSetsEnabled(booleanValueOf(props.getProperty("multipleResultSetsEnabled"), true));
    configuration.setUseColumnLabel(booleanValueOf(props.getProperty("useColumnLabel"), true));
    configuration.setUseGeneratedKeys(booleanValueOf(props.getProperty("useGeneratedKeys"), false));
    //设置mybatis默认的执行器,不配就是 SIMPLE
    configuration.setDefaultExecutorType(ExecutorType.valueOf(props.getProperty("defaultExecutorType", "SIMPLE")));
    configuration.setDefaultStatementTimeout(integerValueOf(props.getProperty("defaultStatementTimeout"), null));
    configuration.setDefaultFetchSize(integerValueOf(props.getProperty("defaultFetchSize"), null));
    configuration.setDefaultResultSetType(resolveResultSetType(props.getProperty("defaultResultSetType")));
    configuration.setMapUnderscoreToCamelCase(booleanValueOf(props.getProperty("mapUnderscoreToCamelCase"), false));
    configuration.setSafeRowBoundsEnabled(booleanValueOf(props.getProperty("safeRowBoundsEnabled"), false));
    configuration.setLocalCacheScope(LocalCacheScope.valueOf(props.getProperty("localCacheScope", "SESSION")));
    configuration.setJdbcTypeForNull(JdbcType.valueOf(props.getProperty("jdbcTypeForNull", "OTHER")));
    configuration.setLazyLoadTriggerMethods(stringSetValueOf(props.getProperty("lazyLoadTriggerMethods"), "equals,clone,hashCode,toString"));
    configuration.setSafeResultHandlerEnabled(booleanValueOf(props.getProperty("safeResultHandlerEnabled"), true));
    configuration.setDefaultScriptingLanguage(resolveClass(props.getProperty("defaultScriptingLanguage")));
    configuration.setDefaultEnumTypeHandler(resolveClass(props.getProperty("defaultEnumTypeHandler")));
    configuration.setCallSettersOnNulls(booleanValueOf(props.getProperty("callSettersOnNulls"), false));
    configuration.setUseActualParamName(booleanValueOf(props.getProperty("useActualParamName"), true));
    configuration.setReturnInstanceForEmptyRow(booleanValueOf(props.getProperty("returnInstanceForEmptyRow"), false));
    configuration.setLogPrefix(props.getProperty("logPrefix"));
    configuration.setConfigurationFactory(resolveClass(props.getProperty("configurationFactory")));
    configuration.setShrinkWhitespacesInSql(booleanValueOf(props.getProperty("shrinkWhitespacesInSql"), false));
    configuration.setDefaultSqlProviderType(resolveClass(props.getProperty("defaultSqlProviderType")));
  }

是不是挺吓人的。。。。至于Configuration里面那么多对象干嘛的,这是后面要研究的事情,而且这么多,我们肯定得挑几个重点的看看。

好的,本节告一段落。

标签:getProperty,settings,标签,booleanValueOf,props,mybatis,configuration
From: https://www.cnblogs.com/shenxingzhuge/p/17884069.html

相关文章

  • MyBatis(上)
    1.MyBatis简介1.1MyBatis简述MyBatis是一个Java持久层框架,用于简化数据库交互的开发。它的核心思想是将SQL语句与Java代码分离,通过配置文件或注解来映射Java对象与数据库表之间的关系。MyBatis提供了简洁的API,使得开发人员可以更方便地进行数据库操作。MyBatis最初是Apache的......
  • HTML列表标签学习
    一、有序列表例子:<ol> <li>苹果</li> <li>梨子</li></ol>其中有序列表可以有不同的排序方式:<h4>大写字母列表:</h4><oltype="A"><li>Apples</li><li>Bananas</li><li>Lemons</li><li>......
  • Java登陆第二十天——HTML常用标签
    文本标签文本常用的HTML标签:标签名标签描述<h1></h1>标题标签<h6></h6>标题标签<p></p>段落标签<hr>换行标签<br>换行标签标签栗子:<!DOCTYPEhtml><htmllang="en"><head><metacharset=&quo......
  • MyBatis的10种高级用法
    目录用来循环容器的标签forEach,查看例子concat模糊查询choose(when,otherwise)标签selectKey标签if标签if+where的条件判断if+set实现修改语句if+trim代替where/set标签foreach用来循环容器的标签forEachforeach元素的属性主要有item,index,collection,open,sep......
  • Chrome浏览器对不同标签页进行着色
    方法1:使用标签组Chrome浏览器的标签组功能允许您将标签分组在一起,并为每个组分配不同的颜色。要创建标签组,请右键单击一个标签,然后选择“添加到新组”。您还可以将标签拖放到另一个标签上以将其添加到组中。要更改标签组的颜色,请单击标签组的圆点,然后选择“更改颜色”。您可以......
  • mybatis-plus处理blob字段
    转载自:www.javaman.cn在SpringBoot项目中使用MyBatis-Plus处理longblob字段时,我们可以按照以下步骤进行操作。假设longblob存储的是字符串数据。以下是完整的示例代码:添加依赖:在你的项目的pom.xml文件中添加MyBatis-Plus的依赖:<dependency><groupId>com.......
  • mybatis-plus 新版代码生成器模板
    publicclassCodeGenerator{publicstaticvoidmain(String[]args){//数据源配置FastAutoGenerator.create("jdbc:mysql://127.0.0.1:3306/xdclass?useUnicode=true&characterEncoding=utf-8&useSSL=false","root",&qu......
  • 悲观锁、乐观锁、mybatis-plus实现乐观锁
    转载自:www.javaman.cn1、悲观锁、乐观锁乐观锁和悲观锁是两种用于处理并发操作的数据锁定策略。它们在处理多个事务尝试同时访问和修改同一数据时的方法有所不同。悲观锁(PessimisticLocking):概念:悲观锁是一种基于悲观态度的数据并发控制机制。它总是假设最坏的情况,即认为其他......
  • 悲观锁、乐观锁、mybatis-plus实现乐观锁
    悲观锁、乐观锁、mybatis-plus实现乐观锁转载自:www.javaman.cn1、悲观锁、乐观锁乐观锁和悲观锁是两种用于处理并发操作的数据锁定策略。它们在处理多个事务尝试同时访问和修改同一数据时的方法有所不同。悲观锁(PessimisticLocking):概念:悲观锁是一种基于悲观态度的数......
  • Mybatis-plus逻辑删除
    转载自:www.javaman.cn1、application.yml配置mybatis-plus:表示这是MyBatis-Plus的配置部分。global-config:全局配置。db-config:数据库相关配置。logic-delete-field:指定逻辑删除的字段名。在这里,指定的字段名是deleted。这意味着,当你调用MyBatis-Plus的逻辑......