Precautions for @Param annotation (reproduced)

1 Overview

First of all, it is clear that this annotation is for the parameter assignment in the SQL statement .

The role of @Param is to name the parameters, such as a method A (int id) in the mapper, when adding annotations A (@Param(“userId”) int id), that is to say, the outside wants to take out the incoming id value , you only need to take its parameter name userId. Pass the parameter value as in the SQL statement, and use #{userId} to assign the value to the SQL parameter.

2. Examples:

Example 1: @Param annotates parameters of basic types

Method in mapper:

public User selectUser(@Param("userName") String name,@Param("password") String pwd);

Maps to tags in xml

<select id="selectUser" resultMap="User">  
   select * from user  where user_name = #{userName} and user_password=#{password}  
</select>

The userName and password in where user_name = #{userName} and user_password = #{password} are taken from the annotation @Param(), and the taken values ​​are the values ​​of the formal parameters String name and String pwd in the method.

Example 2: @Param annotation JavaBean object

The SQL statement extracts the properties in the object through the alias in the @Param annotation and then copies them

Method in mapper:

public List<User> getAllUser(@Param("user") User u);

Maps to tags in xml

<select id="getAllUser" parameterType="com.vo.User" resultMap="userMapper">  
        select   
        from user t where 1=1  
             and   t.user_name = #{user.userName}  
              and   t.user_age = #{user.userAge}  
    </select>  

3. Precautions

  • When the @Param annotation is used to declare parameters, the value of the SQL statement can use #{} and ${}.

  • When you do not use the @Param annotation to declare parameters, you must use #{} to get the parameters. Using ${} to get the value will result in an error.

  • When the @Param annotation is not used, there can only be one parameter, and it is a Javabean. JavaBean properties can be referenced in SQL statements, and only JavaBean properties can be referenced.

@Select("SELECT * from Table where id = #{id}")
Enchashment selectUserById(User user);

————————————————
Copyright statement: This article is the original article of CSDN blogger "Xiaofengwanyueyiwangguanhexiaosuo", following the CC 4.0 BY-SA copyright agreement, please attach the reprint Link to the original source and this statement.
Original link: https://blog.csdn.net/Sunshineoe/article/details/114697944

4. Supplement

The meaning of the third point above has two meanings:

  1. If there is no @param on the javaBean, if there are multiple parameters in the mapper, an error will be reported;
  2. If there is no @param on the javaBean, there is no need to use a prefix when taking the value in xml, it is directly #{userName} instead of #{user.userName}
int addUser(User user); //这里不带上@Param("user")
<insert id="addUser" useGeneratedKeys="true" keyProperty="uid" keyColumn="uid">
    insert into users (username, password) values (#{username}, #{password})
</insert>

Guess you like

Origin blog.csdn.net/qq_43575801/article/details/128845777