Mastering MyBatis Parameter Passing: From Single Values to Collections
This guide explains every common way to pass parameters in MyBatis—including single values, multiple arguments via indexes, @Param, Map, POJO, List, and array—showing mapper signatures, XML mappings, and sample code to help interview preparation and real‑world development.
Introduction
During a recent interview the author was asked about the different ways MyBatis can receive parameters. To help interviewees and developers alike, the article systematically presents the most common MyBatis parameter‑passing techniques.
Single Parameter
A single argument can be referenced in SQL with #{paramName}. For consistency, use the same name as the method parameter.
UserInfo selectByUserId(String userId); <select id="selectByUserId" resultType="cn.cb.demo.domain.UserInfo">
select * from user_info where user_id=#{userId} and status=1
</select>Multiple Parameters
Using Index (Not Recommended)
Parameters can be accessed by positional placeholders like #{param1}, #{param2}. This approach is discouraged by most coding standards.
UserInfo selectByUserIdAndStatus(String userId, Integer status); <select id="selectByUserIdAndStatus" resultType="cn.cb.demo.domain.UserInfo">
select * from user_info where user_id=#{param1} and status=#{param2}
</select>Using @Param
The @Param annotation assigns explicit keys, allowing clear references in the XML.
UserInfo selectByUserIdAndStatus(@Param("userId") String userId, @Param("status") Integer status); <select id="selectByUserIdAndStatus" resultType="cn.cb.demo.domain.UserInfo">
select * from user_info where user_id=#{userId} and status=#{status}
</select>Using Map
MyBatis converts a Map into key‑value pairs; the keys are used directly in the SQL.
UserInfo selectByUserIdAndStatusMap(Map<String,Object> map); <select id="selectByUserIdAndStatusMap" resultType="cn.cb.demo.domain.UserInfo">
select * from user_info where user_id=#{userId} and status=#{status}
</select> @Test
void contextLoads() {
Map<String,Object> map = new HashMap<>();
map.put("userId", "1222");
map.put("status", 1);
UserInfo userInfo = userMapper.selectByUserIdAndStatusMap(map);
System.out.println(userInfo);
}Using POJO (Recommended)
Encapsulating parameters in a plain Java object (POJO) is the preferred style; each field must have a getter.
UserInfo selectByEntity(UserInfoReq userInfoReq); <select id="selectByEntity" resultType="cn.cb.demo.domain.UserInfo">
select * from user_info where user_id=#{userId} and status=#{status}
</select> @Data
public class UserInfoReq {
private String userId;
private Integer status;
}List Parameter
When a collection is needed for an IN clause, pass a List and iterate with <foreach>.
List<UserInfo> selectList(List<String> userIds); <select id="selectList" resultMap="userResultMap">
select * from user_info where status=1
and user_id in
<foreach collection="list" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</select>Array Parameter
An array works similarly to a list; the same <foreach> construct is used.
List<UserInfo> selectList(String[] userIds); <select id="selectList" resultMap="userResultMap">
select * from user_info where status=1
and user_id in
<foreach collection="array" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</select>Conclusion
All of the presented parameter‑passing methods are frequently encountered in interviews and production code; developers should be familiar with each to choose the most readable and maintainable option for their MyBatis projects.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Code Ape Tech Column
Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
