詳解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
三、類(lèi)名別名(typeAliases)
下邊記錄類(lèi)名別名的三種方式:
方式一:
類(lèi)名別名,僅用于xml配置,意在降低冗余的全限定類(lèi)名的書(shū)寫(xiě),例如:
在mybatis核心配置文件中編寫(xiě)類(lèi)名別名:
<?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)名來(lái)作為它的別名。 比如 com.lxc.domain下邊的User類(lèi),的別名為 user;
Mybatis 會(huì)在包名下邊搜索需要的Java Bean(這個(gè)Java Bean 就是 下圖domain里邊的類(lèi)文件)。
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)文章
Java編譯錯(cuò)誤問(wèn)題:需要class,interface或enum
這篇文章主要介紹了Java編譯錯(cuò)誤問(wèn)題:需要class,interface或enum,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02
java設(shè)計(jì)模式之策略模式在促銷(xiāo)活動(dòng)場(chǎng)景中的使用案例
這篇文章主要為大家介紹了java設(shè)計(jì)模式之策略模式在促銷(xiāo)活動(dòng)場(chǎng)景中案例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
Springboot集成mybatis實(shí)現(xiàn)多數(shù)據(jù)源配置詳解流程
在日常開(kāi)發(fā)中,若遇到多個(gè)數(shù)據(jù)源的需求,怎么辦呢?通過(guò)springboot集成mybatis實(shí)現(xiàn)多數(shù)據(jù)源配置,簡(jiǎn)單嘗試一下,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
jvm細(xì)節(jié)探索之synchronized及實(shí)現(xiàn)問(wèn)題分析
這篇文章主要介紹了jvm細(xì)節(jié)探索之synchronized及實(shí)現(xiàn)問(wèn)題分析,涉及synchronized的字節(jié)碼表示,JVM中鎖的優(yōu)化,對(duì)象頭的介紹等相關(guān)內(nèi)容,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-11-11
Java中線程狀態(tài)+線程安全問(wèn)題+synchronized的用法詳解
這篇文章主要介紹了Java中線程狀態(tài)+線程安全問(wèn)題+synchronized的用法詳解,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04

