示例【根据姓名和班级查询学生信息】
配置StudentMapper.xml
<select id="getStudentByNameAndClasses" resultType="com.po.Student">
select * from t_student
<where>
<if test="sname!=null">
and sname like concat('%',#{sname},'%')
</if>
<if test="classes!=null">
and classes=#{classes}
</if>
</where>
</select>
创建StudentDao
@Test
public void getStudentByNameAndClasses(){
Student stu=new Student();
stu.setSname("张三");
stu.setClasses("大一");
students=session.selectList("com.mapper.StudentMapper.getStudentByNameAndClasses", stu);
for (Student student : students) {
System.out.println(student);
}
}
传递姓名和班级参数
传递班级参数
姓名和班级都为空