
示例1:查询业务员的联系记录
1.控制器代码(RelationController.java)
//分页列出联系记录
@RequestMapping(value="toPage/customerRecord")
public String listRelationRecord(Map map,String beginTime,String endTime,
String uname,Long curPage){
Map<String,Object> map1 = new HashMap<String, Object>();
if(beginTime!= null && !"".equals(beginTime)){
map1.put("beginTime",beginTime);
}
if(endTime != null && !"".equals(endTime)){
map1.put("endTime",endTime);
}
if(uname != null && !"".equals(uname)){
map1.put("uname","%"+uname+"%");
}
if(curPage == null){
curPage=(long) 1;
}
Page page = relationService.getPageDataRecord(curPage, 4, map1);
map.put("page", page);
return "Relation/ListRelationRecord";
}
2.服务层代码(RelationService.java)
//分页列出联系记录
public Page getPageDataRecord(long curPage , long pageRows ,Map map){
return relationDaoImpl.getPageDataRecord(curPage, pageRows, map);
}
3.模型层代码(RelationDaoImpl.java)
/**
* 分页列出联系记录
* @param curPage 当前页
* @param pageRows 每页的行数
* @param map 传给mybatis的参数Map
* @return
*/
public Page getPageDataRecord(long curPage , long pageRows ,Map map){
//初始化Page
Page page = new Page(); //获取联系记录的总记录数
long count = session.selectOne(Relation.class.getName()+".getCountRecord",map); //获取总页数=(总记录数+每页的行数-1)/每页的行数。如:总记录数为24条,每页的行数为4条,则共有:(24+4-1)/4=6页
long allPage = (count+pageRows -1)/pageRows; //设置当前页
if(curPage < 1)curPage = 1;//如果当前页小于1则设当前页为第1页
if(allPage > 0 && curPage > allPage ) curPage = allPage;//如果当前页大于总页数则设当前页为总页数(即尾页) //将查询的开始记录偏移量、最大查询记录数放到map中
map.put("start", (curPage -1)* pageRows);//查询记录的偏移量=(当前页-1)*每页的行数。如当前页为2,则查询记录的偏移量为(2-1)*4=4
map.put("stop", pageRows);//最大查询数=每页的行数 //获取联系记录数据
List<Relation> dataList = session.selectList(Relation.class.getName()+".getDataListRecord",map);
//设置page的当前页
page.setCurPage(curPage);
//设置page的每页的行数
page.setPageRows(pageRows);
//设置page的总页数
page.setPageNum(allPage);
//设置page的数据
page.setDataList(dataList);
//返回page
return page;
}
4.Mybatis的映射文件(Relation.xml)
4.1 获取联系记录的总记录数。即在getPageDataRecord方法中long count = session.selectOne(Relation.class.getName()+".getCountRecord",map);语句对应的xml文件中的SQL
<!-- 获取联系记录的总记录数 -->
<select id="getCountRecord" resultType="long" parameterType="Map">
select count(*) from user u,relation r,linkman l,customer c where
r.lid=l.lid and r.uid=u.uid and l.cid=c.cid
<if test="beginTime != null">
<![CDATA[AND redate >= #{beginTime}]]>
</if>
<if test="endTime != null">
<![CDATA[AND redate <= #{endTime}]]>
</if>
<if test="uname != null">
and username like #{uname}
</if>
</select>
4.2 获取联系记录数据。即在getPageDataRecord方法中List<Relation> dataList = session.selectList(Relation.class.getName()+".getDataListRecord",map);语句对应的xml文件中的SQL
<!-- 列出联系记录分页 -->
<select id="getDataListRecord" resultMap="listRelation" parameterType="Map">
select reid,r.lid,content,c.cid,cname,redate,nextdate,nextremark,l.lname,r.uid,u.username,
l.phone,c.statues,c.address from user u,relation r,linkman l,customer c where
r.lid=l.lid and r.uid=u.uid and l.cid=c.cid
<if test="beginTime != null">
<![CDATA[AND redate >= #{beginTime}]]>
</if>
<if test="endTime != null">
<![CDATA[AND redate <= #{endTime}]]>
</if>
<if test="uname != null">
and username like #{uname}
</if>
order by redate desc limit #{start},#{stop}
</select>
说明:因为在控制器RelationController.java中有另一个分页方法,所以在模型层代码(RelationDaoImpl.java)中没有调用父类(BaseDaoImpl.java)的分页方法,下面贴一下此控制器中另一个分页方法——分页列出联系计划。
jsp代码:(ListRelationRecord.jsp)
<jsp:include page="../page.jsp">
<jsp:param value="customerRecord" name="url"/>
</jsp:include>
page.jsp
<body>
<c:set var="url" value="${param.url}"></c:set>
<form name="form" action="${url}" method="post">
<input id="currentPage" type="hidden" name="curPage" />
</form>
<div>
<a href="javascript:toPage(1)">首页</a>
<a href="javascript:toPage(${page.curPage-1})">上一页</a>
<a href="javascript:toPage(${page.curPage+1})">下一页</a>
<a href="javascript:toPage(${page.pageNum})">尾页</a>
一共有【${page.pageNum}】页
当前第【${page.curPage}】页
<%-- 当前第
<select onchange="toPage(this.value)">
<c:forEach begin="1" end="${page.pageNum}" var="v">
<option value="${v}"
<c:if test="${v==page.curPage}">
selected="selected"
</c:if>
> ${v}</option>
</c:forEach>
</select> 页 --%>
</div>
<script type="text/javascript">
function toPage(curPage){
//把当前页设置出来
document.getElementById("currentPage").value=curPage;
//提交action
document.forms['form'].submit();
}
</script>
</body>
示例2:分页列出联系计划
1.控制器代码(RelationController.java)
//分页列出联系计划
@RequestMapping(value="toPage/customerPlan")
public String listRelation(Map map,String beginTime,String endTime,
String uname,Long curPage){
Map<String,Object> map1 = new HashMap<String, Object>();
if(beginTime!= null && !"".equals(beginTime)){
map1.put("beginTime",beginTime);
}
if(endTime != null && !"".equals(endTime)){
map1.put("endTime",endTime);
}
if(uname != null && !"".equals(uname)){
map1.put("uname","%"+uname+"%");
}
if(curPage == null){
curPage=(long) 1;
}
Page page = relationService.getRePlanList(curPage, 4, map1);
map.put("page", page);
return "Relation/listRelation";
}
2.服务层代码(RelationService.java)
//分页列出联系计划
public Page getRePlanList(long curPage, long pageRows, Map map){
return relationDaoImpl.getRePlanList(curPage,pageRows,map);
}
3.模型层代码(RelationDaoImpl.java)
@Repository
public class RelationDaoImpl extends BaseDaoImpl<Relation>{
//分页列出联系计划
public Page getRePlanList(long curPage, long pageRows, Map map){
return super.getPageData(curPage, pageRows, map);
} }
4.模型层代码(父类BaseDaoImpl.java)
public class BaseDaoImpl<T> {
/**
* 分页
* @param curPage 当前页
* @param pageRows 每页的条数
* @param map
* @return
*/
public Page getPageData(long curPage , long pageRows ,Map map){
Page page = new Page(); long count = session.selectOne(clazz.getName()+".getCount",map);//获取数据的总条数
long allPage = (count+pageRows -1)/pageRows;//根据每页的行数+总条数获取总页数 //设置当前页
if(curPage < 1)curPage = 1;//如果当前页小于1则设当前页的值为1
if(allPage > 0 && curPage > allPage ) curPage = allPage;//如果当前页大于总页数则设当前页为总页数 map.put("start", (curPage -1)* pageRows);//设置查询的起始值
map.put("stop", pageRows);//设置返回的最大条数——即每页的行数 //获取数据
List<T> dataList = session.selectList(clazz.getName()+".getDataList",map);
page.setCurPage(curPage);//当前页
page.setPageRows(pageRows);//每页的行数
page.setPageNum(allPage);//总页数
page.setDataList(dataList);//数据 return page;
} }
5.Mybatis的映射文件(Relation.xml)
5.1 获取联系计划的总记录数。即在getPageData方法中 long count = session.selectOne(clazz.getName()+".getCount",map);语句对应的xml文件中的SQL
<!-- 获取联系计划的总记录数 -->
<select id="getCount" resultType="long" parameterType="Map">
select count(*) from
linkman l,relation r,customer c,user u where
r.lid=l.lid and r.uid=u.uid and l.cid=c.cid and nextdate>now()
<if test="beginTime != null">
<![CDATA[AND nextdate >= #{beginTime}]]>
</if>
<if test="endTime != null">
<![CDATA[AND nextdate <= #{endTime}]]>
</if>
<if test="uname != null">
and username like #{uname}
</if>
</select>
5.2获取联系计划的数据。即在getPageData方法中 List<T> dataList = session.selectList(clazz.getName()+".getDataList",map);语句对应的xml文件中的SQL
<!-- 获取联系计划分页数据 -->
<select id="getDataList" resultMap="listRelation" parameterType="Map">
Select reid,redate,content,nextdate, nextremark,u.uid,username,
l.lid,l.lname,lsex,l.phone,c.cid,cname,c.statues,c.address from
linkman l,relation r,customer c,user u where
r.lid=l.lid and r.uid=u.uid and l.cid=c.cid and nextdate>now()
<if test="beginTime != null">
<![CDATA[AND nextdate >= #{beginTime}]]>
</if>
<if test="endTime != null">
<![CDATA[AND nextdate <= #{endTime}]]>
</if>
<if test="uname != null">
and username like #{uname}
</if>
order by nextdate desc limit #{start},#{stop}
</select>