public class Conv
{
// 每页显示条数
public static int pageSize=10;
//登录后放在Session中的User信息
}
2、Page类,包含了可以存放List的容器
public class Page
{
/**当前页*/
private int curPage;
/**总页数*/
private int total;
/**总的记录数*/
private int sum;
/**结果集*/
private List result;
public Page(){}
public Page(int curPage,int sum) {
this.curPage = curPage;
this.sum = sum;
int totalPage =
(int) Math.ceil(sum/Double.parseDouble(Conv.pageSize + ""));
if (totalPage == 0)
totalPage = 1;
this.total=totalPage;
}
public int getCurPage() {
return curPage;
}
public void setCurPage(int curPage) {
this.curPage = curPage;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public int getSum() {
return sum;
}
public void setSum(int sum) {
this.sum = sum;
}
public List getResult() {
return result;
}
public void setResult(List result) {
this.result = result;
}
}
3、UnitService类获取SqlSession,利用mybatis的RowBounds进行分页,执行sql语句获得结果集
public class UnitService
{
private static UnitService unitService = null;
Logger LOG = Logger.getLogger(DeptService.class);
private UnitService(){}
public static UnitService getInstance(){
if(unitService==null){
unitService=new UnitService();
}
return unitService;
}
public interface IunitDao
{
/**
* 查询单位列表
*/
public List<QyDw> queryQyDw(Map<String,Object> map);
}
5、UnitDao.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace应对应接口路径-->
<mapper namespace="com.xinfeijinxin.qy.dao.IunitDao">
<!--查询记录,map参数可以根据情况去掉-->
<select id="queryQyDw" parameterType="map" resultType="com.xinfeijinxin.qy.bean.QyDw">
select dwid,dwmc,dwjc,sjdw,dwlx from qy_dw where 1=1
<if test="keyword!=null and keyword !=''">
and dwmc like CONCAT('%','${keyword}','%' )
</if>
<if test="keyword!=null and keyword !=''">
and dwjc like CONCAT('%','${keyword}','%' )
</if>
<if test="keyword!=null and keyword !=''">
and sjdw like CONCAT('%','${keyword}','%' )
</if>
<if test="keyword!=null and keyword !=''">
and dwlx like CONCAT('%','${keyword}','%' )
</if>
</select>