mybatis参数传递的问题

时间:2022-08-29 21:15:07

mybatis参数传递的问题:如何传递一个参数并在xxxMapper.xml中拿到参数值用于sql操作?如何传递多个参数并在xxxMapper.xml中拿到参数值用于sql操作?

1.基本数据类型只能传一个(int,String,Data等),通过#{参数名}即可获取传入值,参数名要严格一致。

举例:

接口代码:

List<FundoutRequest> selectPage1(String name);


Mapper的mybatis查询代码:

<select id="selectPage1" resultMap="BaseResultMap" >
    select 
    <include refid="Base_Column_List" />
    from cap_settlement_daily_summary where 1 = 1 
    <if test="name != null">
      and `id` = #{name,jdbcType=VARCHAR}
      </if></select>

2.复杂数据类型(例如java实体类,Map等),通过#{对象.属性名}或#{Map的key值}即可获取传入的值(#也可以看情况换成$,但是要注意在mybatis中使用$有可能会产生sql注入)。

举例:

接口代码:

List<FundoutRequest> selectPageNoLimit(FundoutRequest fundoutRequest);


Mapper的mybatis查询代码:

<select id="selectPageNoLimit" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from cap_settlement_daily_summary where 1 = 1 
    <if test="settlementDailySummary.id != null" >
      and `id` = #{settlementDailySummary.id,jdbcType=BIGINT}
    </if>
        <if test="settlementDailySummary.accountingDate != null" >
      and `accounting_date` = #{settlementDailySummary.accountingDate,jdbcType=VARCHAR}
    </if>
        <if test="settlementDailySummary.merchantId != null" >
      and `merchant_id` = #{settlementDailySummary.merchantId,jdbcType=VARCHAR}
    </if>
        <if test="settlementDailySummary.merchantName != null" >
      and `merchant_name` = #{settlementDailySummary.merchantName,jdbcType=VARCHAR}
    </if>
        <if test="settlementDailySummary.paymentTransactionCount != null" >
      and `payment_transaction_count` = #{settlementDailySummary.paymentTransactionCount,jdbcType=INTEGER}
    </if>
        <if test="settlementDailySummary.paymentTransactionAmount != null" >
      and `payment_transaction_amount` = #{settlementDailySummary.paymentTransactionAmount,jdbcType=DECIMAL}
    </if>
  </select>

3.传多个参数的方法:对于传的是多个基本数据类型的情况,必须在每个参数前使用注解标识(在接口的参数前添加@Param(value="参数名")),不然无法区分基本数据类型哪个是哪个。对于有不同的复杂数据类型的多个参数,建议也统一采用注解标识。通过#{对象.属性名}或#{Map的key值}即可获取传入的参数值(#也可以看情况换成$,但是要注意在mybatis中使用$有可能会产生sql注入)。

举例:

接口代码:

List<FundoutRequest> selectPage(@Param("fundoutRequest") FundoutRequest fundoutRequest, @Param("pageable")0 Pageable pageable);
</pre><p>Mapper的mybatis查询代码:</p><pre name="code" class="java"><select id="selectPage" resultMap="BaseResultMap" >    select     <include refid="Base_Column_List" />    from cap_settlement_daily_summary where 1 = 1         <if test="settlementDailySummary.id != null" >      and `id` = #{settlementDailySummary.id,jdbcType=BIGINT}    </if>        <if test="settlementDailySummary.accountingDate != null" >      and `accounting_date` = #{settlementDailySummary.accountingDate,jdbcType=VARCHAR}    </if>        <if test="settlementDailySummary.merchantId != null" >      and `merchant_id` = #{settlementDailySummary.merchantId,jdbcType=VARCHAR}    </if>        <if test="settlementDailySummary.merchantName != null" >      and `merchant_name` = #{settlementDailySummary.merchantName,jdbcType=VARCHAR}    </if>        <if test="settlementDailySummary.paymentTransactionCount != null" >      and `payment_transaction_count` = #{settlementDailySummary.paymentTransactionCount,jdbcType=INTEGER}    </if>        <if test="settlementDailySummary.paymentTransactionAmount != null" >      and `payment_transaction_amount` = #{settlementDailySummary.paymentTransactionAmount,jdbcType=DECIMAL}    </if>        <if test="pageable.sort != null" >      order by       <foreach collection="pageable.sort" item="order" separator="," >        <if test="   order.property == 'id'    || order.property == 'accounting_date'   || order.property == 'merchant_id'   || order.property == 'merchant_name'   || order.property == 'payment_transaction_count'   || order.property == 'payment_transaction_amount'        " >        ${order.property} ${order.direction}        </if>      </foreach>    </if>    <if test="pageable.offset >= 0 and pageable.pageSize > 0" >      limit ${pageable.offset}, ${pageable.pageSize}    </if>  </select>