参考答案
Mapper接口传递多个参数的4种实现方式:
1. 接口中传多个参数,在 xml 中使用 #{param0}、#{param1}…
//方式一 //java System.out.println("------ selectUserByParamIndex ------"); user = userMapper.selectUserByParamIndex(31, "javamianshi1"); System.out.println(user); //xml <select id="selectUserByParamIndex" resultType="javamianshi.po.User"> select * from user where id = #{arg0} and name = #{arg1} </select>
------ selectUserByParamIndex ------ User{id=31, name='javamianshi1', mc='null'}
2. 使用 @param 注解指定名称,在 xml 中使用 #{名称}
//方式二 //java System.out.println("------ selectUserByAnnotation ------"); user = userMapper.selectUserByAnnotation(31, "javamianshi1"); System.out.println(user); //xml <select id="selectUserByAnnotation" resultType="javamianshi.po.User"> select * from user where id = #{id} and name = #{name} </select>
------ selectUserByAnnotation ------ User{id=31, name='javamianshi1', mc='null'}
3. 多个参数封装到 Java bean 中
//方式三 //java System.out.println("------ selectUserByPo ------"); user = userMapper.selectUserByPo(new User(31, "javamianshi1")); System.out.println(user); //xml <select id="selectUserByPo" resultType="constxiong.po.User" parameterType="javamianshi.po.User"> select * from user where id = #{id} and name = #{name} </select>
------ selectUserByPo ------ User{id=31, name='javamianshi1', mc='null'}
4. 多个参数指定 key,put 到 Map 中
//方式四 //java System.out.println("------ selectUserByMap ------"); Map<String, Object> param = new HashMap<>(); param.put("id", 31); param.put("name", "javamianshi1"); user = userMapper.selectUserByMap(param); System.out.println(user); //xml <select id="selectUserByMap" resultType="javamianshi.po.User"> select * from user where id = #{id} and name = #{name} </select>
------ selectUserByMap ------ User{id=31, name='javamianshi1', mc='null'}
以上,是MyBatis面试题【Mapper接口如何传递多个参数】的参考答案。
输出,是最好的学习方法。
欢迎在评论区留下你的问题、笔记或知识点补充~
—end—