最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Mybatis中and和循環(huán)or混用操作(or轉(zhuǎn)換成in)

 更新時間:2021年07月13日 09:15:30   作者:NO0b  
這篇文章主要介紹了Mybatis中and和循環(huán)or混用操作(or轉(zhuǎn)換成in),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

Mybatis and和循環(huán)or混用

這次項目用到一個and和or混用的場景 , 因為用到多個or(循環(huán)), 沒想到好的辦法

最終轉(zhuǎn)換成用 IN實現(xiàn):

場景

用left join鏈接多個表, 多個條件and篩選, 其中狀態(tài)(state)條件篩選出多個可選狀態(tài)的條目,

本來想用and 和 or 但是 or的條件是個數(shù)組參數(shù), 需要遍歷states , 可能0個可能多個, 拼了半天沒有成功 , 最后發(fā)現(xiàn)用 IN 和FOREACH就可以了

DAO層接口

List<OrderInfoForm> selectOrdersByStatesSelective(
            @Param(value="order")Order order,
            @Param(value="states")Integer[] states);

Mybatis實現(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>

這里的重點是:

 <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>

把多個state的or關(guān)系轉(zhuǎn)化為 states in (state1,state2,state3...)

in中用foreach循環(huán)

mybatis plus and 和or合并寫法

記錄一下and 和 or 混合使用

sql 語句實現(xiàn)

SELECT  * FROM somc_operation_plan 
WHERE ( title LIKE '%測試%' 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()));

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

商水县| 赤峰市| 泰来县| 嘉禾县| 海城市| 涡阳县| 株洲市| 蒙自县| 定西市| 六安市| 乾安县| 张家界市| 石泉县| 个旧市| 耒阳市| 淳安县| 扶余县| 图木舒克市| 蒲江县| 阳信县| 隆安县| 绥宁县| 盐城市| 民勤县| 富蕴县| 清水县| 安丘市| 游戏| 武鸣县| 云和县| 临夏县| 九江县| 海城市| 云梦县| 广西| 曲水县| 黄浦区| 丰宁| 内黄县| 清原| 尚志市|