Mybatis 動態標籤 choose when otherwise

本質上是if else
需求:當參數 id 有值的時候優先使id 查詢,當 id 沒有值時就去判斷用戶名是否有值,如果有值就用用戶名查詢 ,如果用戶名也沒有值,就使查詢無結果。

SysUser selectByidOrUserName(SysUser sysUser) ;
<select id=”selectByidOrUserName” resultType=” tk.mybatis.simple.m0del. SysUser” >
	select id,
	user name userName ,
	us e r password userPassword,
	user email userEmail ,
	user info userinfo ,
	head_img headimg ,
	create time createTime
	from sys_ user
	where 1 = 1
	<choose>
	<when test=” id != null” >
	and id= #{id}
	</when>
	<when test=” userName != null and userName !=””>
	and user name = #{userName}
	</when>
	<otherwise>
	and 1 = 2
	</otherwise>
	</ choose>
</select>

在這裏插入圖片描述

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