DAO文件在MyBatis进行操作:
- 如果操作方法只有一个简单类型或者字符串类型的参数,在Mapper配置中可以直接通过#{key}直接获取
- 如果操作方法有一个对象类型的参数,在Mapper配置中可以直接通过#{attrName}获取对象的指定属性值(attrName必须是参数对象的属性)
- 如果操作方法有一个Map类型的参数,在Mapper配置中可以直接通过#{key}获取key对应的value
- 如果操作方法有多个参数:
- 在mapper里使用特殊参数(arg0,arg1,arg2…)或(param1,param2,param3…),需要按顺序
- 使用@Param(“start”)定义别名,在mapper中使用别名获取指定参数
Student queryStudentById(String stuNum); int insertStudent(Student student); Listmapper文件queryStudentListByPage(HashMap map); List queryStudentListByPage(int start, int length); List queryStudentListByPage(@Param("sta") int start, @Param("len") int length);
mapper.xml 标签使用
测试类select from students where stu_num=#{stuNum} insert into students(stu_num, stu_name, stu_sex, stu_age) values(#{stuNum}, #{stuName}, #{stuSex}, #{stuAge})
// Map类型的传参
public void queryStudentListByMap() {
StudentDao studentDao = MyBatisUtil.getMapper(StudentDao.class);
HashMap map = new HashMap();
map.put("start", 0);
map.put("length", 2);
List students = studentDao.queryStudentListByPage(map);
}
// 多个参数
public void queryStudentListByPage() {
StudentDao studentDao = MyBatisUtil.getMapper(StudentDao.class);
List students = studentDao.queryStudentListByPage(1, 2);
}



