MyBatis可以通过两种方式接收多个参数:
public interface MyMapper {
List<User> getUsers(@Param("name") String name, @Param("age") int age);
}
<select id="getUsers" resultType="User">
SELECT * FROM users
WHERE name = #{name}
AND age = #{age}
</select>
public interface MyMapper {
List<User> getUsers(Map<String, Object> params);
}
<select id="getUsers" resultType="User">
SELECT * FROM users
WHERE name = #{name}
AND age = #{age}
</select>
使用示例:
Map<String, Object> params = new HashMap<>();
params.put("name", "John");
params.put("age", 30);
List<User> users = myMapper.getUsers(params);
或者:
public class UserParams {
private String name;
private int age;
// getters and setters
}
UserParams params = new UserParams();
params.setName("John");
params.setAge(30);
List<User> users = myMapper.getUsers(params);
无论使用哪种方式,MyBatis都会根据参数的名称或者属性名来匹配SQL语句中的参数。