Mybatis @Param用和不用的区别

一、单个参数

1、基本数据类型:SQL语句中不论有没有动态SQL,加不加@Param都可。test和#{}中写什么均可,不过一般参数保持一致。

User selectByUsername(String username);
<!--普通SQL-->
<select id="selectByUsername" resultType="com.university.pojo.entity.User">
    SELECT * FROM sys_user WHERE username = #{username}
</select>
<!--动态SQL-->
<select id="selectByUsername" resultType="com.university.pojo.entity.User">
    SELECT * FROM user
    <where>
        <if test="username != null and username != ''">
           and username = #{username}
        </if>
    </where>
</select>

2、对象:SQL语句中不论有没有动态SQL

(1)加@Param,test和#{}中获取方式是对象.属性,因为绑定的是对象。

List<User> getUser(@Param("user") User user);
<select id="getUser" resultType="com.university.pojo.entity.User">
    SELECT * FROM user
    <where>
        <if test="user.id != null and user.id != ''">
            and id = #{user.id}
        </if>
        <if test="user.username != null and user.username != ''">
            and username = #{user.username}
        </if>
    </where>
</select>

(2)不加@Param,test和#{}中获取方式直接属性即可。

List<User> getUser(User user);
<select id="getUser" resultType="com.university.pojo.entity.User">
    SELECT * FROM user
    <where>
        <if test="id != null and id != ''">
            and id = #{id}
        </if>
        <if test="username != null and username != ''">
            and username = #{username}
        </if>
    </where>
</select>

二、多个参数

1、基本数据类型

(1)不加@Param注解,可以写 param1、param2 … 这种的参数表示形式,也可以参数写一致(因为mybatis-plus高版本之后(经测试3.1.2必须要加)允许不加 @Param指定参数名称,自动会以入参的名称作为param key,低版本必须要加上@Param),如下:

List<User> selectUser(Integer id, String username);
<select id="selectUser" resultType="com.university.pojo.entity.User">
    SELECT * FROM user
    <where>
        <if test="param1 != null and param1 != ''">
            and id = #{param1}
        </if>
        <if test="param2 != null and param2 != ''">
            and username = #{param2}
        </if>
    </where>
</select>

(2)加了@param之后,xml中就可以直接输入参数了,但是经测试写param1、param2 … 这种的参数也没问题,不过最好保持参数一致。

List<User> selectUser(@Param("id") Integer id, @Param("username") String username);
<select id="selectUser" resultType="com.university.pojo.entity.User">
     SELECT * FROM user
     <where>
         <if test="id != null and id != ''">
             and id = #{id}
         </if>
         <if test="username != null and username != ''">
             and username = #{username}
         </if>
     </where>
</select>

2、参数包含对象:加不加@Param均可,都是通过对象.属性获取

List<User> getUser(String role,User user);
<select id="getUser" resultType="com.university.pojo.entity.User">
    SELECT * FROM user
    <where>
        <if test="user.id != null and user.id != ''">
            and id = #{user.id}
        </if>
        <if test="user.username != null and user.username != ''">
            and username = #{user.username}
        </if>
        <if test="role != null and role != ''">
            and role = #{role}
        </if>
    </where>
</select>

三、集合(针对低版本,经测试mybatis-plus3.1.2版本必须要加)

1、list作为参数,不加@Param的话,标签内的collection必须为list,否则报如下错:

在这里插入图片描述

正确的示例:

void insertUser(@Param("userList") List<User> userList);
<insert id="insertUser">
      insert into user(username,password) values
      <foreach collection="userList" item="user">
          (#{user.username}, #{user.password})
      </foreach>
</insert>

或者不加@Param,collection中使用list也可以。

2、数组作为参数,不加@Param的话,标签内的collection必须为array。

3、map作为参数,直接用属性值即可。如:

UserInfo selectByUserIdAndStatusMap(Map<String,Object> map);
<select id="selectByUserIdAndStatusMap" resultType="cn.cb.demo.domain.UserInfo">
     select * from user_info where user_id=#{userId} and status=#{status}
</select>

测试如下:

@Test
void contextLoads() {
    Map<String,Object> map=new HashMap<>();
    map.put("userId","1222");
    map.put("status",1);
    UserInfo userInfo = userMapper.selectByUserIdAndStatusMap(map);
    System.out.println(userInfo);
}

参考文章:mybatis 参数处理,单个参数,多个参数_mybatis 单个参数_f45056231p的博客-CSDN博客

https://www.cnblogs.com/dw3306/p/16820044.html

https://blog.csdn.net/m0_37539286/article/details/125058027
————————————————
版权声明:本文为CSDN博主「静水楼台x」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/xiyafei122/article/details/127985176

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值