MyBatis(3.2.3) - Passing multiple input parameters

MyBatis's mapped statements have the parameterType attribute to specify the type of input parameter. If we want to pass multiple input parameters to a mapped statement, we can put all the input parameters in a HashMap and pass it to that mapped statement.

MyBatis provides another way of passing multiple input parameters to a mapped statement. Suppose we want to find students with the given name and email.

public interface StudentMapper {
    List<Student> findAllStudentsByNameEmail(String name, String email);
}

MyBatis supports passing multiple input parameters to a mapped statement and referencing them using the #{param} syntax.

<select id="findAllStudentsByNameEmail" resultMap="StudentResult">
    select stud_id, name,email, phone from Students where name = #{param1} and email = #{param2}
</select>

Here #{param1} refers to the first parameter name and #{param2} refers to the second parameter email.

StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
studentMapper.findAllStudentsByNameEmail(name, email);

 

posted on 2016-03-01 17:00  huey2672  阅读(562)  评论(0编辑  收藏  举报