Mapper中
public interface StudentMapper {
@Select("select * from students")
List<Students> findAll();
@Insert("insert into students (sid,username,major,dnum) values (#{sid},#{username},#{major},#{dnum})")
void addStudent(Students students);
@Delete("delete from students where id=#{id}")
void delStudentById(Integer id);
@SelectProvider(type = StudentProvider.class, method = "findStudentByManyCondition")
List<Students> findByManyCondition(Students students);
}
对应的Provider类
public class StudentProvider {
public String findStudentByManyCondition(Students students){
StringBuffer sql = new StringBuffer("select * from students where 1=1");
String sid, username, major, dnum;
sid = students.getSid();
username = students.getUsername();
major = students.getMajor();
dnum = students.getDnum();
if(!"".equals(sid)){
sql.append(" and sid='"+sid+ "'");
}
if(!"".equals(username)){
sql.append(" and username='"+username+ "'");
}
if(!"".equals(major)){
sql.append(" and major='"+major+ "'");
}
if(!"".equals(dnum)){
sql.append(" and dnum='"+dnum+ "'");
}
return sql.toString();
}
}
|