编程知识 cdmana.com

There are four usage scenarios of @ param annotation in mybatis, and the last one is often ignored!

Preface

Some of my friends think MyBatis Only when there are multiple parameters in the method , You need to add @Param annotation , In fact, this understanding is not accurate . Even if MyBatis Method has only one parameter , It may also use @Param annotation .

however , Before you sum up the rules , You may find it inexplicable , Sometimes a parameter does not need to be added @Param annotation , sometimes , But need to add , Error will be reported if not added .

Some people think it's MyBatis Different versions of the pot , Undeniable? ,MyBatis It's growing fast , The differences between different versions are quite obvious , But this one doesn't add @Param The problem of annotation , It's not a version of the pot ! Today, brother song is going to talk about this issue with you , In the end, which situations need to add @Param annotation .

First , The following several need to be added @Param Annotated scene , I believe that we all have a consensus :

The first one is : Method has multiple parameters , need @Param annotation

@Mapper

public interface UserMapper {

    Integer insert(@Param("username") String username, @Param("address") String address);

}

Corresponding XML The documents are as follows :

<insert id="insert" parameterType="org.javaboy.helloboot.bean.User">

    insert into user (username,address) values (#{username},#{address});

</insert>

This is the most common need to add @Param Annotated scene .

The second kind : Method parameters should be aliased , need @Param annotation

When you need to give an alias to a parameter , We also need @Param annotation , For example, the method is defined as follows :

@Mapper

public interface UserMapper {

    User getUserByUsername(@Param("name") String username);

}

Corresponding XML The definition is as follows :

<select id="getUserByUsername" parameterType="org.javaboy.helloboot.bean.User">

    select * from user where username=#{name};

</select>

honestly , There's not much demand for that , Trouble .

The third kind of :XML Medium SQL Used $ , So in the parameters, we also need @Param annotation

There will be injection holes , But sometimes you have to use symbols , For example, when you want to pass in a column name or a table name , You have to add @Param annotation , for example :

@Mapper

public interface UserMapper {

    List<User> getAllUsers(@Param("order_by")String order_by);

}

Corresponding XML The 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>

The first three , It's easy to understand , I believe a lot of little friends know , In addition to these three common scenarios , There is also a special scene , Often overlooked .

A fourth , That's dynamic SQL , If it's dynamic SQL Parameters are used as variables in , So it also needs @Param annotation , Even if you only have one parameter .

If we're on the move SQL Used in the Parameters as the judging conditions , Then we must add @Param Annotated , For example, the following methods :

@Mapper

public interface UserMapper {

    List<User> getUserById(@Param("id")Integer id);

}

Defined SQL as follows :

<select id="getUserById" resultType="org.javaboy.helloboot.bean.User">

    select * from user

    <if test="id!=null">

        where id=#{id}

    </if>

</select>

This situation , Even if there is only one parameter , You also need to add @Param annotation , But this kind of situation is often ignored !

Okay , I don't know if you have GET Here we are ? If you have any questions, please leave a message to discuss .

版权声明
本文为[You're crud]所创,转载请带上原文链接,感谢
https://cdmana.com/2020/12/20201224154047523f.html

Scroll to Top