Mybatis project configuration file instance details, mybatis configuration file

Source: Internet
Author: User

Mybatis project configuration file instance details, mybatis configuration file

Mybatis project configuration

First, this is a simple mybatis project configuration file:

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration> <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> <mappers>  <mapper resource="org/mybatis/example/BlogMapper.xml"/> </mappers></configuration>

Environment

Mybatis supports multiple environments and can be configured as needed

For 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>  <environment id="test">   <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>

There will be two environments. Which one is selected for the defalut tag in environments and which one is selected by default?

TransactionManager

Mybatis supports two types of transaction managers: jdbc and managed)

Jdbc: application management database connection Lifecycle

Managed: The application server is responsible for managing the lifecycle of database connections (this function is generally available on commercial servers, such as JBOSS WebLogic)

DataSource

Type: used to configure the data source. The data types include UNPOOLED, POOLED, and JNDI.

UNPOOLED: No connection pool. Every database operation, mybatis creates a new connection. After the connection is used up, it is closed: suitable for small concurrent projects.

POOLED: With connection pool

JNDI: Use the application server to configure the JNDI data source to obtain the database connection

Properties

Configure attributes

For example:

<properties resource="org/mybatis/example/config.properties"> <property name="username" value="dev_user"/> <property name="password" value="F2Fa3!33TYyg"/></properties><dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/></dataSource>

TypeAliases

Alias for the complete qualified name of the class for ease of use

For example:

<TypeAliases> <typeAlias alias = "Author" type = "domain. blog. author "/> <typeAlias alias =" Blog "type =" domain. blog. blog "/> <typeAlias alias =" Comment "type =" domain. blog. comment "/> <typeAlias alias =" Post "type =" domain. blog. post "/> <typeAlias alias =" Section "type =" domain. blog. section "/> <typeAlias alias =" Tag "type =" domain. blog. tag "/> </typeAliases> <! -- Most commonly used --> <typeAliases> <package name = "domain. blog"/> </typeAliases>

Mappers

Introduce a ing File

<!-- Using classpath relative resources --><mappers> <mapper resource="org/mybatis/builder/AuthorMapper.xml"/> <mapper resource="org/mybatis/builder/BlogMapper.xml"/> <mapper resource="org/mybatis/builder/PostMapper.xml"/></mappers><!-- Using url fully qualified paths --><mappers> <mapper url="file:///var/mappers/AuthorMapper.xml"/> <mapper url="file:///var/mappers/BlogMapper.xml"/> <mapper url="file:///var/mappers/PostMapper.xml"/></mappers><!-- Using mapper interface classes --><mappers> <mapper class="org.mybatis.builder.AuthorMapper"/> <mapper class="org.mybatis.builder.BlogMapper"/> <mapper class="org.mybatis.builder.PostMapper"/></mappers><!-- Register all interfaces in a package as mappers --><mappers> <package name="org.mybatis.builder"/></mappers>

Settings

An example of the settings element fully configured is as follows:

<settings> <setting name="cacheEnabled" value="true"/> <setting name="lazyLoadingEnabled" value="true"/> <setting name="multipleResultSetsEnabled" value="true"/> <setting name="useColumnLabel" value="true"/> <setting name="useGeneratedKeys" value="false"/> <setting name="autoMappingBehavior" value="PARTIAL"/> <setting name="autoMappingUnknownColumnBehavior" value="WARNING"/> <setting name="defaultExecutorType" value="SIMPLE"/> <setting name="defaultStatementTimeout" value="25"/> <setting name="defaultFetchSize" value="100"/> <setting name="safeRowBoundsEnabled" value="false"/> <setting name="mapUnderscoreToCamelCase" value="false"/> <setting name="localCacheScope" value="SESSION"/> <setting name="jdbcTypeForNull" value="OTHER"/> <setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString"/></settings>

Plugins

Plug-ins such:

Paging plug-in

<plugins>  <plugin interceptor="com.github.pagehelper.PageHelper">    <property name="dialect" value="mysql"/>    <property name="offsetAsPageNum" value="true"/>    <property name="rowBoundsWithCount" value="true"/>    <property name="pageSizeZero" value="true"/>  </plugin></plugins>

Thank you for reading this article. I hope it will help you. Thank you for your support for this site!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.