Mybatis configuration file detailed

MyBatis is an open source project of Apache, iBatis. In 2010, this project was migrated from apache software foundation to google code and renamed to MyBatis. Moved to Github in November 2013.

The term iBATIS comes from the combination of "internet" and "abatis" and is a Java-based persistence layer framework. The persistence layer framework provided by iBATIS includes SQL Maps and Data Access Objects (DAOs)

MyBatis is an excellent persistence layer framework that supports custom SQL, stored procedures, and advanced mapping. MyBatis avoids almost all JDBC code and manually sets parameters and gets result sets. MyBatis can use simple XML or annotations to configure and map native information, mapping interfaces and Java POJOs (Plain Old Java Objects) to records in the database.

Mybatis configuration file detailed

Detailed Mybatis configuration file

First, the scope of use of SqlSession

1, SQLSessionFactoryBuilder

Create a session factory SqlSessionFactory through SqlSessionFactoryBuilder, use SqlSessionFactoryBuilder as a tool class, do not need to use singleton mode management, when creating SqlSessionFactory, you only need new SqlSessionFactoryBuilder.

2, SqlSessionFactory

Create a SqlSession through the SqlSessionFactory and use the singleton pattern to manage the SqlSessionFactory (when the factory is created, only one instance is used).

3, SqlSession

SqlSession is an interface for users (programmers).

SqlSession provides a number of methods for manipulating databases: selectOne (returns a single object), selectList (returns single or multiple objects).

SqlSession is thread insecure, because in SqlSession, in addition to the methods in the interface (the method of operating the database). There are also data field properties.

The best application for SqlSession is in the body of the method, which is defined as a local variable.

Second, SqlMapConfig.xml configuration file detailed

1, Properties property

The Properties property is used to load the property configuration file.

"! --

Load properties file

Resource: relative path of the property file

Url: the absolute path of the property file

--"

"properties resource="config/db.properties"

"! --

Configure some properties

Name: the name of the attribute

Value: the value of the attribute

--"

"! -- "property name="" value="" /" --"

/properties

Properties:

Mybatis will load the properties according to the following properties:

1. The properties defined in the property tag will be read first.

2. It will then read the properties when loaded via resource or url in the properties tag, which will override the properties that have been read with the same name.

3. Finally, the attribute passed in the parameterType of sql will be read, which will overwrite the attribute of the same name that has been read.

2, settings global parameter configuration

The Mybatis framework can adjust some running parameters at runtime, such as: enabling the second level cache, enabling lazy loading, and so on. These parameters

To be configured through the settings tab, the global parameters will affect the running behavior of Mybatis, the configuration must pay attention.

"! --Global parameter configuration --"

"settings"

"! --

Name: attribute name

Value: the value of the attribute

--"

"setting name="" value=""/"

/settings

Common global configuration parameters are as follows:

Mybatis configuration file detailed

Mybatis configuration file detailed

Mybatis configuration file detailed

3, typeAlia

In the parameterType and resultType attributes in the mapping file, you need to specify the input and output mapping types. If the type is PO, it is not particularly convenient to specify the full path of the PO class. You can use typeAlias ​​to define some aliases to facilitate development.

"! --Defining an alias --"

typeAliases

"! --

Type: a string that needs to be aliased

Alias: the corresponding alias

--"

typeAlias ​​type=“” alias=”” /》

"! --

Name: the package name, the class below the package will be scanned, and the alias will be set automatically.

The alias set is the class name, and the initials can be capitalized.

--"

"package name="" /"

/typeAliases

Myabais default alias

Mybatis configuration file detailed

4, typeHandlers (type processor)

Mybatis completes the jdbc type and Java type swapping through typeHandlers. Normally, the type processor provided by Mybatis meets daily needs and does not require us to customize it.

"typeHandlers"

"typeHandler handler="" jdbcType="" javaType="" /"

"/typeHandlers"

The data types supported by Mybatis by default

Mybatis configuration file detailed

5, environment environment

"! --

Environmental configuration

After the integration with Spring, the configuration will be abolished.

--"

"environments default="development"

"environment id="development"

"! -- Using JDBC transaction management, transaction management is handed over to Mybatis --"

"transactionManager type="JDBC" "/transactionManager"

"! --Database connection pool, with Mybatis management --"

"dataSource type="POOLED"

"property name="driver" value="com.mysql.jdbc.Driver" /"

"property name="url" value="jdbc:mysql://localhost:3306/mybatis" /"

"property name="username" value="root" /"

"property name="password" value="root" /"

/dataSource

/environment

/environments

6, mapper load map file

There are three ways to load a map file.

1, load a single mapping file

"! --Introduction of mapper--"

Mappers

"! --rerource: Introduce the address of the mapping file relative to the classpath --"

Mapper resource=“config/sqlmap/userMapper.xml” /》

/mappers

2, load a single mapping file through the Mapper interface

Prerequisites:

(1). Develop using the Mapper proxy.

(2). The Mapper interface file name must be the same as the mapping file name.

(3). Mapper interface file and mapping file are in the same directory

"! --Introduction of mapper--"

Mappers

Mapper class=“com.jack.po.UserMapper” /》

/mappers

3, batch record mapping file

Prerequisites:

(1). Develop using the Mapper proxy.

(2). The Mapper interface file name must be the same as the mapping file name.

(3). Mapper interface file and mapping file are in the same directory

"! --Introduction of mapper--"

Mappers

"! --name: package path where the mapper interface file is located --"

"package name="" /"

/mappers

Stainless Steel Decorative Tube

ShenZhen Haofa Metal Precision Parts Technology Co., Ltd. , https://www.haofametals.com