詳解Mybatis核心配置文件
Mybatis核心配置文件
記錄在mybatis核心配置文件中,常用的配置選項(xiàng):
下邊是之前的配置選項(xiàng):
<?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"> <!--開(kāi)發(fā)環(huán)境 - 數(shù)據(jù)庫(kù)配置信息--> <environment id="development"><!--配置的唯一--> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://127.0.0.1:3306/java_pro?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=false "/> <property name="username" value="root"/> <property name="password" value="lvxingchen@123"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/lxc/dao/UserMapper.xml"></mapper> </mappers> </configuration>
一、屬性(properties)
在mybatis配置文件中,它提供了一個(gè)屬性標(biāo)簽 <properties> , 這個(gè)屬性標(biāo)簽跟 xxx.properties文件一樣,配置一些基礎(chǔ)信息,只不過(guò)現(xiàn)在又提供了一個(gè)標(biāo)簽形式。
我們把配置文件中基礎(chǔ)信息單獨(dú)寫(xiě)在db.properties文件中
<?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> <!--添加properties標(biāo)簽--> <properties resource="db.properties"></properties> <environments default="development"> <!--開(kāi)發(fā)環(huán)境 - 數(shù)據(jù)庫(kù)配置信息--> <environment id="development"><!--配置的唯一--> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="#{driver}"/><!--用#{}占位,里邊是配置文件的key --> <property name="url" value="#{url}"/> <property name="username" value="#{username}"/> <property name="password" value="#{password}"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/lxc/dao/UserMapper.xml"></mapper> </mappers> </configuration>
還可以在屬性標(biāo)簽里添加配置標(biāo)簽,
注意:如果同時(shí)配置 配置文件中 和 <property>,那么配置文件優(yōu)先級(jí)高。
<configuration> <properties resource="db.properties"> <property name="username" value="root" /> <property name="password" value="lvxingchen" /> </properties> <!-- ··· ··· --> </configuration>
注意一個(gè)問(wèn)題:
在配置文件中,url 路徑中有 & 符號(hào),會(huì)使用 & 來(lái)代替,當(dāng)我們把url抽離出來(lái),放到xxx.properties 文件中時(shí),需要把 &; 改成 & 。
二、設(shè)置(settings)
cacheEnabled
lazyLoadingEnabled
logImpl
三、類名別名(typeAliases)
下邊記錄類名別名的三種方式:
方式一:
類名別名,僅用于xml配置,意在降低冗余的全限定類名的書(shū)寫(xiě),例如:
在mybatis核心配置文件中編寫(xiě)類名別名:
<?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> <!--注意書(shū)寫(xiě)順序:properties -》 typeAliases-》 environments --> <properties resource="db.properties"></properties> <!--別名配置: com.lxc.domain.User的別名為User--> <typeAliases> <typeAlias alias="User" type="com.lxc.domain.User" /> </typeAliases> <environments default="development"> <!-- ··· --> </environments> <mappers></mappers> </configuration>
在sql映射文件中使用別名:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.lxc.dao.UserMapper"> <!--現(xiàn)在的配置--> <select id="getUserList" resultType="User"> select * from mybatis </select> <!--原來(lái)的配置 <select id="getUserList" resultType="com.lxc.domain.User"> select * from mybatis </select>--> </mapper>
方式二:
也可以使用一個(gè)包名
<?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> <properties resource="db.properties"></properties> <!--包名的別名配置--> <typeAliases> <package name="com.lxc.domain" /> </typeAliases> <environments default="development"> <!-- ··· --> </environments> <mappers></mappers> </configuration>
在使用的時(shí)候注意:
每一個(gè)在包 com.lxc.domain
中的 Java Bean,在沒(méi)有注解的情況下,會(huì)使用 Bean 的首字母小寫(xiě)的非限定類名來(lái)作為它的別名。 比如 com.lxc.domain下邊的User類,
的別名為 user;
Mybatis 會(huì)在包名下邊搜索需要的Java Bean(這個(gè)Java Bean 就是 下圖domain里邊的類文件)。
com.lxc.domain.User 的別名為 user
方式三:
注解方式,這種方式其實(shí)是第二種方式的延伸,Mybatis 會(huì)在包名下邊搜索需要的Java Bean, 若有注解,則別名為注解的值,看下邊代碼:
使用這種方式前提必須在mybatis核心配置文件中配置:
<typeAliases>
<package name="com.lxc.domain" />
</typeAliases>
@Alias("InstanceUsers") public class User { }
此時(shí) com.lxc.domain.User 的別名為 InstanceUsers
四、映射器(mappers)
最常用的配置
<!--每一個(gè)Mapper.xml(sql映射文件)都需要在Mybatis核心文件中注冊(cè)!--> <mappers> <mapper resource="com/lxc/dao/UserMapper.xml"></mapper> </mappers>
到此這篇關(guān)于詳解Mybatis核心配置文件的文章就介紹到這了,更多相關(guān)Mybatis核心配置文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于bootstrap.yml和bootstrap.properties的優(yōu)先級(jí)問(wèn)題
這篇文章主要介紹了關(guān)于bootstrap.yml和bootstrap.properties的優(yōu)先級(jí)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03java selenium教程環(huán)境搭建基于Maven
本文主要介紹Java selenium 環(huán)境的安裝,這里介紹了基于Maven的環(huán)境搭建,有需要的小伙伴可以參考下2016-08-08基于jenkins實(shí)現(xiàn)發(fā)布node.js項(xiàng)目
這篇文章主要介紹了基于jenkins實(shí)現(xiàn)發(fā)布node.js項(xiàng)目,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07對(duì)handlerexecutionchain類的深入理解
下面小編就為大家?guī)?lái)一篇對(duì)handlerexecutionchain類的深入理解。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07SpringBoot使用FreeMarker模板發(fā)送郵件
這篇文章主要為大家詳細(xì)介紹了SpringBoot使用FreeMarker模板發(fā)送郵件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04SpringBoot中多環(huán)境配置和@Profile注解示例詳解
這篇文章主要介紹了SpringBoot中多環(huán)境配置和@Profile注解,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-01-01spring?kafka?@KafkaListener詳解與使用過(guò)程
這篇文章主要介紹了spring-kafka?@KafkaListener詳解與使用,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-02-02