【mybatis】mybatis动态order by 的问题, 注意 只需要把#{} 改成 ${} 即可

2022-12-29,,,

先说解决方案:

注意  只需要把#{} 改成 ${}  即可

 

 

再看 使用过程:

Mapper.java

List<IntegralGoods> findInUid(@Param("uidList") List<String> uidList,@Param("order") String order,@Param("orderType") String orderType);

首先,是这样的mybatis拼接的sql语句

<select id="findInUid" parameterType="com.pisen.cloud.luna.ms.jifen.base.domain.IntegralGoods" resultMap="baseResBean">

        select
        a.id as 'id',
        a.uid as 'uid',
        a.create_date as 'createDate',
        a.update_date as 'updateDate',
        a.update_id as 'updateId',
        a.create_id as 'createId',
        a.brand_uid as 'brandUid',
        a.tid as 'tid',
        a.stock as 'stock',
        a.name as 'name',
        a.goods_code as 'goodsCode',
        a.market_value as 'marketValue',
        a.specification as 'specification',
        a.remark as 'remark',
        a.type as 'type',
        a.integral as 'integral',
        a.description as 'description',
        a.sale_num as 'saleNum',
        a.limit_num as 'limitNum',
        a.shelf_flag as 'shelfFlag',
        a.home_show_flag as 'homeShowFlag',
        sl.shelf_date as 'shelfDate',
        sl.obtained_date as 'obtainedDate',
        b.id b_id,
        b.src b_src,
        b.type b_type,
        b.sort  b_sort
        from
            integral_goods a
        left join
            integral_goods_img b
        on
            a.uid = b.integral_goods_id
        left join
            shelf_log sl
        on
            a.uid = sl.integral_goods_uid
        <where>
                    a.uid
            IN
            <foreach collection="uidList" item="item" index="index" open="(" separator="," close=")">
                #{item}
            </foreach>
            <if test="order != null and orderType">
                order by #{order} #{orderType}
            </if>
        </where>

    </select>

 

执行的sql语句是这样:

SELECT
    a.id AS 'id',
    a.uid AS 'uid',
    a.create_date AS 'createDate',
    a.update_date AS 'updateDate',
    a.update_id AS 'updateId',
    a.create_id AS 'createId',
    a.brand_uid AS 'brandUid',
    a.tid AS 'tid',
    a.stock AS 'stock',
    a. NAME AS 'name',
    a.goods_code AS 'goodsCode',
    a.market_value AS 'marketValue',
    a.specification AS 'specification',
    a.remark AS 'remark',
    a.type AS 'type',
    a.integral AS 'integral',
    a.description AS 'description',
    a.sale_num AS 'saleNum',
    a.limit_num AS 'limitNum',
    a.shelf_flag AS 'shelfFlag',
    a.home_show_flag AS 'homeShowFlag',
    sl.shelf_date AS 'shelfDate',
    sl.obtained_date AS 'obtainedDate',
    b.id b_id,
    b.src b_src,
    b.type b_type,
    b.sort b_sort
FROM
    integral_goods a
LEFT JOIN integral_goods_img b ON a.uid = b.integral_goods_id
LEFT JOIN shelf_log sl ON a.uid = sl.integral_goods_uid
WHERE
    a.uid IN (
        '09f163c30c504d7fb571186db9c10ff3',
        'd8a9184443524e369be59417e9edd409',
        '778b5c0fbc4141b4bd8de3b7756a1d9d',
        '58d53fe0126d47c8bf382647244e2b1d'
    )
ORDER BY
    'integral' 'asc'

 

这样执行sql  是没有效果的 !!!

正确的

<select id="findInUid" parameterType="com.pisen.cloud.luna.ms.jifen.base.domain.IntegralGoods" resultMap="baseResBean">

        select
        a.id as 'id',
        a.uid as 'uid',
        a.create_date as 'createDate',
        a.update_date as 'updateDate',
        a.update_id as 'updateId',
        a.create_id as 'createId',
        a.brand_uid as 'brandUid',
        a.tid as 'tid',
        a.stock as 'stock',
        a.name as 'name',
        a.goods_code as 'goodsCode',
        a.market_value as 'marketValue',
        a.specification as 'specification',
        a.remark as 'remark',
        a.type as 'type',
        a.integral as 'integral',
        a.description as 'description',
        a.sale_num as 'saleNum',
        a.limit_num as 'limitNum',
        a.shelf_flag as 'shelfFlag',
        a.home_show_flag as 'homeShowFlag',
        sl.shelf_date as 'shelfDate',
        sl.obtained_date as 'obtainedDate',
        b.id b_id,
        b.src b_src,
        b.type b_type,
        b.sort  b_sort
        from
            integral_goods a
        left join
            integral_goods_img b
        on
            a.uid = b.integral_goods_id
        left join
            shelf_log sl
        on
            a.uid = sl.integral_goods_uid
        <where>
                    a.uid
            IN
            <foreach collection="uidList" item="item" index="index" open="(" separator="," close=")">
                #{item}
            </foreach>
            <if test="order != null and orderType">
                order by ${order} ${orderType}
            </if>
        </where>

    </select>

 

执行的sql语句是这样的:

SELECT
    a.id AS 'id',
    a.uid AS 'uid',
    a.create_date AS 'createDate',
    a.update_date AS 'updateDate',
    a.update_id AS 'updateId',
    a.create_id AS 'createId',
    a.brand_uid AS 'brandUid',
    a.tid AS 'tid',
    a.stock AS 'stock',
    a. NAME AS 'name',
    a.goods_code AS 'goodsCode',
    a.market_value AS 'marketValue',
    a.specification AS 'specification',
    a.remark AS 'remark',
    a.type AS 'type',
    a.integral AS 'integral',
    a.description AS 'description',
    a.sale_num AS 'saleNum',
    a.limit_num AS 'limitNum',
    a.shelf_flag AS 'shelfFlag',
    a.home_show_flag AS 'homeShowFlag',
    sl.shelf_date AS 'shelfDate',
    sl.obtained_date AS 'obtainedDate',
    b.id b_id,
    b.src b_src,
    b.type b_type,
    b.sort b_sort
FROM
    integral_goods a
LEFT JOIN integral_goods_img b ON a.uid = b.integral_goods_id
LEFT JOIN shelf_log sl ON a.uid = sl.integral_goods_uid
WHERE
    a.uid IN (
        '09f163c30c504d7fb571186db9c10ff3',
        'd8a9184443524e369be59417e9edd409',
        '778b5c0fbc4141b4bd8de3b7756a1d9d',
        '58d53fe0126d47c8bf382647244e2b1d'
    )
ORDER BY
    integral ASC