Mybatis-config.xml description

Time:2021-6-12

Configuration

properties

The content of external properties configuration file is introduced;

  • Resource: introduce resources under classpath
  • URL: introduce resources under network path or disk path
<properties resource="dbconfig.properties"></properties>

settings

Settings contains many important settings
Setting: used to set each setting item

  • Name: set item name
  • Value: set the value of the item
<settings>
    <setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>

typeAliases

Alias processor: you can alias our Java types. Aliases are not case sensitive

1. Typealias: alias a Java type
Type: Specifies the full class name of the type to be aliased; The default alias is the lowercase class name; employee
Alias: specifies a new alias

<typeAlias type="com.atguigu.mybatis.bean.Employee" alias="emp"/>

2. Package: batch alias all classes under a package
Name: Specifies the package name (a default alias (lowercase class name) for each class of the current package and all its descendants)

<package name="com.atguigu.mybatis.bean"/>

3. In the case of batch aliasing, use the @ alias annotation to specify a new alias for a type

plugins

1、TODO

environments

Mybatis can be configured with a variety of environments. Default specifies to use a certain environment. It can switch environment quickly.

Environment: configure a specific environment information; There must be two labels; ID represents the unique identity of the current environment

  • Transaction manager: transaction manager;
    Type: the type of transaction manager;

     JDBC(JdbcTransactionFactory)|MANAGED(ManagedTransactionFactory)

    Custom transaction manager: implements the transactionfactory interface. Type is specified as the full class name

  • Datasource: data source;
    Type: data source type;

    UNPOOLED(UnpooledDataSourceFactory)
    |POOLED(PooledDataSourceFactory)
    |JNDI(JndiDataSourceFactory)

    Custom data source: implement datasourcefactory interface, type is the name of the whole class

databaseIdProvider

type=”DB_VENDOR”: org.apache.ibatis.mapping.VendorDatabaseIdProvider
The function is to get the database vendor’s identity (drive getdatabaselactam ()), and mybatis can execute different SQL according to the database vendor’s identity;

<databaseIdProvider type="DB_VENDOR">
        <!--  Alias different database vendors -- >
        <property name="MySQL" value="mysql"/>
        <property name="Oracle" value="oracle"/>
        <property name="SQL Server" value="sqlserver"/>
    </databaseIdProvider>

mappers

The SQL mapping file (employeemapper. XML) we have written must be registered in the global configuration file (mybatis config. XML)
The role of mappers is to register the SQL mapping to the global configuration

  • Registration profile

Resource: reference the SQL mapping file under the classpathmapper/EmployeeMapper.xml
URL: refers to the SQL mapping file under the network path or disk pathfile:///var/mappers/AuthorMapper.xml

<mapper resource="mapper/EmployeeMapper.xml"/>
  • Registration interface

Class: Reference (Registration) interface
There is a SQL mapping file. The mapping file name must have the same name as the interface and be placed in the same directory as the interface;
There is no SQL mapping file, and all SQL statements are written on the interface with annotations;

<!-- There are SQL mapping files -- >
<mapper class="com.atguigu.mybatis.dao.EmployeeMapper"/>
<mapper 
<!-- No SQL mapping file -- >
class="com.atguigu.mybatis.dao.EmployeeMapperAnnotation"/>

recommend:
More important, complex Dao interface, let’s write SQL mapping file
It’s not important. Simple Dao interface can use annotation for rapid development;

  • Batch registration

Name: Specifies the package name of the batch registration interface

<package name="com.atguigu.mybatis.dao"/>

When using batch registration, if you want to put all SQL mapping files in a separate directory, a good way is to create a new conf directory (source code folder) in the classpath, and create a new package corresponding to Dao interface in the directorycom.xxxx.dao, and then put all the XML Mapping Files in it, so that the files in the source folder conf directory will be merged into the classpath. In fact, these XML files are in the same classpath as the interface
Mybatis-config.xml description