mybatis-plus:xml拼接sql方式
mybatis-plus:xml拼接sql
foreach操作map
<if test = "vo.map!= null and vo.map.size() > 0">
<foreach item="item" index="key" collection="vo.map">
<if test = "key != null and key != 'assetType'">
// jsonParam為json類型字段
and JSON_EXTRACT(jsonParam, '$.${key}') = #{item}
</if>
<if test = "key != null and key == 'assetType'">
and ${key} = #{item}
</if>
</foreach>
</if>foreach操作List
<if test="assetCodes != null and list.size > 0">
and field in
<foreach collection="list" item="e" index="index" open="(" close=")" separator=",">
#{e}
</foreach>
</if>foreach操作String
<if test="vo.departmentCodeList != null and vo.departmentCodeList != ''">
AND r.department_code in
<foreach item="departmentCode" collection="vo.departmentCodeList.split(',')" open="(" separator="," close=")">
#{departmentCode}
</foreach>
</if>mybatis-plus動態(tài)拼接sql語句
解釋
${ew.customSqlSegment} 是 MyBatis-Plus 中用于在 SQL 語句中插入自定義 SQL 片段的占位符。
ew 是指 Wrapper 對象(通常是 QueryWrapper 或 UpdateWrapper),而 customSqlSegment 是這個對象中的自定義 SQL 片段。
用途
這個占位符通常用于動態(tài)拼接 SQL 語句
允許在特定的 SQL 語句部分插入自定義的條件、過濾器或者其他 SQL 片段。
例子
QueryWrapper<MsCustomer> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("status", "active");
// 動態(tài)添加一個自定義的 SQL 片段
queryWrapper.apply("custom_column > {0}", someValue);
List<MsCustomer> customers = msCustomerMapper.selectList(queryWrapper);在 Mapper XML 文件中,可以像這樣使用 ${ew.customSqlSegment}:
<select id="selectCustomers" resultType="MsCustomer">
SELECT * FROM ms_customer ${ew.customSqlSegment}
</select>作用
customSqlSegment 允許你通過編程方式動態(tài)生成 SQL 語句中的某些部分,從而實現(xiàn)更靈活的查詢和操作。
例如:
如果在代碼中調(diào)用了 apply() 方法或其他添加條件的方法,這些條件會被自動拼接到 ${ew.customSqlSegment} 處,從而生成最終的 SQL 語句。
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java 定時器(Timer,TimerTask)詳解及實例代碼
這篇文章主要介紹了 Java 定時器(Timer,TimerTask)詳解及實例代碼的相關(guān)資料,需要的朋友可以參考下2017-01-01
Java實現(xiàn)將png格式圖片轉(zhuǎn)換成jpg格式圖片的方法【測試可用】
這篇文章主要介紹了Java實現(xiàn)將png格式圖片轉(zhuǎn)換成jpg格式圖片的方法,涉及java文件讀寫及圖形創(chuàng)建等相關(guān)操作技巧,需要的朋友可以參考下2018-03-03

