当使用实体类对象时 需要sql语句中#{...}中的变量名需要与实体类对象中的一致。
当使用map时,需要sql语句中#{...}中的变量名与map中的key相同。
查询多个
@Select("select * from student where sname=#{sname} or sname=#{sname1}")
List<Student> getStudentList2(Map<String,Object> map);
@Test
public void selectStudent(){
SqlSession sqlSession = null;
try {
sqlSession = MybatisUtils.getSqlSession();
StudentDao mapper = sqlSession.getMapper(StudentDao.class);
Map<String, Object> map = new HashMap<String, Object>();
map.put("sname","周梅");
map.put("sname1","李云");
List<Student> students = mapper.getStudentList2(map);
for (Student student : students) {
System.out.println(student);
}
} finally {
if (sqlSession!=null)
sqlSession.close();
}
}
2、在添加等中使用
通过map可以随意制造sql语句,不受到 new一个实体类对象的限制。