【mybatis】mybatis动态order by 的问题, 注意 只需要把#{} 改成 ${} 即可 - Angel挤一挤 - 博客园

先说解决方案:

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

再看 使用过程:

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

复制代码; "复制代码")


原网址: 访问
创建于: 2021-07-19 11:28:53
目录: default
标签: 无

请先后发表评论
  • 最新评论
  • 总共0条评论