MyBatis (3) global configuration file

mybatis-config.xml

1.properties property:

   These properties are externally configurable and dynamically replaceable, either in a typical Java properties file or passed through a child element of the properties element, for example:

db.properties:


    jdbc.driver=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql:///mybatis
    jdbc.user=root
    jdbc.password=123456

Use the properties tag to reference the properties file in the mybatis-config.xml file:


    <!-- 
        mybatis 可以引入 properties 文件 :
            1.resource:引入类路径下的文件
            2.url:引入网络或磁盘文件
     -->
    <properties resource="db.properties"></properties>

The properties in it can then be used throughout the configuration file to replace property values ​​that need to be dynamically configured. for example:


    <dataSource type="POOLED">
        <property name="driver" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.user}" />
        <property name="password" value="${jdbc.password}" />
    </dataSource>

2.settings settings:

3.typeAliases alias processor

- Set a simple alias for a java type, the default is lowercase class name, you can also use alias to re-specify the alias:

    <typeAliases>
        <typeAlias type="cn.edu.pzhu.cg.entities.Employee" alias="Employee"/> 
    </typeAliases>
- In the case of many classes, you can set aliases in batches. Each class under this package creates a default alias, which is a simple class name in lowercase:

    <typeAliases>
       <package name="cn.edu.pzhu.cg.entities"/>
    </typeAliases>
- If there are classes with the same class name in multiple packages, you can use the annotation @Alias ​​to specify a new alias for it:

    @Alias("emp")
    public class Employee{
        ...
    }

4.environments

  • MyBatis can be configured with multiple environments, such as development, test and production environments that require different configurations.
  • Each environment is configured with an environment tag and specifies a unique identifier
  • You can quickly switch environments by specifying an environment identifier with the default attribute in the environments tag

    <environments default="development_MySQL">
        <environment id="development_MySQL">
            <transactionManager type="JDBC" />
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driver}" />
                <property name="url" value="${jdbc.url}" />
                <property name="username" value="${jdbc.user}" />
                <property name="password" value="${jdbc.password}" />
            </dataSource>
        </environment>

        <environment id="development_Oracle">
            <transactionManager type="JDBC" />
            <dataSource type="POOLED">
                <property name="driver" value="oracle.jdbc.driver.OracleDriver" />
                <property name="url" value="jdbc:oracle:thin:@localhost:1521/test" />
                <property name="username" value="root" />
                <property name="password" value="123456" />
            </dataSource>
        </environment>
    </environments>

5.dataSource

6.databaseIdProvider environment




7.mapper mapping

Use classpath-relative resource references:

    <mappers>
       <mapper resource="EmployeeMapper.xml" />
    </mappers>
Use a fully qualified resource locator (URL:

    <mappers>
      <mapper url="file:///var/mappers/AuthorMapper.xml"/>
      <mapper url="file:///var/mappers/BlogMapper.xml"/>
      <mapper url="file:///var/mappers/PostMapper.xml"/>
    </mappers>
The fully qualified class name of the class implementing the mapper interface:

    <mappers>
      <mapper class="org.mybatis.builder.AuthorMapper"/>
      <mapper class="org.mybatis.builder.BlogMapper"/>
      <mapper class="org.mybatis.builder.PostMapper"/>
    </mappers>
Register all mapper interface implementations in the package as mappers
<mappers>
    <package name="cn.edu.pzhu.cg.dao"/>
</mappers>

Register interface:

class: reference (register) interface

  1. There is a sql mapping file, the mapping file name must be the same as the interface file name, and placed in the same directory
  2. There is no sql mapping file, all sql is written on the interface using annotations

recommend:

  1. Mapping files for more important and complex Dao interfaces;
  2. Unimportant, simple Dao interfaces can use annotations for quick development.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324770038&siteId=291194637