Passing Parameters in MyBatis
MyBatis provides multiple ways to pass parameters from Java code to XML mapper files. This article explores several common techniques used in parameter handling.
Single Simple Parameter
When a DAO interface method has a single simple paraemter (such as a primitive type or String), you can use any placeholder name in the XML file. The name of the method parameter is not importatn.
Mapper XML File - StudentDao.xml
<mapper namespace="com.example.dao.StudentDao">
<select id="selectStudentById" resultType="com.example.domain.Student">
SELECT id, name, email, age FROM student WHERE id = #{studentId}
</select>
</mapper>
DAO Interface - StudentDao
public interface StudentDao {
Student selectStudentById(Integer id);
}
Test Code
@Test
public void testSelectStudentById() {
SqlSession session = MyBatisUtil.getSqlSession();
StudentDao dao = session.getMapper(StudentDao.class);
Student student = dao.selectStudentById(1002);
System.out.println(student);
}
Using parameterType Attribute
The parameterType attribute can specify the type of the parameter passed to the SQL statement. However, it's optional because MyBatis can infer the type automatically via reflection.
Example with parameterType
<select id="selectStudentById" parameterType="int" resultType="com.example.domain.Student">
SELECT id, name, email, age FROM student WHERE id = #{studentId}
</select>
Multiple Parameters Using @Param Annotation
For methods with multiple parameters, you can use the @Param annotation to assign names to each parameter, which can then be referenced in the XML file.
Mapper XML File
<select id="selectMultipleParams" resultType="com.example.domain.Student">
SELECT id, name, email, age FROM student WHERE name = #{myName} OR age = #{myAge}
</select>
DAO Interface
public List<Student> selectMultipleParams(@Param("myName") String name, @Param("myAge") Integer age);
Test Code
@Test
public void testMultipleParams() {
SqlSession session = MyBatisUtil.getSqlSession();
StudentDao dao = session.getMapper(StudentDao.class);
List<Student> students = dao.selectMultipleParams("John", 23);
students.forEach(System.out::println);
}
Multiple Parameters Using Java Objects
You can also pass a Java object as a parameter. The properties of this object will be used as SQL parameter values.
Parameter Object Class
public class QueryParams {
private String queryName;
private Integer queryAge;
// getters and setters
}
Mapper XML File
<select id="selectByObject" resultType="com.example.domain.Student">
SELECT id, name, email, age FROM student WHERE name = #{queryName} OR age = #{queryAge}
</select>
DAO Interface
List<Student> selectByObject(QueryParams params);
Test Code
@Test
public void testSelectByObject() {
SqlSession session = MyBatisUtil.getSqlSession();
StudentDao dao = session.getMapper(StudentDao.class);
QueryParams params = new QueryParams();
params.setQueryName("Alice");
params.setQueryAge(25);
List<Student> students = dao.selectByObject(params);
students.forEach(System.out::println);
}
Passing Parameters by Position (Not Recommended)
You can also reference parameters by their position in the method signature using #{arg0}, #{arg1}, etc. However, this approach is not recommended as it can make the code harder to maintain.
Passing Parameters Using Map (Not Recommended)
Another option is to use a Map object to pass multiple parameters. The keys of the map are used in the XML file to reference the values.