Introduction of SqlSession

The SqlSession object is a primary interface in Mybatis through which commands can be executed to obtain mapper samples and manage transactions.

SqlSession between provides the operation method of database, such as: the session. The selectOne (” org. Mybatis. Example. BlogMapper. SelectBlog “, 101). However, these methods are generally not used. Instead, Mapper (dao interface) mapping files are configured and proxy objects of Mapper are obtained through SqlSession.

SqlSession is created by SqlSessionFactory. The SqlSessionFactory openSession method has an overloaded form: OpenSession (Boolean autoCommit), autoCommit specifies whether to commit automatically (i.e., whether to start a transaction). The default value is false. So by default, the update, delte, and INSERT statements require a transaction commit, which is the COMMIT () method of SqlSession, otherwise the transaction will be rolled back. You can also use openSession(true) to obtain the SqlSession that closes the transaction.

In addition, SqlSession objects are not thread-safe, so each thread should have its own SqlSession object and close SqlSession: session.close() after each database operation.

Master profile

File structure

Master configuration file structure: (See the official documentation for details: mybatis.org/mybatis-3/z…

Configuration

  • Properties

  • Settings

  • typeAliases

  • typeHandlers

  • objectFactory

  • Plugins

  • Environments

    • Environment (environment variable)
      • transactionManager
      • A dataSource
  • DatabaseIdProvider (Database vendor Identity)

  • Mappers (mapper)

The following describes some common tags. See the official documentation for details on the use of other tags.

<properties>

This label is used to configure parameters related to connecting to the database. It can be configured through the subtag or referenced to an external configuration file using the Resource property, and the two methods can be mixed. ${paramName} is used to obtain the value of the

configuration.

For example, you can change the main configuration file in Quick Start to:

/ /... omit<properties resource="jdbc.properties" />

<environments default="development">
    <environment id="development">
        <transactionManager type="JDBC"/>
        <dataSource type="POOLED">
            <property name="driver" value="${driver}"/>
            <property name="url" value="${url}"/>
            <property name="username" value="${username}"/>
            <property name="password" value="${password}"/>
        </dataSource>
    </environment>
</environments>/ /... omitCopy the code

Jdbc.properties :(this file is in the resource directory)

driver=com.mysql.jdbc.Driver
url=JDBC: mysql: / / 127.0.0.1:3306 / test? serverTimezone=UTC
username=root
password=admin
Copy the code

Property information is normally configured in the properties file, not the property tag.

<settings>

< Settings > is used to configure some global variables in Mybatis. For example, lazy loading is enabled:

<settings>
    <setting name="lazyLoadingEnabled" value="true"/>
</settings>
Copy the code

Mybatis has a number of global variables, other variables are detailed at mybatis.org/mybatis-3/z…

<typeAliases>

The purpose of this tag is to specify an abbreviated alias for a Java class rather than using its fully qualified name every time you specify the class.

Such as:

The type attribute specifies the fully qualified name of the class; The alias property specifies the alias.

<typeAliases>
  <typeAlias alias="author" type="domain.blog.Author"/>
</typeAliases>
Copy the code

With this configuration, you can replace domain-blog.autho with author, case insensitive, wherever it is used.

It is also possible to use the < Package > tag in the

tag to specify a package name that aliases all classes in this package by using the unqualified class name of the lowercase Bean. For example, the alias of doma.blog. blog is blog. This method is more recommended.

Example:

<typeAliases>
  <package name="domain.blog"/>
</typeAliases>
Copy the code

<environments>


is used to configure the environment. Multiple database environments can be configured, but only one environment can be selected. Use default to specify the ID of

to be used. Each instance of SqlSessionFactory can correspond to only one environment. If you want to connect to two databases, you need two SQlsessionFactories.

Configure the transactionManager in

via
.

The following is from the official website:

In MyBatis there are two types of transaction manager (i.e., type = “[JDBC | MANAGED]”) :

  • JDBC – This configuration directly uses JDBC’s commit and rollback facilities, which rely on connections obtained from data sources to manage transaction scopes.

  • MANAGED – This configuration does little. It never commits or rolls back a connection, but lets the container manage the entire life cycle of the transaction (such as the context of the JEE application server). It closes the connection by default. However, some containers do not want the connection to be closed, so you need to set the closeConnection property to false to prevent the default closing behavior

  • If you are using Spring + MyBatis, there is no need to configure the transaction manager because the Spring module uses its own manager to override the previous configuration.

The

element uses the standard JDBC dataSource interface to configure the JDBC connection object’s resources. Mybatis provides three dataSource types:

  • UNPOOLED: Opens one connection each time a database connection is requested and closes the connection when it is finished. The concept of connection pooling is not used. Suitable for simple applications that do not require high availability of database connections.
  • POOLED: Connection pooling is used to avoid the initialization and authentication time required to create new connection instances.
  • JNDI: Puts a datasource reference to a JNDI context.

Example:

<environments default="development">
    <environment id="development">
        <transactionManager type="JDBC"/>
        <dataSource type="POOLED">
            <property name="driver" value="${driver}"/>
            <property name="url" value="${url}"/>
            <property name="username" value="${username}"/>
            <property name="password" value="${password}"/>
        </dataSource>
    </environment>
</environments>
Copy the code

<mappers>

This tag is used to specify the location of the mapping file, that is, to tell Mybatis where to find the mapping file corresponding to Mapper.

There are several configuration modes:

  • Using the first approach, you look for the mapping files in the Resource directory (under the classpath). The directory structure of the mapping file must be consistent with the package structure of the corresponding Mapper, for exampleorg/mybatis/builder/UserMapper.xmlThe correspondingorg.mybatis.builder.UserMapper. ** and note that the delimiter is/instead of. **
<! -- Use a resource reference relative to the classpath -->
<mappers>
  <mapper resource="org/mybatis/builder/UserMapper.xml"/>
</mappers>


<! Implement the fully qualified class name of the class using the mapper interface
<mappers>
  <mapper class="org.mybatis.builder.AuthorMapper"/>
</mappers>


<! Register all mapper interface implementations as mapper
<mappers>
  <package name="org.mybatis.builder"/>
</mappers>
Copy the code