zoukankan      html  css  js  c++  java
  • mybatis中mapper接口的参数设置几种方法

    方法一:忽略parameterType,加@param("xxx")注解

    在mapper接口中加上@param("xxx")注解,则在配置文件中直接用即可

    List<Map<String, Object>> getDataByTime(@Param("startTime") String startTime, @Param("endTime") String endTime, @Param("platformId") Long platformId);
    
    <select id="getDataByTime" resultType="java.util.Map">
          SELECT
            t.seller_id as sellerId,
            sum(t.payment_price) as total,
          FROM
            trade_orders t
          WHERE
            AND
              t.platform_id = #{platformId}
            <if test="startTime != null and startTime != ''">
            AND
                <![CDATA[t.order_time >= #{startTime}]]>
            </if>
            <if test="endTime != null and endTime != ''">
            AND
              <![CDATA[t.order_time <= #{endTime}]]>
            </if>
            GROUP BY
              t.seller_id
        </select>
    

    方法二:忽略parameterType,不加@param("xxx")注解

    用#{index},是第几个就用第几个的索引,索引从0开始

    List<Map<String, Object>> getDataByTime(String startTime, String endTime, Long platformId);
    
    <select id="getDataByTime" resultType="java.util.Map">
          SELECT
            t.seller_id as sellerId,
            sum(t.payment_price) as total,
          FROM
            trade_orders t
          WHERE
            AND
              t.platform_id = #{3}
            <if test="startTime != null and startTime != ''">
            AND
                <![CDATA[t.order_time >= #{0}]]>
            </if>
            <if test="endTime != null and endTime != ''">
            AND
              <![CDATA[t.order_time <= #{1}]]>
            </if>
            GROUP BY
              t.seller_id
        </select>
    

    方法三:使用Map封装参数,parameterType=“hashmap”

    封装好后,直接在配置文件引用#{key}即可

    List<Map<String, Object>> getDataByTime(HashMap map);
    
    <select id="getDataByTime" parameterType="hashmap" resultType="java.util.Map">
          SELECT
            t.seller_id as sellerId,
            sum(t.payment_price) as total,
          FROM
            trade_orders t
          WHERE
            AND
              t.platform_id = #{platformId}
            <if test="startTime != null and startTime != ''">
            AND
                <![CDATA[t.order_time >= #{startTime}]]>
            </if>
            <if test="endTime != null and endTime != ''">
            AND
              <![CDATA[t.order_time <= #{endTime}]]>
            </if>
            GROUP BY
              t.seller_id
        </select>
    

    方法四:使用List封装参数

    mapper配置文件使用foreach标签循环list

    List<Map<String, Object>> getDataByTime(List<String> list);  
    
    <select id="getXXXBeanList" resultType="java.util.Map">
      select XX from trade_orders where id in
      <foreach item="item" index="index" collection="list" open="(" separator="," close=")">  
        #{item}  
      </foreach>  
    </select> 
    
  • 相关阅读:
    Redis环境搭建(整合SprinigBoot一键切换集群和单机环境)
    Docker + SpringBoot2.0快速构建镜像
    kafka实现分布式事务
    【本地事物】
    【数据结构之二叉树】
    变量
    python 介绍
    编程语言的区别
    路飞学城python教程 第二章练习题
    windows下nginx配合ffmpeg推送视频流
  • 原文地址:https://www.cnblogs.com/kobelieve/p/10490635.html
Copyright © 2011-2022 走看看