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