<select id="selectStudentsByWhere" resultType="com.bjpowernode.domain.Student"> select id,name,email,age from student <where> <if test="name!=null and name!=''"> name like #{name} </if> <if test="age>0"> or age < #{age} </if> </where> </select>
public class TestMybatis { @Test public void testselectStudentsByWhere(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); StudentDao dao = sqlSession.getMapper(StudentDao.class); Student student = new Student(); student.setName("%王%"); student.setAge(21); List<Student> list = dao.selectStudentsByWhere(student); for(Student student1:list){ System.out.println(student1); } } }