StudentQuery:
package Student; import java.util.ArrayList;
import java.util.List; public class StudentQuery {
private Integer id;
private String no;
private String name;
private String age;
private float score;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public float getScore() {
return score;
}
@Override
public String toString() {
return "StudentQuery [id=" + id + ", no=" + no + ", name=" + name
+ ", age=" + age + ", score=" + score + ", fields=" + fields
+ "]";
}
public void setScore(float score) {
this.score = score;
} private String fields;
public String getFields() {
return fields;
}
public void setFields(String fields) {
this.fields = fields;
} public class FieldOrder{
private String field; //id , name imgUrl
private String order; // desc asc public FieldOrder(String field, String order) {
super();
this.field = field;
this.order = order;
}
public String getField() {
return field;
}
public void setField(String field) {
this.field = field;
}
public String getOrder() {
return order;
}
public void setOrder(String order) {
this.order = order;
}
} //orderby 集合
private List<FieldOrder> fieldOrders = new ArrayList<FieldOrder>(); //按照Id排序
public void orderbyId(boolean isAsc){
fieldOrders.add(new FieldOrder("id",isAsc == true ? "asc" : "desc"));
} private Integer pageNo = 1;
private Integer startRow;
private Integer pageSize = 10;
public Integer getPageNo() {
return pageNo;
}
public void setPageNo(Integer pageNo) {
this.startRow = (pageNo - 1) * pageSize;
this.pageNo = pageNo;
}
public Integer getStartRow() {
return startRow;
}
public void setStartRow(Integer startRow) {
this.startRow = startRow;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.startRow = (pageNo - 1) * pageSize;
this.pageSize = pageSize;
}
}
mapper语句
<sql id="studentSelector">
select
<if test="fields != null">
${fields}
</if>
<if test="fields == null">
id, no, name, age, score
</if>
from student
</sql> <sql id="studentOrderBy">
<if test="fieldOrders != null and fieldOrders.size > 0">
order by
<foreach collection="fieldOrders" item="fieldOrder" separator=",">
${fieldOrder.field} ${fieldOrder.order}
</foreach>
</if>
</sql> <sql id="studentLimit">
<if test="startRow != null">
limit ${startRow}, ${pageSize}
</if>
</sql> <select id="getStudentList" parameterType="StudentQuery" resultMap="BaseResultMap">
<include refid="studentSelector"></include>
<include refid="studentOrderBy"></include>
<include refid="studentLimit"></include>
</select>
test
public class Test {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-dao.xml");
StudentMapper studentMapper = (StudentMapper) ctx.getBean("studentMapper");
StudentQuery studentQuery = new StudentQuery();
studentQuery.setFields("id, name");
studentQuery.orderbyId(false);
studentQuery.setPageNo(2);
studentQuery.setPageSize(2);
List<Student> students = studentMapper.getStudentList(studentQuery);
for(Student s : students){
System.out.println(s);
}
}
}