如何正确使用Mybatis的

如何正确使用Mybatis的

本文介绍了如何正确使用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' 的属性的 getter

我可以理解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,则取决于几个条件.

  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 等).

    • 在 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

        • 您可以在 #{} 中使用任何名称引用参数.

        • you can reference the parameter with any name in #{}.

        你必须在${}test属性的中引用参数为_parameter;value 属性.这就是您的第二个示例抛出异常的原因.

        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>
        

        1. ... 没有映射到唯一参数的类型处理程序(即参数是 POJO 或 Map),您可以直接使用它们的名称(如果参数是 Map,则为键).

        1. ... 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="userId != null">and id = #{userId}</if>
            <if test="userName != null">and name = #{userName}</if>
          </where>
        </select>
        

      • 否则,您可以使用 MyBatis 隐式分配的名称来引用参数,即 arg0, arg1, ...(我不建议这样做,因为它是脆弱且容易出错).

      • 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 不计算在内.

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

  • 07-22 09:31