【mybatis】mybatis動態order by 的問題, 注意 只須要把#{} 改爲 ${} 便可

先說解決方案:java

注意  只須要把#{} 改爲 ${}  便可sql

 

 

再看 使用過程:mybatis

Mapper.javaapp

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

首先,是這樣的mybatis拼接的sql語句dom

<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語句是這樣:ui

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  是沒有效果的 !!!spa

正確的code

<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語句是這樣的:blog

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
相關文章
相關標籤/搜索