How to implement multiple parameters of mybatis

Time:2021-7-29

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:

//Modify the user name according to the incoming user name and primary key 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:

//Error instance:
<update>
 update user set username = #{name1} where id = #{oid}
</update>

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

//Error message
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, ID, Param1 and param2. Then we can follow the instructions of the console.

① According to the specified parameter name


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

② In order of parameters


<update>
  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:

//Modify the user name according to the incoming user name and primary key 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:

//Error instance:
<update>
 update user set username = #{ddd} where id = #{fff}
</update>

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

//Error message:
Cause: org.apache.ibatis.binding.BindingException: Parameter 'ddd' not found. 
Available parameters are [arg1, arg0, param1, param2]

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

① In order of parameters


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

② In order of parameters


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

summary

  • Using @ param annotation, we can obtain parameter values according to user-defined names in SQL statements, or in the order of Param1, param2.
  • Directly pass in the parameters, and obtain the corresponding parameter values in the order of arg1, arg2,… In the SQL statement according to the order of parameter transmission.
  • Directly pass in parameters. In the SQL statement, the parameter values are obtained in the order of Param1, param2,… According to the order of parameter transmission.

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 be helpful to your study, and I hope you can support developpaer.

Recommended Today

Implementation example of go operation etcd

etcdIt is an open-source, distributed key value pair data storage system, which provides shared configuration, service registration and discovery. This paper mainly introduces the installation and use of etcd. Etcdetcd introduction etcdIt is an open source and highly available distributed key value storage system developed with go language, which can be used to configure sharing […]