動態sql的四個標籤
- if:判斷
- choose (when, otherwise):分支選擇;帶了break的swtich-case
如果帶了id就用id查,如果帶了lastName就用lastName查;只會進入其中一個 - trim 字符串截取(where(封裝查詢條件), set(封裝修改條件))
- foreach 遍歷集合
if 標籤
<!-- if標籤 -->
<!--
test:判斷表達式(OGNL)
遇見特殊符號應該去寫轉義字符,比如連接 && ,或者""
PS:&&可以用and替代, ""可以用''替代
-->
<!-- 查詢員工,要求,攜帶了哪個字段查詢條件就帶上這個字段的值 -->
<!-- public List<Employee> getEmpsByConditionIf(Employee employee); -->
<select id="getEmpsByConditionIf" resultType="com.atguigu.mybatis.bean.Employee">
select * from tbl_employee
<!--
where 標籤:相當於sql語句中的where,
不過where標籤會自動去除where之後的and
舉個例子,下面的sql語句中,我沒傳入id,但是傳入了lastName
如果沒用where標籤,sql語句就是:
select * from tbl_employee where and lastName = ?
這個sql語句自然是錯誤
用了where標籤,sql則是:
select * from tbl_employee where lastName = ?
-->
<where>
<if test="id!=null">
id=#{id}
</if>
<if test="lastName != null and lastName != ''">
and last_name like #{lastName}
</if>
<if test="email!=null and email.trim()!=''">
and email=#{email}
</if>
<!-- ognl會進行字符串與數字的轉換判斷 "0"==0 -->
<if test="gender==0 or gender==1">
and gender=#{gender}
</if>
</where>
</select>
choose 標籤
<!-- choose 標籤 -->
<!-- public List<Employee> getEmpsByConditionChoose(Employee employee); -->
<select id="getEmpsByConditionChoose" resultType="com.atguigu.mybatis.bean.Employee">
select * from tbl_employee
<where>
<!-- 如果帶了id就用id查,如果帶了lastName就用lastName查;只會進入其中一個 -->
<choose>
<when test="id!=null">
id=#{id}
</when>
<when test="lastName!=null">
last_name like #{lastName}
</when>
<when test="email!=null">
email = #{email}
</when>
<otherwise>
gender = 0
</otherwise>
</choose>
</where>
</select>
trim 標籤
<!-- trim 標籤 -->
<!--public List<Employee> getEmpsByConditionTrim(Employee employee); -->
<select id="getEmpsByConditionTrim" resultType="com.atguigu.mybatis.bean.Employee">
select * from tbl_employee
<!-- 如果後面多出的and或者or
where標籤則不能解決,此時就可以使用 trim標籤
prefix="":前綴:trim標籤體中是整個字符串拼串 後的結果。
prefix給拼串後的整個字符串加一個前綴
prefixOverrides="":
前綴覆蓋: 指定去掉整個字符串前面的某些字符
suffix="":後綴
suffix給拼串後的整個字符串加一個後綴
suffixOverrides=""
後綴覆蓋:指定去掉整個字符串後面的某些字符
-->
<!-- 自定義字符串的截取規則 -->
<trim prefix="where" suffixOverrides="and">
<if test="id!=null">
id=#{id} and
</if>
<if test="lastName!=null && lastName!=""">
last_name like #{lastName} and
</if>
<if test="email!=null and email.trim()!=""">
email=#{email} and
</if>
<!-- ognl會進行字符串與數字的轉換判斷 "0"==0 -->
<if test="gender==0 or gender==1">
gender=#{gender}
</if>
</trim>
</select>
<!-- 擴展:Trim更新拼串 -->
<update id="updateEmp">
update tbl_employee
<trim prefix="set" suffixOverrides=",">
<if test="lastName!=null">
last_name=#{lastName},
</if>
<if test="email!=null">
email=#{email},
</if>
<if test="gender!=null">
gender=#{gender}
</if>
</trim>
where id=#{id}
</update>
foreach 標籤
<!--foreach 標籤 -->
<!--public List<Employee> getEmpsByConditionForeach(List<Integer> ids); -->
<select id="getEmpsByConditionForeach" resultType="com.atguigu.mybatis.bean.Employee">
select * from tbl_employee
<!--
collection:指定要遍歷的集合:
list類型的參數會特殊處理封裝在map中,map的key就叫list
item:將當前遍歷出的元素賦值給指定的變量
separator:每個元素之間的分隔符
open:遍歷出所有結果拼接一個開始的字符
close:遍歷出所有結果拼接一個結束的字符
index:索引。遍歷list的時候是index就是索引,item就是當前值
遍歷map的時候index表示的就是map的key,item就是map的值
#{變量名}就能取出變量的值也就是當前遍歷出的元素
-->
<foreach collection="ids" item="item_id" separator=","
open="where id in(" close=")">
#{item_id}
</foreach>
</select>
使用foreach 實現批量保存
<!-- 批量保存 -->
<!--public void addEmps(@Param("emps")List<Employee> emps); -->
<!--MySQL下批量保存:可以foreach遍歷 mysql支持values(),(),()語法-->
<insert id="addEmps">
insert into tbl_employee(
<!--引用下面定義的sql代碼塊 -->
<include refid="insertColumn"></include>
)
values
<foreach collection="emps" item="emp" separator=",">
(#{emp.lastName},#{emp.email},#{emp.gender},#{emp.dept.id})
</foreach>
</insert>
<!--
抽取可重用的sql片段。方便引用
1、sql抽取:經常將要查詢的列名,或者插入用的列名抽取出來方便引用
2、include來引用已經抽取的sql:
3、include還可以自定義一些property,sql標籤內部就能使用自定義的屬性
include-property:取值的正確方式${prop},
#{不能使用這種方式}
-->
<sql id="insertColumn">
<if test="_databaseId=='oracle'">
employee_id,last_name,email
</if>
<if test="_databaseId=='mysql'">
last_name,email,gender,d_id
</if>
</sql>
<!--
擴展
不只是方法傳遞過來的參數可以被用來判斷,取值。。。
mybatis默認還有兩個內置參數:
_parameter:代表整個參數
單個參數:_parameter就是這個參數
多個參數:參數會被封裝爲一個map;_parameter就是代表這個map
比如傳來一個employee,可以使用#{_parameter.lastNAme}獲取屬性
_databaseId:如果配置了databaseIdProvider標籤。
_databaseId就是代表當前數據庫的別名oracle
-->