ibatis分页(以mysql为例)

时间:2022-04-19 20:09:01

 

Users.xml

<typeAlias alias="Student" type="com.entity.Student"/>

<select id="pageStudents" parameterClass="java.util.Map" resultClass="Student">
      select * from student
      <dynamic prepend="WHERE">
          <isGreaterThan prepend="AND" property="age" compareValue="0">
               age > #age#
          </isGreaterThan>
      </dynamic> 
       order by id limit #begin#,#pageSize#
</select>

 

 

Test.java

public void pageStudents() throws SQLException
{

  List<Student> lstStu = new ArrayList<Student>();
  // 显示数量
  int pageSize = 1;

  // 当前页数
  int currentPage = 0;

  // 起始条数

  int begin = 0;

  // 筛选条件
  int age = 20;
  pageSize = 8;
  currentPage = 1;
  begin = currentPage > 1 ? pageSize * (currentPage - 1) : 0;
  param =
new HashMap<String, Integer>();
  param.put("begin", begin);
  param.put("pageSize", pageSize);
  param.put("age", age);
  lstStu = sqlMap.queryForList("pageStudents", param);
  for (Student s: lstStu)
  {   

       System.out.print(s); 

  }

}

 

这只是一个测试实例,根据这个实例结合J2EE的分页思想的做法,应该很容易集成到项目里。