Detailed explanation of MyBatis global configuration file

MyBatis global configuration file

The configuration file of MyBatis contains settings and properties information that deeply affect the behavior of MyBatis. Document

The top-level structure is as follows:

configuration

• properties

• settings

• typeAliases type naming

• typeHandlers type handler

• objectFactory object factory

• plugins

• environments

• environment environment variables

• transactionManager transaction manager

• dataSource data source

• databaseIdProvider database vendor ID

• mappers

properties

mybatis can use properties to introduce the content of external properties configuration files

resource: Introduce resources under the classpath

url: Introduce resources under the network path or disk path

<properties resource="dbconfig.properties"></properties>

If the properties are configured in more than one place, MyBatis will be loaded in the following order:

– The properties specified in the properties element body are read first.

– Then read the property file under the classpath according to the resource attribute in the properties element or read the property file according to the path specified by the url attribute, and overwrite the read attribute with the same name. – Finally read the attribute passed as a method parameter, and overwrite the read attribute with the same name.

settings

These are extremely important adjustment settings in MyBatis, and they will change the runtime behavior of MyBatis.

setting contains many important setting items

setting: used to set each setting item

name: set item name

value: the value of the setting item

mapUnderscoreToCamelCase: CamelCase named last_name -> lastName

    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>

typeAliases type naming

typeAliases: You can set aliases for each class under this package in batches

Aliases are not case sensitive

Create a default alias, which is a lowercase simple class name

typeAlias: alias a java type

type: Specify the full class name of the type to be aliased; the default alias is the lowercase of the class name: employee

alias: specify a new alias

package: batch aliases for all classes under a certain package

name: Specify the package name (a default alias (lowercase class name) for each class under the current package and all descendant packages below)

Existing problems:

Suppose there is a class Employee under the bean package, and there is a subpackage under the bean package and a class called Employee. At this time, the class names of the two packages will conflict, so you can also use annotations to create aliases for the class

 @Alias("")

public class Employee{}

It is worth noting that MyBatis has built-in corresponding type aliases for many common Java types. They are all case-insensitive, and we must not use existing aliases when creating aliases.

environments

Mybatis can be configured with a variety of environments. Default specifies the use of a certain environment to achieve fast switching

environments: configure a specific environment information; there must be two tags, id represents the unique identifier of the current environment

transactionManager transaction manager

type: The type of transaction manager [JDBC (using the commit and rollback settings of JDBC, depends on the connection obtained from the data source to manage the scope of the transaction.) | MANAGED (do not commit or roll back a connection, let the container manage the transaction The entire life cycle (such as the context of the JEE application server)]

JDBC:JdbcTransactionFactory

A custom transaction manager can implement the TransactionFactory interface

dataSource data source

type: The type of data source [UNPOOLED (Do not use connection pool)|POOLED (Use connection pool)|JNDI (Find the specified data source in containers such as EJB or application server)]

Custom data source implements DataSourceFactory interface

In actual development, we use Spring to manage the data source and configure the transaction control to cover the above configuration

databaseIdProvider database vendor ID

databaseIdProvider: Different statements can be executed according to different database vendors.

type: database type DB_VENDOR

The function is to get the database vendor identification (the driver comes with it), mybatis can execute different SQL according to the database vendor identification

MYSQL Oracle SQL Server

value: an alias for the identification, convenient for SQL statements to use the databaseId attribute to reference

DB_VENDOR

– It will be set by the string returned by DatabaseMetaData#getDatabaseProductName(). Since this string is usually very long and different versions of the same product will return different values, it is best to set an attribute alias to make it

Shorten

• MyBatis matching rules are as follows:

– 1. If the databaseIdProvider tag is not configured, then databaseId=null

– 2. If the databaseIdProvider tag is configured, use the name configured on the tag to match the database information, and set databaseId= the value specified by the configuration on the match, otherwise it will still be null

– 3. If the databaseId is not null, he will only find the SQL statement that configures the databaseId

– 4. MyBatis will load all statements without the databaseId attribute and with the databaseId attribute matching the current database. If the same statement with databaseId and without databaseId is found at the same time, the latter will be discarded.

mappers

mapper: register a sql mapping

Register profile

resource: reference the sql mapping file under the classpath

url: Use the sql mapping file in the network path or the disk path

Registration interface

class: cited application interface

1. Interface mapping file. The name of the mapping file must be the same as the interface name and placed in the same directory

2. There is no sql mapping file, all sql is written on the interface with annotations

Annotations are written on the interface

Recommendation: It is more important and complicated to write sql mapping files in Dao interface, not important and simple, you can use annotations for quick development

package: batch registration

sql mapping file

namespace: namespace; specified as the full class name of the interface

id: unique identifier

resultType: return value type

#{id}: Take out the id value from the passed parameter

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="school.xauat.mybatis.dao.EmployeeMapper">
    <select id="getEmpById" resultType="employee" >
        select * from tbl_employee where id = #{id}
    </select>
    <select id="getEmpById" resultType="employee" databaseId="mysql">
        select * from tbl_employee where id = #{id}
    </select>
    <select id="getEmpById" resultType="employee" databaseId="oracle">
        select * from tbl_employee where id = #{id}
    </select>
</mapper>

to sum up

1. The interface is programming

Primitive: Dao -----> DaoImpl

Mybatis:        Mapper  ----->  xxMapper.xml

2. SqlSession represents a session with the database, which must be closed when used up

3. SqlSession, like connection, is non-thread safe. You should get a new object every time you use it

4. The mapper interface does not have an implementation class, but Mybatis will generate a proxy object for this interface (binding the interface and xml)

EmployeeMapper empMapper = SqlSession.getMapper(Employee.class);

5. Two important configuration files

Mybatis global configuration file; contains database connection pool information, transaction manager information, system operation information

sql mapping file: saves the mapping information of each SQL statement

Extract sql

Guess you like

Origin blog.csdn.net/qq_45796208/article/details/110508432