2.1.3Mybatis核心檔案分析
①配置檔案層級:
Mybatis核心配置檔案層級關系
·configurtion配置
·properties屬性
·settings設定
·typeAliases類型别名
·typeHandlers類型處理器
·objectFactory對象工廠
·plugins 插件
·environments環境
·environment環境變量
·transactionManager事務管理器
·DataSource資料源
·databaseIdProvider資料庫廠商辨別
·mappers映射器
②Mybatis常用配置解析
1)environments标簽
<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:///test"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
① transactionManager事務管理器有兩種:
JDBC:這個配置是直接使用JDBC的送出和復原設定,它依賴于從資料源得到的連接配接來管理事務作用域;
MANAGED:幾乎不做處理,它從來都不送出或復原一個連接配接,而是讓容器來管理事務的整個生命周期(比如JEE應用伺服器的上下文),預設情況它會關閉連接配接,然⽽⼀些容器并不希望這樣,是以需要将 closeConnection 屬性設定為 false 來阻⽌它預設的關閉⾏為。
②:資料源(dataSource)有三個類型
•UNPOOLED:這個資料源的實作隻是每次被請求時打開和關閉連接配接。
•POOLED:這種資料源的實作利⽤“池”的概念将 JDBC 連接配接對象組織起來。
•JNDI:這個資料源的實作是為了能在如 EJB 或應⽤伺服器這類容器中使⽤,容器可以集中或在外部配置資料源,然後放置⼀個 JNDI 上下⽂的引⽤。
2)mepper标簽
該标簽的作用是做加載映射,加載方式有:
•使⽤相對于類路徑的資源引⽤,例如:
<mapper resource="org/mybatis/builder/AuthorMapper.xml"/>
•使⽤完全限定資源定位符(URL),例如:
<mapper url="file:///var/mappers/AuthorMapper.xml"/>
•使⽤映射器接⼝實作類的完全限定類名,例如:
<mapper class="org.mybatis.builder.AuthorMapper"/>
•将包内的映射器接⼝實作全部注冊為映射器,例如:
<package name="org.mybatis.builder"/>
3)Properties标簽
實際開發中習慣将資料源資訊單獨抽取成一個properies檔案,properties标簽可以加載額外的配置檔案
<!--加載外部的properties-->
<properties resource = "jdbc.properties"></properties>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
4)typeAliases标簽
類型别名,為java類型設定一個短的名字
<!--配置typeAliases,為com.lagou.domain.User定義别名為user-->
<typeAliases>
<typeAlias type="com.lagou.domain.User" alias = "user"></typeAlias>
</typeAliases>
<select id =""findAll resultType="user">
select * from User
</select>
③映射配置檔案mapper.xml
1)動态sql之where if标簽
<select id="findByCondition" parameterType="user" resultType="user">
select * from User
<where>
<if test="id!=0">
and id=#{id}
</if>
<if test="username!=null">
and username=#{username}
</if>
</where>
</select>
2)動态sql之 foreach 标簽
<select id="findByIds" parameterType="list" resultType="user">
select * from User
<where>
<foreach collection="list" open="id in(" close=")" item="id"separator=",">
#{id}
</foreach>
</where>
</select>
foreach(周遊)标簽屬性含義:
屬性 | 含義 |
---|---|
collection | 要周遊的集合元素,注意不需要寫#{} |
open | 代表語句的開始部分 |
close | 代表結束部分 |
item | 集合中單個元素生成的變量名 |
sperator | 分隔符 |
3)sql片段抽取 提取重複的sql,使用include引用
<!--抽取sql⽚段簡化編寫-->
<sql id="selectUser" select * from User</sql>
<select id="findById" parameterType="int" resultType="user">
<include refid="selectUser"></include> where id=#{id}
</select>
<select id="findByIds" parameterType="list" resultType="user">
<include refid="selectUser"></include>
<where>
<foreach collection="array" open="id in(" close=")" item="id"separator=",">
#{id}
</foreach>
</where>
</select>