@[toc]
九昂羡、動(dòng)態(tài)SQL
9.1 if
總結(jié)
:根據(jù)標(biāo)簽中test屬性所對(duì)應(yīng)的表達(dá)式?jīng)Q定標(biāo)簽中的內(nèi)容是否需要拼接到SQL中待笑。
User getUserByParamsWithIf(User user);
<select id="getUserByParamsWithIf" resultType="User">
select * from litemall_user where 1=1
<if test="username != '' and username != null">
and username = #{username}
</if>
<if test="id != 0 and id != null">
and id = #{id}
</if>
</select>
9.2 where
總結(jié)
:
- 當(dāng)where標(biāo)簽中有內(nèi)容時(shí)鸣皂,會(huì)自動(dòng)生成where關(guān)鍵字,并且將內(nèi)容前多余的and或or去掉暮蹂。
- 當(dāng)where標(biāo)簽中沒有內(nèi)容時(shí)寞缝,此時(shí)where標(biāo)簽沒有任何效果。
- 注意:where標(biāo)簽不能將其中內(nèi)容后面多余的and或or去掉仰泻。
User getUserByParamsWithWhere(User user);
<select id="getUserByParamsWithWhere" resultType="User">
select * from litemall_user
<where>
<if test="username != '' and username != null">
and username = #{username}
</if>
<if test="id != 0 and id != null">
and id = #{id}
</if>
</where>
</select>
9.3 trim
總結(jié)
:trim用于去掉或添加標(biāo)簽中的內(nèi)容
常用屬性
:
prefix:在trim標(biāo)簽中的內(nèi)容的前面添加某些內(nèi)容
prefixOverrides:在trim標(biāo)簽中的內(nèi)容的前面去掉某些內(nèi)容
suffix:在trim標(biāo)簽中的內(nèi)容的后面添加某些內(nèi)容
suffixOverrides:在trim標(biāo)簽中的內(nèi)容的后面去掉某些內(nèi)容
User getUserByParamsWithTrim(User user);
<select id="getUserByParamsWithTrim" resultType="User">
select * from litemall_user
<trim prefix="where" suffixOverrides="and">
<if test="username != '' and username != null">
username = #{username} and
</if>
<if test="gender != 0 and gender != null">
gender = #{gender} and
</if>
<if test="id != 0 and id != null">
id = #{id} and
</if>
</trim>
</select>
9.4 choose第租、when、otherwise
總結(jié)
:相當(dāng)于if..else if ..else 我纪,其中when至少要有一個(gè),otherwise最多只能有一個(gè)。
List<User> getUserByParamsWithChoose(User user);
<select id="getUserByParamsWithChoose" resultType="User">
select * from litemall_user
<where>
<choose>
<when test="username != '' and username != null">
username = #{username}
</when>
<when test="gender != 0 and gender != null">
gender = #{gender}
</when>
<when test="id != 0 and id != null">
id = #{id}
</when>
</choose>
</where>
</select>
9.5 foreach
總結(jié):屬性:
collection:設(shè)置要循環(huán)的數(shù)組或集合
item:表示集合或數(shù)組中的每一個(gè)數(shù)據(jù)
separator:設(shè)置循環(huán)體之間的分隔符
open:設(shè)置foreach標(biāo)簽中的內(nèi)容的開始符
close:設(shè)置foreach標(biāo)簽中的內(nèi)容的結(jié)束符
9.6 SQL片段
總結(jié)
:sql片段浅悉,可以記錄一段公共sql片段趟据,在使用的地方通過include標(biāo)簽進(jìn)行引入。
User getUserByParamsWithSQLFragment();
<sql id="userColumns">
id, username, password, mobile, gender, last_login_ip
</sql>
<select id="getUserByParamsWithSQLFragment" resultType="User">
select <include refid="userColumns"></include> from litemall_user where id = 4
</select>
本人其他相關(guān)文章鏈接
1.一术健、MyBatis簡介:MyBatis歷史汹碱、MyBatis特性、和其它持久化層技術(shù)對(duì)比荞估、Mybatis下載依賴包流程
2.二咳促、搭建MyBatis采用xml方式,驗(yàn)證CRUD(增刪改查操作)
3.三勘伺、MyBatis核心配置文件詳解
4.四跪腹、MyBatis獲取參數(shù)值的兩種方式(重點(diǎn))
5.五、MyBatis的增刪改查模板(參數(shù)形式包括:String飞醉、對(duì)象冲茸、集合、數(shù)組缅帘、Map)
6.六轴术、MyBatis特殊的SQL:模糊查詢、動(dòng)態(tài)設(shè)置表名钦无、校驗(yàn)名稱唯一性
7.七逗栽、MyBatis自定義映射resultMap
8.八、(了解即可)MyBatis懶加載(或者叫延遲加載)
9.九失暂、MyBatis動(dòng)態(tài)SQL
10.十彼宠、MyBatis的緩存
11.十一、MyBatis的逆向工程
12.十二趣席、MyBatis分頁插件