在mybatis中传入参数如果含多个,可以选用的方法就是JAVABean和注解
以下就是使用注解的具体实例:
Mapper.java
public interface StudentMapper {
public List<Student> listStudentByIdAndName(@Param("id")Long id,
@Param("name")String name) ; //这样就可以在xml中#{属性名}寻找对应的参数,适合五个以下的传参,超过五个用javabean
public List<Student> listStudentByStudent(@Param("stu")Student stu);
}
Mapper.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.itrucheng.mapper.StudentMapper"> <!-- 接口名要用全名,没有别名 -->
<select id="listStudentByIdAndName" resultType="Student" > <!-- 可以不用提供parameterType, 因为它会自己找到类型 -->
select * from stu where s_id < #{id} and s_name like concat (#{name}, '%');
</select>
<select id="listStudentByStudent" resultType="Student">
select * from stu where s_id < #{stu.s_id} and s_name like concat (#{stu.s_name}, '%');
</select>
</mapper>
注意:
xml中不能写<,>,必须要用响应的转义字符<、>进行转义
一般来说,如果传入的参数超过5个我们就要将响应的参数封装成一个JAVABean,进行传递。
如果没有超过5个,就可以用注解传入多个参数,灵活方便