mybatis中的if-else及if嵌套使用方式
if-else及if嵌套使用方式
案例一:if-else
在使用mybatis mapper 動態(tài)sql時(shí),不免會出現(xiàn)if-else的使用,但是好像又沒有這種語法,提供的是choose標(biāo)簽代替if-else
例如:
select * from t_stu t <where> ? ? <choose> ? ? ? ? <when test="query == 0"> ? ? ? ? ? ? and t.status = 1? ? ? ? ? </when> ? ? ? ? <otherwise> ? ? ? ? ? ? ? ? and t.status ?NOT IN (9,5) ? ? ? ? </otherwise> ? ? </choose> ? ? and t.delete_status = 1 </where>
也可以用多個(gè)if判斷實(shí)現(xiàn):
select * from t_stu t <where> ? ? <if test="query == 0"> ? ? ? ? and t.status = 1? ? ? </if> ? ? <if test="query != 0"> ? ? ? ? and t.status ?NOT IN (9,5) ? ? </if> ? ? and t.delete_status = 1 </where>
案例二:if嵌套
在實(shí)際編碼過程中會有一些判斷條件會一直重復(fù)使用,一直寫在if標(biāo)簽中寫的代碼會特長,而且臃腫
select * from t_stu t <where> ? ? <if test="query == 0 and type = 1"> ? ? ? ? and t.type = 'we' and t.delete = 1 ? ? </if> ? ? <if test="query == 0 and type = 2"> ? ? ? ? and t.type = 'wq' and t.delete = 1 ? ? </if> ? ? <if test="query == 0 and type = 3"> ? ? ? ? and t.type = 'wr' and t.delete = 1 ? ? </if> </where>
變現(xiàn)后:
select * from t_stu t <where> ? ? <if test="query == 0"> ? ? ? ? <if test="type = 1"> ? ? ? ? ? ? and t.type = 'we' ? ? ? ? </if> ? ? ? ? ?<if test="type = 2"> ? ? ? ? ? ? and t.type = 'wq' ? ? ? ? </if> ? ? ? ? <if test="type = 3"> ? ? ? ? ? ? and t.type = 'wr' ? ? ? ? </if> ? ? </if> ? ? and t.delete = 1 </where>
mybatis if-else寫法
mybaits中沒有else要用chose when otherwise代替
<choose> ? ? <when test=""> ? ? ? ? //... ? ? </when> ? ? <otherwise> ? ? ? ? //... ? ? </otherwise> </choose>
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
多個(gè)SpringBoot項(xiàng)目采用redis實(shí)現(xiàn)Session共享功能
這篇文章主要介紹了多個(gè)SpringBoot項(xiàng)目采用redis實(shí)現(xiàn)Session共享,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
Java Stream 流實(shí)現(xiàn)合并操作示例
這篇文章主要介紹了Java Stream 流實(shí)現(xiàn)合并操作,結(jié)合實(shí)例形式詳細(xì)分析了Java Stream 流實(shí)現(xiàn)合并操作原理與相關(guān)注意事項(xiàng),需要的朋友可以參考下2020-05-05
java web項(xiàng)目實(shí)現(xiàn)文件下載實(shí)例代碼
現(xiàn)在項(xiàng)目里面有個(gè)需求,需要把系統(tǒng)產(chǎn)生的日志文件給下載到本地 先獲取所有的日志文件列表,顯示到界面,選擇一個(gè)日志文件,把文件名傳到后臺2013-09-09
springboot2.0+elasticsearch5.5+rabbitmq搭建搜索服務(wù)的坑
這篇文章主要介紹了springboot2.0+elasticsearch5.5+rabbitmq搭建搜索服務(wù)的坑,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-06-06
JavaScript中new運(yùn)算符的實(shí)現(xiàn)過程解析
這篇文章主要介紹了JavaScript中new運(yùn)算符的實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10
java自定義注解實(shí)現(xiàn)前后臺參數(shù)校驗(yàn)的實(shí)例
下面小編就為大家?guī)硪黄猨ava自定義注解實(shí)現(xiàn)前后臺參數(shù)校驗(yàn)的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-11-11

