MyBatis-多参数传递

多参数传递

Posted by LQFGH on January 22, 2019

为接口方法传递多个参数,该如何在映射文件中获取参数

1
2
3
4
5
6
	 * 根据idusername获取employee
	 * @param id
	 * @param userName
	 * @return
	 */
	Employee selectByIdAndName(Integer id,String lastName);

方法一 0-N

使用 0-N 获取参数

1
2
3
	<select id="selectByIdAndName" resultType="com.mybatis.bean.Employee">
		select * from tbl_employee where id = #{0} and last_name = #{1}
	</select>

方法二 param1-paramN

使用 param1-paramN获取参数

1
2
3
	<select id="selectByIdAndName" resultType="com.mybatis.bean.Employee">
		select * from tbl_employee where id = #{param1} and last_name = #{param2}
	</select>

方法三 具名参数

使用具名参数

1
2
3
4
5
6
7
	/**
	 * 根据id和username获取employee
	 * @param id
	 * @param userName
	 * @return
	 */
	Employee selectByIdAndName(@Param("id") Integer id,@Param("lastName") String lastName);
1
2
3
	<select id="selectByIdAndName" resultType="com.mybatis.bean.Employee">
		select * from tbl_employee where id = #{id} and last_name = #{lastName}
	</select>

方法四 使用pojo

方法五 使用 Map

1
2
3
4
5
6
	/**
	 * 根据id和username获取employee
	 * @param map
	 * @return
	 */
	Employee selectByMap(Map<String, Object> map);
1
2
3
	<select id="selectByMap" resultType="com.mybatis.bean.Employee">
		select * from tbl_employee where id = #{id} and last_name = #{lastName}
	</select>

方法六 定义 DTO 数据传输对象