Mybatis重點解析

Mybatis重點解析

定義 resultMap

<!-- 建立 User 實體和數據庫表的對應關係
type 屬性:指定實體類的全限定類名
id 屬性:給定一個唯一標識,是給查詢 select 標籤引用用的。
-->
<resultMap type="com.itheima.domain.User" id="userMap">
<id column="id" property="userId"/>
<result column="username" property="userName"/>
<result column="sex" property="userSex"/>
<result column="address" property="userAddress"/>
<result column="birthday" property="userBirthday"/>
</resultMap>

id 標籤:用於指定主鍵字段
result 標籤:用於指定非主鍵字段
column 屬性:用於指定數據庫列名
property 屬性:用於指定實體類屬性名稱

映射配置

<!-- 配置查詢所有操作 -->
<select id="findAll" resultMap="userMap">
select * from user
</select>

別名typeAliases

<typeAliases>
<!-- 單個別名定義 -->
<typeAlias alias="user" type="com.itheima.domain.User"/>
<!-- 批量別名定義,掃描整個包下的類,別名爲類名(首字母大寫或小寫都可以) -->
<package name="com.itheima.domain"/>
<package name=" 其它包 "/>
</typeAliases>

mappers (映射器)

<mapper resource=" " />
使用相對於類路徑的資源
如:<mapper resource="com/itheima/dao/IUserDao.xml" />

<mapper class=" " />
使用 mapper 接口類路徑
如:<mapper class="com.itheima.dao.UserDao"/>
注意:此種方法要求 r mapper 接口名稱和 r mapper 映射文件名稱相同,且放在同一個目錄中。

<package name=""/>
註冊指定包下的所有 mapper 接口
如:<package name="cn.itcast.mybatis.mapper"/>
注意:此種方法要求 mar pper 接口名稱和 r mapper 映射文件名稱相同,且放在同一個目錄中

動態 SQL 之 < if > 標籤

<select id="findByUser" resultType="user" parameterType="user">
select * from user where 1=1
<if test="username!=null and username != '' ">
and username like #{username}
</if>
<if test="address != null">
and address like #{address}
</if>
</select>
注意:<if>標籤的 test 屬性中寫的是對象的屬性名,如果是包裝類的對象要使用 OGNL 表達式的寫法。
另外要注意 where 1=1 的作用~

動態 SQL 之< where > 標籤

<!-- 根據用戶信息查詢 -->
<select id="findByUser" resultType="user" parameterType="user">
<include refid="defaultSql"></include>
<where>
<if test="username!=null and username != '' ">
and username like #{username}
</if>
<if test="address != null">
and address like #{address}
</if>
</where>
</select>

動態標籤之< foreach >標籤

<!-- 查詢所有用戶在 id 的集合之中 -->
<select id="findInIds" resultType="user" parameterType="queryvo">
<!-- select * from user where id in (1,2,3,4,5); -->
<include refid="defaultSql"></include>
<where>
<if test="ids != null and ids.size() > 0">
<foreach collection="ids" open="id in ( " close=")" item="uid"
separator=",">
#{uid}
</foreach>
</if>
</where>
</select>
SQL 語句:
select 字段 from user where id in (?)
<foreach>標籤用於遍歷集合,它的屬性:
collection:代表要遍歷的集合元素,注意編寫時不要寫#{}
open:代表語句的開始部分
close:代表結束部分
item:代表遍歷集合的每個元素,生成的變量名
sperator:代表分隔符

Mybatis 中簡化編寫的 SQL 片段

Sql 中可將重複的 sql 提取出來,使用時用 include 引用即可,最終達到 sql 重用的目的

<!-- 抽取重複的語句代碼片段 -->
<sql id="defaultSql">
select * from user
</sql>


<!-- 配置查詢所有操作 -->
<select id="findAll" resultType="user">
<include refid="defaultSql"></include>
</select>
<!-- 根據 id 查詢 -->
<select id="findById" resultType="UsEr" parameterType="int">
<include refid="defaultSql"></include>
where id = #{uid}
</select>

一對多查詢

<?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.itheima.dao.IUserDao">
<resultMap type="user" id="userMap">
<id column="id" property="id"></id>
<result column="username" property="username"/>
<result column="address" property="address"/>
<result column="sex" property="sex"/>
<result column="birthday" property="birthday"/>
<!-- collection 是用於建立一對多中集合屬性的對應關係
ofType 用於指定集合元素的數據類型
-->
<collection property="accounts" ofType="account">
<id column="aid" property="id"/>
<result column="uid" property="uid"/>
<result column="money" property="money"/>
</collection>
</resultMap>
<!-- 配置查詢所有操作 -->
<select id="findAll" resultMap="userMap">
select u.*,a.id as aid ,a.uid,a.money from user u left outer join account
a on u.id =a.uid
</select>
</mapper>
collection
部分定義了用戶關聯的賬戶信息。表示關聯查詢結果集
property="accList" :
關聯查詢的結果集存儲在 User 對象的上哪個屬性。
ofType="account" :
指定關聯查詢的結果集中的對象類型即List中的對象類型。此處可以使用別名,也可以使用全限定名。

多對多查詢

<?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.itheima.dao.IRoleDao">
<!--定義 role 表的 ResultMap-->
<resultMap id="roleMap" type="role">
<id property="roleId" column="rid"></id>
<result property="roleName" column="role_name"></result>
<result property="roleDesc" column="role_desc"></result>
<collection property="users" ofType="user">
<id column="id" property="id"></id>
<result column="username" property="username"></result>
<result column="address" property="address"></result>
<result column="sex" property="sex"></result>
<result column="birthday" property="birthday"></result>
</collection>
</resultMap>
<!--查詢所有-->
<select id="findAll" resultMap="roleMap">
select u.*,r.id as rid,r.role_name,r.role_desc from role r
left outer join user_role ur on r.id = ur.rid
left outer join user u on u.id = ur.uid
</select>
</mapper>

Mybatis 延遲加載策略

延遲加載:
就是在需要用到數據時才進行加載,不需要用到數據時就不加載數據。延遲加載也稱懶加載.
好處:先從單表查詢,需要時再從關聯表去關聯查詢,大大提高數據庫性能,因爲查詢單表要比關聯查詢多張錶速度要快。

壞處 :
因爲只有當需要用到數據時,纔會進行數據庫查詢,這樣在大批量數據查詢時,因爲查詢工作也要消耗
時間,所以可能造成用戶等待時間變長,造成用戶體驗下降。

我們需要在 Mybatis 的配置文件 SqlMapConfig.xml 文件中添加延遲加載的配置。

<!-- 開啓延遲加載的支持 -->
<settings>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
</settings>

使用 Collection 實現延遲加載

<resultMap type="user" id="userMap">
<id column="id" property="id"></id>
<result column="username" property="username"/>
<result column="address" property="address"/>
<result column="sex" property="sex"/>
<result column="birthday" property="birthday"/>
<!-- collection 是用於建立一對多中集合屬性的對應關係
ofType 用於指定集合元素的數據類型
select 是用於指定查詢賬戶的唯一標識(賬戶的 dao 全限定類名加上方法名稱)
column 是用於指定使用哪個字段的值作爲條件查詢
-->
<collection property="accounts" ofType="account"
select="com.itheima.dao.IAccountDao.findByUid"
column="id">
</collection>
</resultMap>
<!-- 配置查詢所有操作 -->
<select id="findAll" resultMap="userMap">
select * from user
</select>
<collection> 標籤 :
主要用於加載關聯的集合對象
select 屬性 :
用於指定查詢 account 列表的 sql 語句,所以填寫的是該 sql 映射的 id
column 屬性 :
用於指定 select 屬性的 sql 語句的參數來源,上面的參數來自於 user 的 id 列,所以就寫成 id 這一
個字段名了

編寫賬戶持久層映射配置

<!-- 根據用戶 id 查詢賬戶信息 -->
<select id="findByUid" resultType="account" parameterType="int">
select * from account where uid = #{uid}
</select>

二級緩存的開啓與關閉

第一步:在 SqlMapConfig.xml 文件開啓二級緩存

<settings>
<!-- 開啓二級緩存的支持 -->
<setting name="cacheEnabled" value="true"/>
</settings>
因爲 cacheEnabled 的取值默認就爲 true,所以這一步可以省略不配置。爲 true 代表開啓二級緩存;爲
false 代表不開啓二級緩存。

第二步:配置相關的 Mapper 映射文件 件

<cache>標籤表示當前這個 mapper 映射將使用二級緩存,區分的標準就看 mapper 的 namespace 值。
<?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.itheima.dao.IUserDao">
<!-- 開啓二級緩存的支持 -->
<cache></cache>
</mapper>

第三步:置 配置 statement 上面的 useCache 屬性

<!-- 根據 id 查詢 -->
<select id="findById" resultType="user" parameterType="int" useCache="true">
select * from user where id = #{uid}
</select>
將 UserDao.xml 映射文件中的<select>標籤中設置 useCache=true”代表當前這個 statement 要使用
二級緩存,如果不使用二級緩存可以設置爲 false。
注意:針對每次查詢都需要最新的數據 sql,要設置成 useCache=false,禁用二級緩存。

mybatis 的常用註解說明

@Insert: 實現新增
@Update: 實現更新
@Delete: 實現刪除
@Select: 實現查詢
@Result: 實現結果集封裝
@Results: 可以與@Result 一起使用,封裝多個結果集
@ResultMap: 實現引用@Results 定義的封裝
@One: 實現一對一結果集封裝
@Many: 實現一對多結果集封裝
@SelectProvider: 實現動態 SQL 映射
@CacheNamespace: 實現註解二級緩存的使用

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章