MyBatis 引號分割字符串

在這裏插入圖片描述
在這裏插入圖片描述
runProperty 參數會有多個,比如“卡班車,網點車”

傳到xml,

<if test="runProperty != null and runProperty != ''">
		AND RUN_PROPERTY IN 
		<foreach collection="runProperty" item="runProperty" index="index" open="(" separator="," close=")">
			#{runProperty}
		</foreach>
</if>

這樣打印的sql如下圖
在這裏插入圖片描述
這樣肯定是不對的。


正確寫法:
<if test="runProperty != null and runProperty != ''">
			AND RUN_PROPERTY IN 
			<foreach collection="runProperty.split(',')" item="runProperty" index="index" open="(" separator="," close=")">
				#{runProperty}
			</foreach>
</if>

已split(’,’)分割,得到的sql就是

RUN_PROPERTY in('卡班車','網點車') 

有什麼更好的解決方案,歡迎大佬指教!

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