Mybatis实战 之 入参封装

本文详细介绍了Mybatis中传入参数的封装方法,包括单个参数、多个参数、使用@Param注解、POJO对象和Map入参的场景,以及特殊场景如部分参数声明、多参数结合POJO和使用Collection(List、Set、Array)入参的处理方式。无论哪种方式,Mybatis最终都会将参数封装为Map进行处理。

Mybatis实战 之 入参封装

本节我们将详细学习一下Mybatis对传入参数的封装或处理。


实例

  • 单个参数的处理
    对于单个参数的传入,在Mapper接口中我们可以直接使用该参数

    • 例子

       int deleteByPrimaryKey(Integer pid);
       <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
          delete from products
          where pid = #{pid,jdbcType=INTEGER}
        </delete>

  • 传入多个参数
    传入多个参数并且使用Mybatis默认的处理方式的话,Mybatis会将传入的参数按先后顺序转化为一个Map,第一个参数的Key值为 param1,第二个参数的Key值为 param2,依次类推。在Mapper接口中将直接使用param1…..paramN来代表指定位置的参数。

    • 例子

      Product selectProductByCritiers(String pname,String type);
       <select id="selectProductByCritiers" resultMap="BaseResultMap">
          select 
          <include refid="Base_Column_List" />
          from products
          where pname = #{param1,jdbcType=VARCHAR}
          and type=#{param2,jdbcType=VARCHAR}
        </select>

      入参有顺序限制


  • 使用@Param 注解传入多个参数
    在使用@Param注解传入参数时,Mybatis将为每一个参数根据@Param中的值封装成一个Map,Map中的每一个Key值为每一个@Param中的值,无先后顺序,@Param中的值唯一。

    • 例子

       Product selectProductByCritiers(@Param("productName")String pname,@Param("ProductType")String type);
      <select id="selectProductByCritiers" resultMap="BaseResultMap">
          select 
          <include refid="Base_Column_List" />
          from products
          where pname = 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值