mybatis 動态sql
标簽
if --> 使用OGNL表達式, 能直接調用方法(包括靜态方法),能使用 三元運算符, 能做邏輯判斷
choose(when,otherwise)
foreach
trim(where,set)
if 标簽
exmaple
<!--
test ----- OGNL 表達式,特殊字元需要使用轉義字元
含義: 滿足 條件才會拼接 if 标簽中的sql
注意:OGNL 字元串和數字做判斷時 會自動把字元串轉為 數字,example "1" == 1
-->
<select id="select" paramType="User" resultType="user">
select * from account where 1=1
<if test="id != null and id !=' ' " >
and id = #{id}
</if>
<if test="name != null and name.trim() !=' ' " >
and name = #{name}
</if>
</select>
where 标簽
<!--
where ----- 用來取代 where 關鍵字
作用: 使用where 标簽後,在拼接SQL時 會自動 去掉 第一個 and 或 or 關鍵字
-->
<select id="select" paramType="User" resultType="user">
select * from account
<where>
<if test="id != null and id !=' ' " >
and id = #{id}
</if>
<if test="name != null and name.trim() !=' ' " >
and name = #{name}
</if>
</where>
</select>
set 标簽
<!--
set----- 用來取代 set關鍵字
作用: 使用set标簽後,在拼接SQL時 會自動 去掉 最後一個 逗号 ,
-->
<update id="update" paramType="User">
update account
<set>
<if test="id != null and id !=' ' " >
id = #{id},
</if>
<if test="name != null and name.trim() !=' ' " >
name = #{name},
</if>
</set>
</update>
Trim 标簽
<!--
trim ----- 自定義字元串截取規則
作用: 給字元串增加或删除字首 或者增加删除字尾
prefix: trim标簽中字元串拼接完成後, 給拼接後的字元串增加字首
prefixOverrides: 字首覆寫,去掉拼接後的字元串的指定字首字元
suffix: 字尾,給拼接後的字元串增加字尾
suffixOverrides: 字尾覆寫,去掉拼接後的字元串的指定字尾字元
-->
<select id="select" paramType="User" resultType="user">
select * from account
<trim prefix="where" suffixOverrides="and">
<if test="id != null and id !=' ' " >
id = #{id} and
</if>
<if test="name != null and name.trim() !=' ' " >
name = #{name} and
</if>
</trim>
</select>
choose 分支判斷
<!--
choose ----- 分支 類似if else
when : 類似 if else标簽 , 隻要有 一個 when 标簽滿足條件,其他的when 标簽就不會執行
othserwise:表示不滿足條件時的預設選項
-->
<select id="select" paramType="User" resultType="user">
select * from account
<choose>
<when test="id != null and id !=' ' " >
id = #{id} and
</when >
<otherwise test="name != null and name.trim() !=' ' " >
name = #{name} and
</otherwise>
</choose>
</select>
foreach 标簽
<!--
foreach ----- 循環周遊 類似 for
collection: 要周遊的集合
list 類型的參數會特殊處理封裝在map 中, map 的key 就叫list
item:目前周遊出的元素
separator: 每個元素之間的分隔符
open: 所有周遊結果拼接後的字元串 加一個字首
cloase:所有周遊結果拼接後的字元串 加一個字尾
index:索引, 周遊list 時 index 是索引,item 就是目前索引對應的元素
周遊map 是 index 是key item 是value
-->
<select id="select" resultType="user">
select * from account
<foreach collection="list" open= " where id in ( " close=")" separator="," item="temp">
#(temp)
</foreach >
</select>
批量插入 oracle得用其他方式
<insert id="insert" paramType="list">
insert into account values
<foreach collection="list" separator="," item="temp">
(#{temp.name}, #{temp.password}, #{temp.money})
</foreach>
</insert>
bind 标簽
模糊查詢: 可以使用 concat(’%’, #{value}, ‘%’)
<!--
bind:綁定
作用:可以将OGNL 表達式的值綁定到一個變量中,友善後來引用整個變量
-->
<select id="select" resultType="user">
<bind name = "name" value=" '%' + lastname + '%' ">
select * from account where name like #{name}
</select>
sql 和 include标簽
<!--
sql:抽取通用的 sql 片段
include:引用外部的 sql 片段
include: 還可以自定義一些property ,SQL 标簽内部就能使用自定義屬性 include-property, 取值 ${prop},不能使用#{prop}
-->
<sql id="selectAll">
select * from ${tableName}
</sql>
<select id = "select" resultType = "Account">
<include refid="selectAll" ></include> where id = #{id}
</select>
mybatis 兩個預設參數
-
_parameter :代表整個參數
單個參數: _parameter 就是整個參數
多個參數: 參數會被封裝到map中, _parameter 就是代表整個map
- _databaseId: 如果配置了dataBaseIdProvider 标簽 ,_databaseId 就是目前資料庫的别名
OGNL 表達式
