MyBatis核心配置文件深入分析
MyBatis 的核心配置文件是 mybatis-config.xml。注意配置文件中節(jié)點(diǎn)的順序有要求,必須按照下面的順序填寫節(jié)點(diǎn)信息:
(properties,settings,typeAliases,typeHandlers,objectFactory,objectWrapperFactory,reflectorFactory,plugins,environments,databaseIdProvider,mappers)
<configuration> <properties></properties> <settings></settings> <typeAliases></typeAliases> <typeHandlers></typeHandlers> <objectFactory></objectFactory> <objectWrapperFactory></objectWrapperFactory> <reflectorFactory></reflectorFactory> <plugins></plugins> <environments></environments> <databaseIdProvider></databaseIdProvider> <mappers></mappers> </configuration>
環(huán)境配置與 mappers 映射器
基本的 MyBatis 配置:
<?xml version="1.0" encoding="UTF8" ?> <!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="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=UTF-8"/> <property name="username" value="root"/> <property name="password" value=""/> </dataSource> </environment> </environments> <!--每個(gè)mapper.xml都需要在mybatis配置文件中進(jìn)行配置--> <mappers> <mapper resource="mapper/UserMapper.xml"/> </mappers> </configuration>
在上面這段配置中配置了運(yùn)行環(huán)境以及 mappers 映射器。
配置 environment 時(shí),通過 transactionManager 指定事務(wù)管理器,通過 dataSource 指定數(shù)據(jù)庫的引擎類型、連接方式、用戶名、密碼。
mappers 映射器用來和代碼中寫的 mapper 一一對應(yīng),在代碼中寫一個(gè) mapper 接口和 mapper.xml 文件,就需要在 mappers 映射器中增加一個(gè) mapper 節(jié)點(diǎn)。
屬性(properties)
在寫 mybatis-config.xml 環(huán)境配置的時(shí)候,將數(shù)據(jù)庫的連接信息直接寫在了 mybatis-config.xml 配置文件中,不方便后續(xù)的更改,因此可以使用屬性(properties)的能力。
在 resources 目錄下新建一個(gè) db.properties:
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=UTF-8
username=root
password=123
接著在 mybatis-config.xml 的 configuration 中添加 properties 節(jié)點(diǎn),注意這個(gè)節(jié)點(diǎn)的位置必須放在首位。
<properties resource="db.properties"> </properties>
接著就可以用 properties 中的屬性去代替 xml 中的屬性
<property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/>
設(shè)置(settings)
配置文件中的 settings 是 MyBatis 中極為重要的調(diào)整設(shè)置,它們會改變 MyBatis 的運(yùn)行時(shí)行為。
常用的設(shè)置有以下幾種:
設(shè)置的配置方式如下:
<settings> <setting name="cacheEnabled" value="true"/> <setting name="lazyLoadingEnabled" value="true"/> <setting name="mapUnderscoreToCamelCase" value="false"/> <setting name="logImpl" value="NO_LOGGING"/> </settings>
類型別名(typeAliases)
類型別名可為 Java 類型設(shè)置一個(gè)縮寫名字。 它僅用于 XML 配置,意在降低冗余的全限定類名書寫。
類型別名有兩種方式,第一種是對類取別名,第二種是指定一個(gè)包名
<mapper namespace="com.cn.mapper.UserMapper"> <select id="getUserList" resultType="com.cn.pojo.User"> select * from user; </select> </mapper>
第一種方式是對類取別名,這里的 resultType 寫的是類的全限定名,我們可以在 mybatis-config.xml 中使用類型別名來簡化。typeAliases 在配置文件中的位置需要放在 setting 后面
<typeAliases> <typeAlias type="com.cn.pojo.User" alias="user"/> </typeAliases>
這樣設(shè)置之后就可以在 resultType 中直接使用 user
<mapper namespace="com.cn.mapper.UserMapper"> <select id="getUserList" resultType="user"> select * from user; </select> </mapper>
第二種方式是指定一個(gè)包名,MyBatis 會在指定的包名路徑下搜索需要的 JavaBean。修改配置文件,使用包名來指定
<typeAliases> <package name="com.cn.pojo"/> </typeAliases>
每一個(gè)在包 com.lanqiaoyun.pojo 中的 Java Bean,在沒有注解的情況下,會使用 Bean 的首字母小寫的非限定類名來作為它的別名。 比如 com.lanqiaoyun.pojo.User 的別名為 user;若有注解,則別名為其注解值
@Alias("user") public class User { ... }
上方所示完整配置文件如下:
<?xml version="1.0" encoding="UTF8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <properties resource="db.properties"> </properties> <settings> <setting name="cacheEnabled" value="true"/> <setting name="lazyLoadingEnabled" value="true"/> <setting name="mapUnderscoreToCamelCase" value="false"/> <setting name="logImpl" value="NO_LOGGING"/> </settings> <typeAliases> <package name="com.cn.pojo"/> </typeAliases> <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> <!--每個(gè)mapper.xml都需要在mybatis配置文件中進(jìn)行配置--> <mappers> <mapper resource="mapper/UserMapper.xml"/> </mappers> </configuration>
到此這篇關(guān)于MyBatis核心配置文件深入分析的文章就介紹到這了,更多相關(guān)MyBatis配置文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot靜態(tài)資源CSS等修改后再運(yùn)行無效的解決
這篇文章主要介紹了SpringBoot靜態(tài)資源CSS等修改后再運(yùn)行無效的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12詳解IDEA社區(qū)版(Community)和付費(fèi)版(UItimate)的區(qū)別
這篇文章主要介紹了詳解IDEA社區(qū)版(Community)和付費(fèi)版(UItimate)的區(qū)別,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11Gradle構(gòu)建基本的Web項(xiàng)目結(jié)構(gòu)
這篇文章主要為大家介紹了Gradle創(chuàng)建Web項(xiàng)目基本的框架結(jié)構(gòu)搭建,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-03-03springboot+mybatis如何屏蔽掉mybatis日志
這篇文章主要介紹了springboot+mybatis如何屏蔽掉mybatis日志問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05idea將maven項(xiàng)目改成Spring boot項(xiàng)目的方法步驟
這篇文章主要介紹了idea將maven項(xiàng)目改成Spring boot項(xiàng)目的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09詳解Java中格式化日期的DateFormat與SimpleDateFormat類
DateFormat其本身是一個(gè)抽象類,SimpleDateFormat 類是DateFormat類的子類,一般情況下來講DateFormat類很少會直接使用,而都使用SimpleDateFormat類完成,下面我們具體來看一下兩個(gè)類的用法:2016-05-05SpringBoot+Mybatis實(shí)現(xiàn)Mapper接口與Sql綁定幾種姿勢
通常我們在使用Mybatis進(jìn)行開發(fā)時(shí),會選擇xml文件來寫對應(yīng)的sql,然后將Mapper接口與sql的xml文件建立綁定關(guān)系,然后在項(xiàng)目中調(diào)用mapper接口就可以執(zhí)行對應(yīng)的sql,感興趣的可以學(xué)習(xí)一下2021-09-09