Mybatis中and和循環(huán)or混用操作(or轉(zhuǎn)換成in)
Mybatis and和循環(huán)or混用
這次項(xiàng)目用到一個(gè)and和or混用的場(chǎng)景 , 因?yàn)橛玫蕉鄠€(gè)or(循環(huán)), 沒(méi)想到好的辦法
最終轉(zhuǎn)換成用 IN實(shí)現(xiàn):
場(chǎng)景
用left join鏈接多個(gè)表, 多個(gè)條件and篩選, 其中狀態(tài)(state)條件篩選出多個(gè)可選狀態(tài)的條目,
本來(lái)想用and 和 or 但是 or的條件是個(gè)數(shù)組參數(shù), 需要遍歷states , 可能0個(gè)可能多個(gè), 拼了半天沒(méi)有成功 , 最后發(fā)現(xiàn)用 IN 和FOREACH就可以了
DAO層接口
List<OrderInfoForm> selectOrdersByStatesSelective( @Param(value="order")Order order, @Param(value="states")Integer[] states);
Mybatis實(shí)現(xiàn)
<select id="selectOrdersByStatesSelective" resultMap="AllResultMap" > select <include refid="All_Column_List" /> from order_list LEFT JOIN product_method ON product_method.`code` = order_list.purchase_method LEFT JOIN product_color ON product_color.`code` = order_list.color LEFT JOIN product_guarantee ON product_guarantee.`code` = order_list.guarantee LEFT JOIN product_info ON order_list.product_id = product_info.id LEFT JOIN product_model ON product_info.model = product_model.`code` LEFT JOIN product_standard ON product_info.standard = product_standard.`code` LEFT JOIN product_state ON product_state.`code` = order_list.order_state LEFT JOIN product_apperance ON product_apperance.`code` = order_list.apperance LEFT JOIN product_brand ON product_brand.`code` = product_info.brand <where> <if test="order.orderNum != null " > order_num like "%"#{order.orderNum,jdbcType=VARCHAR}"%" </if> <if test="order.operator != null " > and operator like "%"#{order.operator,jdbcType=VARCHAR}"%" </if> <if test="order.purchaseTime != null" > and purchase_time = #{order.purchaseTime,jdbcType=DATE} </if> <if test="order.orderState != null" > and order_state = #{order.orderState,jdbcType=VARCHAR} </if> <if test="order.serialNum != null" > and serial_num like "%"#{order.serialNum,jdbcType=VARCHAR}"%" </if> <if test="states != null and states.length >0"> <foreach collection="states" item="state" separator="," open=" and order_state in (" close=")"> #{state,jdbcType=BIGINT} </foreach> </if> </where> </select>
這里的重點(diǎn)是:
<if test="states != null and states.length >0"> <foreach collection="states" item="state" separator="," open=" and order_state in (" close=")"> #{state,jdbcType=BIGINT} </foreach> </if>
把多個(gè)state的or關(guān)系轉(zhuǎn)化為 states in (state1,state2,state3...)
in中用foreach循環(huán)
mybatis plus and 和or合并寫(xiě)法
記錄一下and 和 or 混合使用
sql 語(yǔ)句實(shí)現(xiàn)
SELECT * FROM somc_operation_plan WHERE ( title LIKE '%測(cè)試%' AND ( charge_user = 'xxx' OR execute_user = 'xxx' ) )
LambdaQueryWrapper<SomcOperationPlan> queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.like(StringUtils.isNotEmpty(operationPlan.getTitle()), SomcOperationPlan::getTitle, operationPlan.getTitle()) .and(wrapper -> wrapper.eq(StringUtils.isNotEmpty(operationPlan.getChargeUser()), SomcOperationPlan::getChargeUser, operationPlan.getChargeUser()).or().eq(StringUtils.isNotEmpty(operationPlan.getExecuteUser()), SomcOperationPlan::getExecuteUser, operationPlan.getExecuteUser()));
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
java實(shí)現(xiàn)簡(jiǎn)單QQ登錄界面
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)簡(jiǎn)單QQ登錄界面,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05關(guān)于QueryWrapper,實(shí)現(xiàn)MybatisPlus多表關(guān)聯(lián)查詢(xún)方式
這篇文章主要介紹了關(guān)于QueryWrapper,實(shí)現(xiàn)MybatisPlus多表關(guān)聯(lián)查詢(xún)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。2022-01-01springboot+vue實(shí)現(xiàn)阿里云oss大文件分片上傳的示例代碼
阿里云推出了直傳,本文主要介紹了springboot+vue實(shí)現(xiàn)阿里云oss大文件分片上傳的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-06-06Java多線程編程實(shí)戰(zhàn)之模擬大量數(shù)據(jù)同步
這篇文章主要介紹了Java多線程編程實(shí)戰(zhàn)之模擬大量數(shù)據(jù)同步,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02java調(diào)用微信接口實(shí)現(xiàn)網(wǎng)頁(yè)分享小功能
這篇文章主要為大家詳細(xì)介紹了java調(diào)用微信接口實(shí)現(xiàn)網(wǎng)頁(yè)分享小功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04Mybatis-Plus中g(shù)etOne方法獲取最新一條數(shù)據(jù)的示例代碼
這篇文章主要介紹了Mybatis-Plus中g(shù)etOne方法獲取最新一條數(shù)據(jù),本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-05-05Java訂單30分鐘未支付自動(dòng)取消該怎么實(shí)現(xiàn)
在開(kāi)發(fā)中往往會(huì)遇到一些關(guān)于延時(shí)任務(wù)的需求,例如生成訂單30分鐘未支付,則自動(dòng)取消,下面這篇文章主要給大家介紹了關(guān)于Java訂單30分鐘未支付自動(dòng)取消該怎么實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2023-03-03Java中ClassLoader類(lèi)加載學(xué)習(xí)總結(jié)
本篇文章主要給大家講述了Java中ClassLoader類(lèi)加載的原理以及用法總結(jié),一起學(xué)習(xí)下。2017-12-12