How to handle multiple parameters as inputs in MyBatis?
In MyBatis, you can use the @Param annotation to handle multiple input parameters. Here are some ways to handle multiple input parameters:
- What is the value of parameter ‘@Param’?
@Select("SELECT * FROM users WHERE username = #{username} AND age = #{age}")
User getUserByNameAndAge(@Param("username") String username, @Param("age") int age);
- Chart
- a data structure called a HashMap
@Select("SELECT * FROM users WHERE username = #{username} AND age = #{age}")
User getUserByMap(Map<String, Object> params);
When calling, you can use params.put(“username”, username) and params.put(“age”, age) to set the parameters.
- Parameter
- A visual representation of an area or location
@Select("SELECT * FROM users WHERE username = #{params.username} AND age = #{params.age}")
User getUserByMapWithParamAnnotation(@Param("params") Map<String, Object> params);
When making the call, you can use params.put(“username”, username) and params.put(“age”, age) to set the parameters.
Parameters can be referenced in SQL statements using either ${} or #{}, for example, SELECT * FROM users WHERE username = #{username}.