There are some small partners feel that when there are multiple parameters MyBatis only method You need to add @Param notes, in fact, this understanding is not accurate. Even MyBatis a single parameter, it may also be used @Param comment.
However, before you summed up the law, you may feel strange, sometimes an argument obviously do not add @Param notes, sometimes, they need to add, do not add error.
Some people will think this is a pot of various versions of the difference of MyBatis, undeniable, MyBatis has developed rapidly, the differences between the different versions quite obvious, but this did to @Param annotated problem, but not the version of pot ! Today, Song Ge and everyone to chat on this issue, in the end under what circumstances need to add @Param comment.
First, we need to add the following few notes @Param scene, I believe we all have a consensus:
- The first: a plurality of method parameters, annotation requires @Param
Such as the following:
@Mapper
public interface UserMapper {
Integer insert(@Param("username") String username, @Param("address") String address);
}
复制代码
Corresponding XML file 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 @Param need to add annotations scene.
- The second: To alias method parameters, need @Param comment
When it is desired to an alias of the parameters, we also need @Param annotation, such a method is defined as follows:
@Mapper
public interface UserMapper {
User getUserByUsername(@Param("name") String username);
}
复制代码
Corresponding XML is defined as follows:
<select id="getUserByUsername" parameterType="org.javaboy.helloboot.bean.User">
select * from user where username=#{name};
</select>
复制代码
Honestly, not much of this demand, trouble.
- Third: XML in SQL using the $, the argument also needs @Param comment
Symbols, such as the name of the column to pass table name or when, this time must be added @Param notes, for example:
@Mapper
public interface UserMapper {
List<User> getAllUsers(@Param("order_by")String order_by);
}
复制代码
Corresponding XML is defined 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>
复制代码
Front three, very easy to understand, I believe that many small partners also know all that, in addition to these three common scenarios, there is a special scene, has often been ignored.
- Fourth, it is dynamic SQL, if you use parameters in dynamic SQL as a variable, you also need to @Param notes, even if you have only one argument.
If we use in dynamic SQL parameters as judge the conditions, then we must also add @Param notes, for example, the following method:
@Mapper
public interface UserMapper {
List<User> getUserById(@Param("id")Integer id);
}
复制代码
SQL definitions out as follows:
<select id="getUserById" resultType="org.javaboy.helloboot.bean.User">
select * from user
<if test="id!=null">
where id=#{id}
</if>
</select>
复制代码
This case, even if there is only one parameter, also need to add @Param notes, and this situation has often been overlooked!
Well, I do not know if you have to GET it? There are discussion questions please leave a message.
Public concern number [south] a little rain, focused on Spring Boot + Micro service and front and rear ends of separation full stack technology, video tutorials on a regular basis to share concerns reply after Java, Java dry Song Ge receive carefully prepared for you!