mapper.xml
<select id="selectlike" resultType="com.rj.entity.Student"> select * from student WHERE sname LIKE '${sname}%' </select> <select id="selectwhere" resultType="com.rj.entity.Student"> select * from student <where> <if test="stu.sname !=null and stu.sname !=''"> sname LIKE '${stu.sname}%' </if> <if test="stu.sno !=null and stu.sno !=''"> AND sno LIKE '${stu.sno}%' </if> </where> </select>
dao
public List<Student> selectwhere(@Param("stu") Student stu);
service
public List<Student> selectwhere(Student stu) throws IOException;
serviceimpl
public List<Student> selectwhere(Student stu) throws IOException { SqlSession session = MyBatisUtil.getSqlSession(); //创建dao层对象 代理模式(反射) IStudentDao dao=session.getMapper(IStudentDao.class); //调用dao层提供的方法 List<Student> list =dao.selectwhere(stu); //关闭session MyBatisUtil.close(); return list; }
testmain
//调用service IStudentService service = new IStudentServiceImpl(); //List<Student> students = service.selectAllList(); Student student=new Student(); student.setSname("王"); student.setSno("19"); //模糊查询 // List<Student> students=service.selectlike(student); //多条件查询 List<Student> students=service.selectwhere(student); //打印输出 System.out.println(students);
总是报这个错是怎么会是呢
Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: org.apache.ibatis.builder.BuilderException: Error evaluating expression 'stu.sno !=null and stu.sno !='''. Cause: org.apache.ibatis.ognl.NoSuchPropertyException: com.rj.entity.Student.sno
### Cause: org.apache.ibatis.builder.BuilderException: Error evaluating expression 'stu.sno !=null and stu.sno !='''. Cause: org.apache.ibatis.ognl.NoSuchPropertyException: com.rj.entity.Student.sno