1、测试类(Dao接口省略)
@Test
public void test01() {
Student stu = new Student("初九", 29, 89);
Map<String, Object> map = new HashMap<String, Object>();
map.put("nameCon", "张");
map.put("ageCon", 23);
map.put("stu", stu);
List<Student> students = dao.selectByCondition(map);
for (Student student : students) {
System.out.println(student);
}
}
@Test
public void test02() {
List<Student> students = dao.selectByConditionS("张", 23);
for (Student student : students) {
System.out.println(student);
}
}
2、对应的mapper.xml文件
select id="selectByCondition" resultType="Student">
select id,name,age,score
from student
where name like '%' #{nameCon} '%'
and age > #{ageCon}
and score > #{stu.score}
</select>
<select id="selectByConditionS" resultType="Student">
select id,name,age,score
from student
where name like '%' #{arg0} '%'
and age > #{arg1}
</select>
3、 #{}可以放什么内容
1)参数对象的属性
2)随意内容,此时的#{}是个占位符(参数为一个的情况)
3)参数是map对,象时的key
4)参数为map时,若key对应的value为对象,则可以将该对象的属性放入
5)参数的索引号 【JDK8以下低版本的所以是0 1 这样,JDK8新版本是arg0 arg1】