如何正确使用Mybatis的@Param批注 [英] How to properly use the @Param annotation of Mybatis

查看:463
本文介绍了如何正确使用Mybatis的@Param批注的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

起初我没有使用@Param注释,这是我的mapper.java

I didn't use @Param annotation at first, this is my mapper.java

public void changeUserAuth(Integer userId,int identity);

,这是我的mapper.xml

, and this is my mapper.xml

<update id="changeUserAuth">
    update user
    <set>
        <if test="identity != 0">identity = #{identity}</if>
    </set>
    <where>
        <if test="userId != 0">userId = #{userId}</if>
    </where>
</update>

然后它可以正常工作!我继续这样写,如下:

then it works correctly!I continue to write like this, as follows:

//this's mapper.java
public void updateUserStatus(Integer userId);

<!--this is mapper.xml>
<update id="changeUserAuth">
    update user
    set deleteFlag= true
    <where>
        <if test="userId != 0">userId = #{userId}</if>
    </where>
</update>

但是,它给出了一个错误,消息是

however,it gave an error,the message is

"class.java.lang.Integer"中没有名为"userId"的属性的吸气剂.

There is no getter for property named 'userId' in 'class.java.lang.Integer'

我可以理解mybatis无法解析Integer,但是为什么它不是像我第一次使用时那样的错误,仅仅是因为我有一个int类型的Parameter?在第二种方法中,我必须使用@Param注释

I can understand that mybatis cannot parse the Integer, but why it is not an error like my first use, just because I have an int type Parameter? In the second method, I have to use @Param annotation

推荐答案

以下是在MyBatis语句中引用参数的方式.

Here is how you reference parameters in MyBatis statements.

在下面的说明中,我将使用此POJO.

I'll use this POJO in the following explanation.

public class User {
  private Integer id;
  private String name;
  //...
}


使用@Param

如果在参数上添加@Param批注,则可以使用指定的名称来引用该参数.这是最简单的情况.


When @Param is used

If you add @Param annotation on a parameter, you can use the specified name to reference the parameter. This is the simplest case.

一些例子:

List<USer> select(@Param("id") Integer userId, @Param("name") String userName);

void insert(@Param("record") User user);

<select id="select" resultType="User">
  select * from users
  <where>
    <if test="id != null">and id = #{id}</if>
    <if test="name != null">and name = #{name}</if>
  </where>
</select>

<insert id="insert">
  insert into users (id, name) values
    (#{record.id}, #{record.name})
</insert>

没有@Param

如果没有@Param,则取决于几个条件.

Without @Param

If there is no @Param, it depends on several conditions.

  1. ...唯一的参数可分配给java.util.List,您可以将参数引用为list.

  1. ... the sole parameter is assignable to java.util.List, you can reference the parameter as list.

List<User> selectByIds(List<Integer> ids);

<select id="select" resultType="User">
  select * from users
  where id in (
    <foreach item="x" collection="list" separator=",">
      #{x}
    </foreach>
  )
</select>

  • ...唯一的参数可分配给java.util.Collection,您可以将参数引用为collection.

  • ... the sole parameter is assignable to java.util.Collection, you can reference the parameter as collection.

    List<User> selectByIds(Set<Integer> ids);
    

    <select id="select" resultType="User">
      select * from users
      where id in (
        <foreach item="x" collection="collection" separator=",">
          #{x}
        </foreach>
      )
    </select>
    

  • ...有一个映射到唯一参数的类型处理程序(即参数为StringInteger等).

  • ... there is a type handler mapped to the sole parameter (i.e. the parameter is String, Integer, etc.).

    • 在MyBatis 3.5.2和更高版本中,您可以使用任何名称引用该参数(不过,出于明显的原因,您应该使用明智的名称).例如

    • With MyBatis 3.5.2 and later, you can reference the parameter using any name (you should use sensible names for obvious reasons, though). e.g.

    List<User> select(Integer id);
    

    <select id="select" resultType="User">
      select * from users
      <where>
        <if test="x != null">and id = #{y}</if>
      </where>
    </select>
    

  • 使用MyBatis 3.5.1

  • With MyBatis 3.5.1

    • 您可以在#{}中使用任何名称引用该参数.
    • 必须在${}<if /><when />test属性以及<bind />value属性中将参数引用为_parameter.这就是第二个示例引发异常的原因.
    • you can reference the parameter with any name in #{}.
    • you must reference the parameter as _parameter in ${}, test attribute of <if /> and <when /> and value attribute of <bind />. This is why your second example throws exception.

    List<User> select(Integer id);
    

    <select id="select" resultType="User">
      select * from users
      <where>
        <if test="_parameter != null">and id = #{z}</if>
      </where>
    </select>
    

  • ...没有映射到唯一参数的类型处理程序(即,参数为POJO或Map<String, ?>),您可以直接使用其名称(或如果参数为a的键)引用参数属性Map).

    ... there is no type handler mapped to the sole parameter (i.e. the parameter is POJO or Map<String, ?>), you can reference the parameter properties directly with their names (or the keys if the parameter is a Map).

    void insert(User user);
    

    <insert id="insert">
      insert into users (id, name) values
        (#{id}, #{name})
    </insert>
    

    当mapper方法采用多个参数时

    1. 如果项目是使用'-parameters'编译的

    1. If the project is compiled with '-parameters' compiler option, you can reference the parameters using their names declared in the method signature. This is your first example.

    List<USer> select(Integer userId, String userName);
    

    <select id="select" resultType="User">
      select * from users
      <where>
        <if test="id != null">and id = #{id}</if>
        <if test="name != null">and name = #{name}</if>
      </where>
    </select>
    

  • 否则,您可以使用MyBatis隐式指定的名称来引用参数,即arg0arg1,...(我不建议这样做,因为它易碎且容易出错).

  • Otherwise, you can reference the parameters using the names implicitly assigned by MyBatis i.e. arg0, arg1, ... (I would not recommend this as it's fragile and error-prone).

    List<USer> select(Integer userId, String userName);
    

    <select id="select" resultType="User">
      select * from users
      <where>
        <if test="arg0 != null">and id = #{arg0}</if>
        <if test="arg1 != null">and name = #{arg1}</if>
      </where>
    </select>
    


  • [1] RowBoundsResultHandler不计算在内.


    [1] RowBounds and ResultHandler does not count.

    这篇关于如何正确使用Mybatis的@Param批注的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆