zoukankan      html  css  js  c++  java
  • 【mybatis】mybatis动态order by 的问题, 注意 只需要把#{} 改成 ${} 即可

    先说解决方案:

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

    再看 使用过程:

    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
  • 相关阅读:
    电子设计基础——有效值检波技术
    电子设计基础——电源的各项指标及测量方法
    电子设计基础——电源的各项指标及测量方法
    计算机网络——IP地址与MAC地址
    计算机网络——IP地址与MAC地址
    LDC1000学习资料
    移动端制作公共样式reset
    单行、多行溢出省略
    垂直居中常用布局
    移动端rem布局必要的meta和script
  • 原文地址:https://www.cnblogs.com/sxdcgaq8080/p/9848016.html
Copyright © 2011-2022 走看看