MyBatis only needs to add @param annotation if there are multiple parameters in the method. The @param annotation may be used even if the MyBatis method has only one argument.

However, until you figure out what the pattern is, you might be confused. Sometimes a parameter doesn’t need the @param annotation, and sometimes it does.

Some people will feel that this is the different version of MyBatis pot, it is undeniable that MyBatis is developing very fast, the difference between different versions is quite obvious, but this add @param annotation problem, but is not the version of the pot! Today Songo is going to talk to you about this question and when you need to add @param annotations.

First, here are a few scenarios where @param annotations need to be added:

  • First: the method has multiple arguments and requires @param annotations

For example:

@Mapper
public interface UserMapper {
    Integer insert(@Param("username") String username, @Param("address") String address);
}
Copy the code

The corresponding XML file is as follows:

<insert id="insert" parameterType="org.javaboy.helloboot.bean.User">
    insert into user (username,address) values (#{username},#{address});
</insert>
Copy the code

This is the most common scenario that requires an @param annotation.

  • The second way is to alias the method parameter, requiring the @param annotation

We also need @param annotations when we need an alias for a parameter, such as the method definition:

@Mapper
public interface UserMapper {
    User getUserByUsername(@Param("name") String username);
}
Copy the code

The corresponding XML definition is as follows:

<select id="getUserByUsername" parameterType="org.javaboy.helloboot.bean.User">
    select * from user where username=#{name};
</select>
Copy the code

To be honest, this kind of demand is not much trouble.

  • Third, if SQL in XML uses $, then @param annotations are also required in the parameters

Symbols, such as the @param annotation, are passed in as column or table names, for example:

@Mapper
public interface UserMapper {
    List<User> getAllUsers(@Param("order_by")String order_by);
}
Copy the code

The corresponding XML definition is as follows:

<select id="getAllUsers" resultType="org.javaboy.helloboot.bean.User">
    select * from user
    <if test="order_by! =null and order_by! = "">
        order by ${order_by} desc
    </if>
</select>
Copy the code

The first three kinds of, are easy to understand, I believe that many partners also understand, in addition to the three common scenes, there is a special scene, often ignored.

  • The fourth is dynamic SQL. If you use parameters as variables in dynamic SQL, you also need @param annotations, even if you only have one parameter.

If we use a parameter in dynamic SQL, we must annotate @param, such as the following method:

@Mapper
public interface UserMapper {
    List<User> getUserById(@Param("id")Integer id);
}
Copy the code

SQL defined as follows:

<select id="getUserById" resultType="org.javaboy.helloboot.bean.User">
    select * from user
    <if test="id! =null">
        where id=#{id}
    </if>
</select>
Copy the code

This case, even with a single parameter, requires the @param annotation, which is often overlooked!

Ok, I don’t know if you got it? If you have any questions, please leave a comment.

Pay attention to the public account [Jiangnan little Rain], focus on Spring Boot+ micro service and front and back end separation and other full stack technology, regular video tutorial sharing, after attention to reply to Java, get Songko for you carefully prepared Java dry goods!