mybatis 项目配置文件实例详解
程序员文章站
2022-03-07 12:24:06
mybatis项目配置
首先这事一个简单的mybatis项目配置文件:
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支持多个环境,可以任意配置
比如:
<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>
就会有两个环境,environments中的defalut标签选择哪一个,默认就是哪一个
transactionmanager
mybatis支持两种类型的事务管理器,jdbc和managed(托管)
jdbc:应用程序管理数据库连接的生命周期
managed:由应用服务器负责管理数据库连接的生命周期(一般商业服务器才有此功能,如jboss weblogic)
datasource
type:用来配置数据源,类型有:unpooled、pooled、jndi
unpooled:没有连接池,每次数据库操作,mybatis都会创建一个新的连接,用完后,关闭:适合小并发项目
pooled:有连接池
jndi:使用应用服务器配置jndi数据源获取数据库连接
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>
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
下一篇: Python实战练习之终于对肯德基下手