How does mybatis pass in the implementation code of multiple parameters

The first method: use @ param annotation

The usage of this method is that when we write method parameters in the interface, we can add a @ param annotation in front of each parameter.

The annotation has a value attribute. We can give a name to the annotated parameter. In the SQL statement, we can obtain the parameter value through this name.

Since multiple parameters are passed in, the input parameter parametertype of the mapping file does not need to be written.

If our method in the interface is as follows:

//根据传入的用户名和主键id去修改用户名
int updateUserByManyParam(@Param("name")String username,@Param("id")Integer id);

Let's write in the mapping file to see what errors the console will report:

//错误实例:
<update id="updateUserByManyParam">
 update user set username = #{name1} where id = #{oid}
</update>

OK, let's see what's wrong on the console:

//错误信息
Cause: org.apache.ibatis.binding.BindingException: Parameter 'name1' not found.
Available parameters are [name,id,param1,param2]

We can see from the error message that the name1 parameter cannot be found. The existing parameters are name and param2. Then we can follow the console.

① According to the specified parameter name

<update id="updateUserByManyParam">
 update user set username = #{name} where id = #{id}
</update>

② In order of parameters

<update id="updateUserByManyParam">
  update user set username = #{param1} where id = #{param2}
</update>

The second method: according to the writing order of parameters

In this way, we don't have to do anything when passing in parameters, just get the parameter values in the SQL statement according to the rules.

If our method in the interface is as follows:

//根据传入的用户名和主键id去修改用户名
int updateUserByManyParam(@Param("name")String username,@Param("id")Integer id);

Like the above, let's write in the mapping file to see what errors the console will report:

//错误实例:
<update id="updateUserByManyParam">
 update user set username = #{ddd} where id = #{fff}
</update>

OK, let's see what's wrong on the console:

//错误信息:
Cause: org.apache.ibatis.binding.BindingException: Parameter 'ddd' not found.
Available parameters are [arg1,arg0,param2]

We can see from the error message that the DDD parameter cannot be found. The existing parameters are arg1 and param2. Then we can follow the console.

① In order of parameters

<update id="updateUserByManyParam">
  update user set username = #{arg0} where id = #{arg1}
</update>

② In order of parameters

<update id="updateUserByManyParam">
  update user set username = #{param1} where id = #{param2}
</update>

summary

Note: the number after param starts from 1, while the number after Arg starts from 0.

The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>