Configuration summary] [Mybatis06 SqlMapConfig.xml file

This post is republished from https://blog.csdn.net/eson_15/article/details/51604171

  After summary on two blog posts, and to develop methods and processes mybatis dao in basic grasp of this section to summarize some of the major common mybatis in the global configuration file SqlMapConfig.xml configuration in development, first look at the global What are things that can be configured in the configuration file are:

Configuring Content effect
<properties> Used to load the properties file
<settings> To set global parameters
<typeAliases> To set the type of alias
<typeHandlers> To set the type of processor
<objectFactory> To set the object factory
<plugins> To set the plug-in
<environments> To set mybatis environment
<mappers> Used to configure the mapping file

  As can be seen from the foregoing post, <environments>it is used to configure mybatis environment, and the integration is mybatis Spring before they manage, but the Spring and integration, these configurations will be discarded, <environments>part of the already mentioned there are been to, I will not repeat them here. Then objectFactory and plugins in general we do not, so here is not summed up, <typeHandlers>the type of processor is used to convert between mybatis in jdbc type and the type of java, mybatis default support basic type of processor enough for us to use, and generally do not You need to customize. This blog is mainly to summarize the use of the few remaining configuration.

1. properties load the properties file

  Some of our connection to the database configuration before all the <environments>hardcoded in, in the actual development is certainly not like this, we need to separate the database connection parameters with the db.properties file and then load the db.properties in SqlMapConfig.xml the property values can be, which uses <properties>up. which is:

<properties resource="db.properties">
  
  
  • 1

  Then in the environment to do something corresponding modifications:
Configuration
  In <properties>can also define an internal attribute value: <property name="" value=""/>, inside the property attribute it may also be <evironments>in to load, which relates to the properties of the loading sequence mybatis:

  1. In the <properties>first reading element defined vivo
  2. Then reads the <properties>element or resource loading url attribute, it overrides the read attribute of the same name
  3. ParameterType final read transfer property ($ {} used in this manner, this embodiment is not # {}), it overrides the read attribute of the same name

  Suggestion : You can see it from above, if the <properties>defined attributes, a bit chaotic, could well mistake, so development do not <properties>add any attribute value of the element body, only attribute values are defined in the properties file. In addition attribute names have a certain particularity defined in the properties file, such as xxx.xxx, this is not easy and some other property values in conflict.

2. settings global parameters

  mybatis framework at runtime can adjust some of the operating parameters, such as: open second-level cache, turn on lazy loading, etc., there is a global parameter settings configuration files:
Configuration settings
  These configuration parameters affect the operation of the global behavior of mybatis, when needed re-setting, not when needed can not be arbitrary set here it does not described in detail, so when I used back then to do something for the corresponding presentation. To tell the truth, I do not bother so much to see ......

3. typeAliases type alias

  这是配置中的一个重点,我们知道,在mapper.xml中定义了很多的statement,statement需要parameterType来指定输入参数的类型、需要resultType来指定输出结果的类型。如果在指定类型时输入类型全路径,有时候会很长,不方便进行开发,那么我们就可以可以针对parameterType或resultType指定的类型定义一些别名,在mapper.xml中通过别名<typeAliases>来定义,方便开发。
mybatis有默认支持的一些别名,一般基本类型都有别名,如下:
Aliases
  但是针对pojo的需要我们自定义别名了,比如我们将自己定义的User对象取个别名为user,如下:

<!-- 别名的定义 -->
<typeAliases>
    <!-- 针对单个别名的定义。type:类型的路径; alias:别名 -->     
    <typeAlias type="mybatis.po.User" alias="user"/>
</typeAliases>
  
  
  • 1
  • 2
  • 3
  • 4
  • 5

  然后输入参数或者输出结果为mybatis.po.User时,就可以用user来代替了,如下:
Aliases
  但是问题来了,如果工程中有很多pojo,那岂不是完蛋……这得定义多少个啊,所以mybatis帮我们解决了这个问题,它提供了批量别名的定义,如下:

<!-- 批量别名定义(这个常用)
指定一个包名,mybatis会自动的扫描包中po类,自动定义别名,别名就是类名(首字母大写或小写都可以) -->
<package name="mybatis.po"/>
  
  
  • 1
  • 2
  • 3

  这就爽了,如果进行了上面这样设置,那么所有放在mybatis.po包下的pojo都可以不用定义了,直接使用类名就可以了,所以我们直接使用user即可。毫无疑问,开发中肯定使用这个批量的别名定义。

3. mappers映射配置

  在前面的几篇博文中可以看到,<mappers>标签是通过resource方法加载单个映射文件的,即:

<mappers>
    <mapper resource="sqlmap/User.xml" />
    <mapper resource="mapper/UserMapper.xml" />
</mappers>
  
  
  • 1
  • 2
  • 3
  • 4

  当然咯,除了resource外,也可以用url来加载,只不过url指定的是绝对路径,硬盘中的路径。这里就不举例了,这里介绍另一种加载映射文件的方式:通过mapper接口来加载。通过mapper接口来加载的话要遵循一个规范:

规范:需要将mapper接口类名和mapper.xml映射文件名称保持一致,且在一个目录中
这个规范的前提是:使用的是mapper代理的方法

  所以上面的配置就可以改成

<mappers>
    <mapper resource="sqlmap/User.xml" />
    <mapper class="mybatis.mapper.UserMapper"/>
</mappers>
  
  
  • 1
  • 2
  • 3
  • 4

  所以要把原来的UserMapper.xml从config/mapper中的拖到和UserMapper.java一个目录下且同名,如下:
mapper interface configuration
  但是还是有个弊端,就跟上面那个别名的配置一样,如果现在有好多个mapper咋整,所以mybatis也给我们提供了批量加载方法:我们只要指定mapper接口的包名,mybatis自动扫描包下面所有的mapper接口进行加载。当然,还是要遵循上面的那个规范。所以我们可以修改上面的配置:

<mappers>
    <mapper resource="sqlmap/User.xml" />
    <mapper class="mybatis.mapper"/>
</mappers>
  
  
  • 1
  • 2
  • 3
  • 4

  这样mybatis.mapper包下的所有mapper.xml映射文件都可以加载了,这就更加简洁了。在开发中推荐使用这种批量加载映射文件的方法。
  关于SqlMapConfig.xml文件中的配置就总结这么多吧~
  

补充:

1. 配置文件中的标签和顺序
properties?, 						配置属性(学习)
settings?, 							全局配置:缓存,延迟加载
typeAliases?, 					类型别名(学习)
typeHandlers?, 					类型转换(操作)(了解)
objectFactory?, 	 objectWrapperFactory?,  reflectorFactory?, 
plugins?, 							插件:分页插件
environments?, 					环境配置(数据源)
databaseIdProvider?, 		
mappers?								引入映射配置文件(学习)

? : 一个或者零个
| : 任选其一
+ : 最少一个
* : 零个或多个
, : 必须按照此顺序编写

——————————————————————————————————————

  • 真正开发中数据库连接参数是放在properties文件中的,在SqlMapConfig.xml中用<properties resource=“db.properties”>引入
  • 参数用比如value=${jdbc.driver}来接
  • <typeAliases>设置别名,<package name="…">批量定义
  • <mappers> tag loading by a single resource mapping file,
    - Mapper may be used to load the interface
    specification is: Mapper interface class names, and file names need to be mapped Mapper.xml consistent and in the same directory in
    this specification is provided: the use of mapper proxy way to develop
    cases,
<mappers>											<mappers>
	<mapper resource="mapper/UserMapper.xml">还可以		<mapper class="mybatis.mapper.UserMapper">
</mappers>											<mappers>

If bulk loading, the package name is the class specified interface.

The following supplements on SqlMapConfig.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!--属性配置-->
    <!--<properties>-->
        <!--<property name="jdbc.driver" value="com.mysql.jdbc.Driver"></property>-->
        <!--<property name="jdbc.url" value="jdbc:mysql://localhost:3306/mybatisdb_331"></property>-->
        <!--<property name="jdbc.username" value="root"></property>-->
        <!--<property name="jdbc.password" value="root"></property>-->
    <!--</properties>-->
    <!--引入外部属性文件-->
    <properties resource="jdbc.properties"></properties>
    <!--
    ${jdbc.driver}:ognl表达式
    在xml中没有el表达式
    -->

    <!--类型别名映射-->
    <typeAliases>
        <!--单个类型映射-->
        <!--<typeAlias type="com.itheima.domain.User" alias="user"></typeAlias>-->
        <!--包的映射:引入该包中所有的pojo类型,配置别名: 简单类名(不区分大小写)-->
        <package name="com.itheima.domain"></package>
    </typeAliases>

    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driver}"/>
                <property name="url" value="${jdbc.url}"/>
                <property name="username" value="${jdbc.username}"/>
                <property name="password" value="${jdbc.password}"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <!--引入UserMapper.xml配置文件-->
        <!--<dao resource="com/itheima/dao/UserDao.xml"/>-->
        <!--引入UserMapper的配置文件:url:绝对路径-->
        <!--<mapper url="file:///F:\ideaworkspace\project_331\mybatis_day02_4_config\src\main\resources\com\itheima\dao\UserDao.xml"></mapper>-->
        <!--引入UserDao接口
            通过接口引入配置:
                前提:必须在同一个包中
                      文件名称必须一致
        -->
        <!--<mapper class="com.itheima.dao.UserDao"></mapper>-->
        <!--
            通过接口引入配置:
                前提:必须在同一个包中
                      文件名称必须一致
                -->
        <!--引用一个包中的所有dao接口-->
        <package name="com.itheima.dao"></package>
    </mappers>
</configuration>

Guess you like

Origin blog.csdn.net/weixin_44212815/article/details/93039970