mybatis項目配置
首先這事一個簡單的mybatis項目配置文件:
?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支持多個環(huán)境,可以任意配置
比如:
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>
就會有兩個環(huán)境,environments中的defalut標簽選擇哪一個,默認就是哪一個
transactionManager
mybatis支持兩種類型的事務(wù)管理器,jdbc和managed(托管)
jdbc:應(yīng)用程序管理數(shù)據(jù)庫連接的生命周期
managed:由應(yīng)用服務(wù)器負責(zé)管理數(shù)據(jù)庫連接的生命周期(一般商業(yè)服務(wù)器才有此功能,如JBOSS WebLogic)
dataSource
type:用來配置數(shù)據(jù)源,類型有:UNPOOLED、POOLED、JNDI
UNPOOLED:沒有連接池,每次數(shù)據(jù)庫操作,mybatis都會創(chuàng)建一個新的連接,用完后,關(guān)閉:適合小并發(fā)項目
POOLED:有連接池
JNDI:使用應(yīng)用服務(wù)器配置JNDI數(shù)據(jù)源獲取數(shù)據(jù)庫連接
properties
配置屬性
比如:
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
給類的完整限定名取別名,方便使用
比如:
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>
!-- 最常用-->
typeAliases>
package name="domain.blog"/>
/typeAliases>
mappers
引入映射文件
!-- 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
插件 比如:
分頁插件
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>
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
您可能感興趣的文章:- MyBatis框架中mybatis配置文件詳細介紹
- mybatis 加載配置文件的方法(兩種方式)
- Mybatis中的config.xml配置文件詳細解析
- spring與mybatis整合配置文件
- mybatis配置文件簡介_動力節(jié)點Java學(xué)院整理
- Spring+SpringMVC+MyBatis深入學(xué)習(xí)及搭建(三)之MyBatis全局配置文件解析
- 詳解MyBatis批量插入數(shù)據(jù)Mapper配置文件的寫法
- 優(yōu)化MyBatis配置文件中的配置詳解