1. 程式人生 > 其它 >Four usage scenarios of @ param annotation in mybatis, the last one is often ignored!

Four usage scenarios of @ param annotation in mybatis, the last one is often ignored!

Some partners think that only when there are multiple parameters inmybatismethod can @ param annotation be added. In fact, this understanding is not accurate. Even if the mybatis method has only one parameter, the @ param annotation may be used.

However, before you summarize the rules, you may feel puzzled. Sometimes a parameter does not need to be annotated with @ param. Sometimes, it needs to be added, and if not, an error will be reported.

Some people will think that this is the pot of different versions of mybatis. It is undeniable that mybatis is developing rapidly and the differences between different versions are quite obvious. However, this problem without @ param annotation is not the pot of versions! Today, brother song is going to talk to you about this problem. In the end, you need to add @ param annotation.

First of all, the following scenarios need to be annotated with @ param. I’m sure everyone has reached a consensus:

  • First, the method has multiple parameters and needs @ param annotation
    @Mapper
    public interface UserMapper {
        Integer insert(@Param("username") String username, @Param("address") String address);
    }

    The corresponding XML file is as follows:

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

    This is the most common scenario where @ param annotation needs to be added.

  • Second, to get alias for method parameters, @ param annotation is required

   When we need to take an alias for a parameter, we also need @ param annotation. For example, the method definition is as follows:

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

  The corresponding XML definition is as follows:

<select id="getUserByUsername" parameterType="org.hello.bean.User">
    select * from user where username=#{name};
</select>

To be honest, this kind of demand is not much, it’s a lot of trouble.

  • Third, SQL in XML uses $, so @ param annotation is also needed in parameters

There will be injection vulnerability in $, but sometimes you have to use the $symbol, for example, to pass in the column name or table name, you must add @ param annotation at this time, for example:

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

The corresponding XML definition is as follows:

<select id="getUserByUsername" parameterType="org.hello.bean.User">
    select * from user where username=#{name};
</select>

The above three kinds are easy to understand. I believe that many little friends also understand them. In addition to these three common scenes, there is a special scene that is often ignored.

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

If we use parameters as judgment conditions in dynamic SQL, we must add @ param annotation, for example, the following methods:

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

The defined SQL is as follows:

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

In this case, even if there is only one parameter, you need to add @ param annotation, which is often ignored!