在MyBatis中,可以通过传入一个Map或者使用@Param注解来传递多个参数。
使用Map传递多个参数示例如下:
// 在mapper接口中定义方法List<User> getUserListByParams(Map<String, Object> params);// 在mapper.xml文件中使用参数<select id="getUserListByParams" resultMap="userResultMap"> SELECT * FROM user WHERE name = #{name} AND age = #{age}</select>// 在调用方法时传入参数Map<String, Object> params = new HashMap<>();params.put("name", "Tom");params.put("age", 20);List<User> userList = userDao.getUserListByParams(params);使用@Param注解传递多个参数示例如下:
// 在mapper接口中定义方法List<User> getUserListByParams(@Param("name") String name, @Param("age") int age);// 在mapper.xml文件中使用参数<select id="getUserListByParams" resultMap="userResultMap"> SELECT * FROM user WHERE name = #{name} AND age = #{age}</select>// 在调用方法时传入参数List<User> userList = userDao.getUserListByParams("Tom", 20);通过以上两种方式,就可以在MyBatis中传递多个参数。


