传递一个参数
Dao 接口中方法的参数只有一个简单类型(java 基本类型和 String),占位符 #{ 任意字符 },和方
法的参数名无关。
接口方法
Student selectStuId(int id)
mapper文件
<select id="selectStuId" resultType="com.hgzy.domian.Student">
select id,name,emial,age from student where id=#{
studentId}
//#{studentId} , studentId 是自定义的变量名称,和方法参数名无关。
</select>
测试方法
@Test
public void testSelectById(){
//一个参数
Student student = studentDao.selectById(1005);
System.out.println("查询 id 是 1005 的学生:"+student);
}
传入多个参数 使用@param
当 Dao 接口方法多个参数,需要通过名称使用参数。在方法形参前面加入@Param(“自定义参数名”),
mapper 文件使用#{自定义参数名}。
例如定义 List selectStudent( @Param(“personName”) String name ) { … }
mapper 文件 select * from student where name = #{ personName}
接口方法
List<Student> selectMultiParam(@Param("personName") String name,
@Param("personAge") int age);
mapper文件
<select id="selectMultiParam" resultType="com.bjpowernode.domain.Student">
select id,name,email,age from student where name=#{
personName} or age
=#