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语句中的参数。
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: mybatis二级缓存的缺点有哪些