mybatis 主配置文件各个标签的分析

Configuration

The MyBatis configuration contains settings and properties that have a dramatic effect on how MyBatis behaves. The high level structure of the document is as follows:

  1. configuration
  2. properties
  3. settings
  4. typeAliases
  5. typeHandlers
  6. objectFactory
  7. plugins
  8. environments
  9. environment
  10. transactionManager
  11. dataSource
  12. databaseIdProvider
  13. mappers

为根标签,主配置文件所有的标签都在这个下面

properties: 配置文件

<properties resource="org/mybatis/example/config.properties">
  <property name="username" value="dev_user"/>
  <property name="password" value="F2Fa3!33TYyg"/>
</properties>

这样就可以自己在其他地方可以使用${username} 的方式引用了
如果一个属性在不只一个地方进行了配置,那么,MyBatis 将按照下面的顺序来加载:

  • 首先读取在 properties 元素体内指定的属性。
  • 然后根据 properties 元素中的 resource 属性读取类路径下属性文件,或根据 url 属性指定的路径读取属性文件,并覆盖之前读取过的同名属性。
  • 最后读取作为方法参数传递的属性,并覆盖之前读取过的同名属性

上面的结论来自下面的这些源码部分

 private void propertiesElement(XNode context) throws Exception {
    if (context != null) {
    //读取在 properties 元素体内指定的属性
      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.");
      }
      //根据 properties 元素中的 resource 属性读取类路径下属性文件,或根据 url 属性指定的路径读取属性文件
      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);
    }
  }

简单理解: 存在source或者url以他们为准,没有以properties 元素体内指定的属性为准

settings 一些设置,比如缓存,懒加载等配置

这是 MyBatis 中极为重要的调整设置,它们会改变 MyBatis 的运行时行为。 下表描述了设置中各项设置的含义、默认值等。
这里不截图了 参考官方文档吧

<settings>
        <setting name="lazyLoadingEnabled" value="true"/><!-- 使用延迟加载 -->
        <setting name="aggressiveLazyLoading" value="false"/><!-- 按需加载对象属性(即访问对象中一个懒对象属性,不会加载对象中其他的懒对象属性) -->
        <setting name="cacheEnabled" value="true"/><!-- 开启二级缓存,默认是false -->
        <setting name="callSettersOnNulls" value="true"/><!--解决,查询返回结果含null没有对应字段值问题-->
        <setting name="mapUnderscoreToCamelCase" value="true"/> <!--开启驼峰命名,比如 mysql 中的字段  User_Code  对应Java bean中的userCode -->
    </settings>

typeAliases

<typeAliases>
        <!--单个类命名-->
        <typeAlias type="com.hrp.sys.entity.SysUser" alias="sysUser"/>
        <!--也可以使用注解@Alias-->
        <!--批量将一个包下的所有类都起别名-->
        <package name="com.hrp.sys.entity"></package>
    </typeAliases>

mybatis 中已经定义好的别名 基本类型是前面加_
在这里插入图片描述
在这里插入图片描述
小写

类型处理器(typeHandlers)

typeHandler
在这里插入图片描述
在这里插入图片描述

插件(plugins)

MyBatis 允许你在映射语句执行过程中的某一点进行拦截调用。默认情况下,MyBatis 允许使用插件来拦截的方法调用包括:

Executor (update, query, flushStatements, commit, rollback, getTransaction, close, isClosed)
ParameterHandler (getParameterObject, setParameters)
ResultSetHandler (handleResultSets, handleOutputParameters)
StatementHandler (prepare, parameterize, batch, update, query)
这些类中方法的细节可以通过查看每个方法的签名来发现,或者直接查看 MyBatis 发行包中的源代码。 如果你想做的不仅仅是监控方法的调用,那么你最好相当了解要重写的方法的行为。 因为在试图修改或重写已有方法的行为时,很可能会破坏 MyBatis 的核心模块。 这些都是更底层的类和方法,所以使用插件的时候要特别当心。

通过 MyBatis 提供的强大机制,使用插件是非常简单的,只需实现 Interceptor 接口,并指定想要拦截的方法签名即可。

后面再专写一个专题关于插件的

环境配置(environments)

<!--
        environment 元素中必须包含transactionManager 和 datasource
        transactionManager type="[JDBC|MANAGED]"
            typeAliasRegistry.registerAlias("JDBC", JdbcTransactionFactory.class);
            typeAliasRegistry.registerAlias("MANAGED", ManagedTransactionFactory.class);
            如果你正在使用 Spring + MyBatis,则没有必要配置事务管理器,因为 Spring 模块会使用自带的管理器来覆盖前面的配置
        dataSource
        type="[UNPOOLED|POOLED|JNDI]"
            typeAliasRegistry.registerAlias("JNDI", JndiDataSourceFactory.class);
            typeAliasRegistry.registerAlias("POOLED", PooledDataSourceFactory.class);
            typeAliasRegistry.registerAlias("UNPOOLED", UnpooledDataSourceFactory.class);
        数据源我们更多的使用第三方的,比如c3p0,druid等
        也可以自定义    
    -->
    <environments default="development">
        <environment id="test">
            <transactionManager type="MANAGED"></transactionManager>
            <dataSource type="JNDI">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="{url}"/>
                <property name="username" value="{username}"/>
                <property name="password" value="{password}"/>
            </dataSource>
        </environment>
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="{url}"/>
                <property name="username" value="{username}"/>
                <property name="password" value="{password}"/>
            </dataSource>
        </environment>
    </environments>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值