一文搞懂MyBatis的几种传参

1.parameterType="string"或者int/long等单个参数,则在获取参数时,分两种情况:

如果是要进行非null的判断,则不可以在if后直接那变量名进行判空,因为mybatis会默认变量名为_parameter,否则会报no getter/setter错误。要写成< if test="_ parameter!=null and _parameter!='' ">

User getUserById(String uid);
<select id="getUserById" resultTypeb ="com.apple.entity.User">
        SELECT
        <include refid="BASE_COLUMN"/>
        FROM
        <include refid="BASE_TABLE"/>
        where 1=1
          <if test="_parameter!=null and _parameter!=''  ">
            and uid=#{uid}
        </if>
        
        <!--不加上@Param直接使用会报no getter/setter错误-->
        <!--<if test="uid!=null">-->
        <!--uid=#{uid}-->
        <!--</if>-->
        
        <!--如果不要进行非null的判断,可以直接使用-->
        <!--and uid=#{uid}-->
</select>

上面情况是针对于string类型参数直接传入,如果不想在判断时使用,mybatis默认的变量名,则需要在dao层后台传入时加上@Param参数可以直接使用变量判断

如果不要进行非null的判断,则可以不用_parameter,直接使用where uid=#{uid}

_parameter:代表整个参数

  • 单个参数:_parameter就是这个参数
  • 多个参数:参数会被封装为一个map;_parameter就是代表这个map

2、对于多个String类型,或者String和int的组合传入,则无法使用Mybatis默认的_parameterType。

1)使用@Param注解,在mybatis中可以直接将此作为变量名判断和使用:

User getUserById4(@Param("uid")Integer uid,@Param("password")String password);
<select id="getUserById4" resultType="com.apple.entity.User">
        SELECT
        <include refid="BASE_COLUMN"/>
        FROM
        <include refid="BASE_TABLE"/>
        where 1=1
        <if test="uid!=null and uid!='' ">
            and uid=#{uid}
        </if>
        <if test="password!=null and password!='' ">
            and password=#{password}
        </if>
</select>

2)不用@param注解,使用下标索引的方式,这里我用#{0}会报错,要用#{arg0}:

User getUserById4(Integer uid,String password);
<select id="getUserById4" resultType="com.apple.entity.User">
        SELECT
        <include refid="BASE_COLUMN"/>
        FROM
        <include refid="BASE_TABLE"/>
        where 1=1
        <if test="arg0!=null and arg0!='' ">
            and uid=#{arg0}
        </if>
        <if test="arg1!=null and arg1!='' ">
            and password=#{arg1}
        </if>
    </select>

3、传参为map,直接使用key判断,跟上面多个string类似

User getUserById5(Map<String, String> paramMap);
Map<String, String> paramMap = new HashMap<>();
paramMap.put("uid", "1");  //主键id可以将int 当做string 传入也能查询
paramMap.put("password", "123");
<select id="getUserById5" resultType="com.apple.entity.User">
        SELECT
        <include refid="BASE_COLUMN"/>
        FROM
        <include refid="BASE_TABLE"/>
        where 1=1
        <if test="uid!=null and uid!='' ">
            and uid=#{uid}
        </if>
        <if test="password!=null and password!='' ">
            and password=#{password}
        </if>
</select>

4、传参为list,parameterType不用写,分两种,List< String/Integer/Long>和List< User/Map>

第一种List< String/Integer/Long>

注意:

  • 如果参数不加@param注解,if和foreach标签里面是用list进行判断!!

  • 如果想用自己写的的参数名称判断,就需要加@param,不然识别不了!!

List<User> getUserById6(List<Integer> list);  
//这里写成下面这种方式,if和foreach里面也是用list进行判断,而不是用idList,不然会报错,要写成idList要加上@param
List<User> getUserById6(List<Integer> idList); 
 <select id="getUserById6" resultType="com.apple.entity.User">
        SELECT
        <include refid="BASE_COLUMN"/>
        FROM
        <include refid="BASE_TABLE"/>
        where 1=1
        <if test="list!=null ">
            and uid in
            <foreach collection="list" index="index" separator="," item="item" open="(" close=")">
                #{item}
            </foreach>
        </if>
    </select>

第二种List< User/Map>

void  addUserBatch(List<User> list);
<!--注意批量插入这种情况下foreach标签不要open="(" close=")"-->
<insert id="addUserBatch">
	insert into t_user(username,password) values
	<foreach collection="list" item="item" index="index" separator=",">
	(
		#{item.username},
		#{item.password}
	)
	</foreach>
</insert>

5、传参为javabean,传参分两种

传参不加上@Param:

void addUser(User user);
<insert id="addUser">
    insert into t_user(username,password) values
    (#{username},#{password})
</insert>

如果传参加上@Param,则xml文件要加上user.

void addUser(@Param("user") User user);
<insert id="addUser">
    insert into t_user(username,password) values
    (#{user.username},#{user.password})
</insert>

以上几种传参:

  • 如果是select查询都不用写paramType,可以直接找到对应的类型,而resultType必须写;

  • 如果是insert/update/delete的时候,paramType和resultType都不用写!

  • foreach标签的open 里面可以写and in 这种 不一定只有(

推荐单个或者多个参数(除了map这种)的时候都写上@param防止出错!!!

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Apple_Web

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值