javaee导测(ssh框架)分页 mysql数据库(带条件查询)

来源:互联网 发布:linux 输出重定向 2 1 编辑:程序博客网 时间:2024/05/18 02:12

原理图


jsp原理图


一,工具类 com.itheima.crm.page

PageBean.java + PageHibernateCallback.java

PageBean.java
package com.itheima.crm.page;import java.util.List;public class PageBean<T> {//必选项private int pageNum;//第几页(当前页)private int pageSize;//每页显示个数(固定值)private int totalRecord;//总记录数(查询数据库)--注意条件//计算项private int startIndex;//开始索引(计算)private int totalPage;//总分页数(计算)//数据private List<T> data;//分页数据//动态显示条private int start ;private int end ;public PageBean(int pageNum, int pageSize, int totalRecord) {super();this.pageNum = pageNum;this.pageSize = pageSize;this.totalRecord = totalRecord;// 计算//1 开始索引this.startIndex = (this.pageNum - 1) * this.pageSize;//2 总分页数this.totalPage = (this.totalRecord + this.pageSize - 1) / this.pageSize;//3 动态显示条//3.1 初始化数据 -- 显示10个分页this.start = 1;this.end = 10;//3.2 初始数据  , totalPage = 4if(this.totalPage <= 10){this.end = this.totalPage;} else {// totalPage =  22 //3.3 当前页 前4后5this.start = this.pageNum - 4;this.end = this.pageNum + 5;// *  pageNum = 1if(this.start < 1){this.start = 1;this.end = 10;}// * pageNum = 22if(this.end > this.totalPage){this.end = this.totalPage;this.start = this.totalPage - 9;}}}public int getPageNum() {return pageNum;}public void setPageNum(int pageNum) {this.pageNum = pageNum;}public int getPageSize() {return pageSize;}public void setPageSize(int pageSize) {this.pageSize = pageSize;}public int getTotalRecord() {return totalRecord;}public void setTotalRecord(int totalRecord) {this.totalRecord = totalRecord;}public int getStartIndex() {return startIndex;}public void setStartIndex(int startIndex) {this.startIndex = startIndex;}public int getTotalPage() {return totalPage;}public void setTotalPage(int totalPage) {this.totalPage = totalPage;}public List<T> getData() {return data;}public void setData(List<T> data) {this.data = data;}public int getStart() {return start;}public void setStart(int start) {this.start = start;}public int getEnd() {return end;}public void setEnd(int end) {this.end = end;}}

package com.itheima.crm.page;import java.sql.SQLException;import java.util.List;import org.hibernate.HibernateException;import org.hibernate.Query;import org.hibernate.Session;import org.springframework.orm.hibernate3.HibernateCallback;public class PageHibernateCallback<T> implements HibernateCallback<List<T>> {private String hql;private Object[] params;private int startIndex;private int pageSize;public PageHibernateCallback<T> setHql(String hql) {this.hql = hql;return this;}public  PageHibernateCallback<T> setParams(Object[] params) {this.params = params;return this;}public  PageHibernateCallback<T> setStartIndex(int startIndex) {this.startIndex = startIndex;return this;}public  PageHibernateCallback<T> setPageSize(int pageSize) {this.pageSize = pageSize;return this;}public List<T> doInHibernate(Session session) throws HibernateException, SQLException {//1 通过hql语句,获得Query对象Query queryObject = session.createQuery(hql);//2 条件设置for (int i = 0; i < params.length; i++) {queryObject.setParameter(i, params[i]);}//3 分页queryObject.setFirstResult(startIndex);queryObject.setMaxResults(pageSize);//4 查询所有return queryObject.list();}}


二, dao层 接口 com.itheima.crm.coursetype.dao

/** * 分页,查询总记录数 *  * @param condition * @param params * @return */public int getTotalRecord(String condition, Object[] params);/** * 分页,查询结果 *  * @param condition *            条件 * @param params *            条件实际参数 * @param startIndex *            开始索引 * @param pageSize *            每页显示个数 * @return */public List<CrmCourseType> findAll(String condition, Object[] params,int startIndex, int pageSize);

,dao层实现类

com.itheima.crm.coursetype.dao.impl

public int getTotalRecord(String condition, Object[] params) {String hql = "select count(c) from CrmCourseType c where 1=1 "+ condition;List<Long> list = this.getHibernateTemplate().find(hql, params);return list.get(0).intValue();}public List<CrmCourseType> findAll(String condition, Object[] params,int startIndex, int pageSize) {String hql = "from CrmCourseType where 1=1 " + condition;return this.getHibernateTemplate().execute(new PageHibernateCallback<CrmCourseType>().setHql(hql).setParams(params).setPageSize(pageSize).setStartIndex(startIndex));}

四,service层接口

com.itheima.crm.coursetype.service

/** * 分页 + 条件查询 *  * @param courseType *            条件 * @param pageNum *            当前页 * @param pageSize *            每页显示个数 * @return */public PageBean<CrmCourseType> findAll(CrmCourseType courseType,int pageNum, int pageSize);

五,service层实现类

com.itheima.crm.coursetype.service.impl

public PageBean<CrmCourseType> findAll(CrmCourseType courseType,int pageNum, int pageSize) {// 1 条件查询// 1.1 拼凑查询条件StringBuilder builder = new StringBuilder();// 1.2 拼凑实际参数 , 可以重复,顺序 --> ListList<Object> paramsList = new ArrayList<Object>();// 2 过滤条件// 2.1 课程类别if (StringUtils.isNotBlank(courseType.getCourseName())) {builder.append(" and courseName like ?");paramsList.add("%" + courseType.getCourseName() + "%");}// 2.2 课程简介if (StringUtils.isNotBlank(courseType.getRemark())) {builder.append(" and remark like ?");paramsList.add("%" + courseType.getRemark() + "%");}// 2.3 总学时:if (StringUtils.isNotBlank(courseType.getTotalStart())) {builder.append(" and total >= ?");paramsList.add(Integer.parseInt(courseType.getTotalStart()));}if (StringUtils.isNotBlank(courseType.getTotalEnd())) {builder.append(" and total <= ?");paramsList.add(Integer.parseInt(courseType.getTotalEnd()));}// 2.4课程费用if (StringUtils.isNotBlank(courseType.getCourseCostStart())) {builder.append(" and courseCost >= ?");paramsList.add(Double.parseDouble(courseType.getCourseCostStart()));}if (StringUtils.isNotBlank(courseType.getCourseCostEnd())) {builder.append(" and courseCost <= ?");paramsList.add(Double.parseDouble(courseType.getCourseCostEnd()));}// 3 使用// 条件 , 格式:" and ..? and ..."String condition = builder.toString();// 实际参数Object[] params = paramsList.toArray();// //////////////////////////////////////////////////////////// 2分页// 2.1 总记录数int totalRecord = this.courseTypeDao.getTotalRecord(condition, params);// 2.2 创建对象PageBean<CrmCourseType> pageBean = new PageBean<CrmCourseType>(pageNum,pageSize, totalRecord);// 2.3 分页数据List<CrmCourseType> data = this.courseTypeDao.findAll(condition,params, pageBean.getStartIndex(), pageBean.getPageSize());pageBean.setData(data);return pageBean;}

六,action层

com.itheima.crm.coursetype.web.action

// 分页数据private int pageNum = 1;public void setPageNum(int pageNum) {this.pageNum = pageNum;}private int pageSize = 2; // 固定值public void setPageSize(int pageSize) {this.pageSize = pageSize;}// /////////////////////////////////////////////////** * 查询所有 *  * @return */public String findAll() {/* * 1简单查询 List<CrmCourseType> allCourseType = * this.courseTypeService.findAll(); // * 查询结果存在值栈 , jsp 通过“#key”获得 * ActionContext.getContext().put("allCourseType", allCourseType); *//* * 2 条件查询 List<CrmCourseType> allCourseType = * this.courseTypeService.findAll(courseType); * ActionContext.getContext().put("allCourseType", allCourseType); */// 3 分页 + 条件PageBean<CrmCourseType> pageBean = this.courseTypeService.findAll(courseType, pageNum, pageSize);ActionContext.getContext().put("pageBean", pageBean);return "findAll";}

七,struts.xml文件的配置

<package name="cou" namespace="/" extends="common"><action name="courseTypeAction_*"class="com.itheima.crm.coursetype.web.action.CourseTypeAction"method="{1}"><result name="findAll">/WEB-INF/pages/coursetype/listCourse.jsp</result></action></package>


八,spring文件的配置

<bean id="courseTypeDao" class="com.itheima.crm.coursetype.dao.impl.CourseTypeDaoImpl"><property name="sessionFactory" ref="sessionFactory"></property></bean><bean id="courseTypeService" class="com.itheima.crm.coursetype.service.impl.CourseTypeServiceImpl"><property name="courseTypeDao" ref="courseTypeDao"></property></bean>

九,jsp文件

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><%@ taglib uri="/struts-tags" prefix="s" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>无标题文档</title><link href="${pageContext.request.contextPath}/css/sys.css" type="text/css" rel="stylesheet" /></head><body > <table border="0" cellspacing="0" cellpadding="0" width="100%">  <tr>    <td class="topg"></td>  </tr></table><table border="0" cellspacing="0" cellpadding="0"  class="wukuang"width="100%">  <tr>    <td width="1%"><img src="${pageContext.request.contextPath}/images/tleft.gif"/></td>    <td width="39%" align="left">[课程类别]</td>       <td width="57%"align="right"><a href="javascript:void(0)" onclick="javascript:document.forms[0].submit();"><img src="${pageContext.request.contextPath}/images/button/gaojichaxun.gif" /></a>          <%--编辑前:添加类别 --%>    <a href="${pageContext.request.contextPath}/courseTypeAction_addOrEditUI">       <img src="${pageContext.request.contextPath}/images/button/tianjia.gif" />    </a>    </td>    <td width="3%" align="right"><img src="${pageContext.request.contextPath}/images/tright.gif"/></td>  </tr></table><%--条件查询 start --%><s:form namespace="/" action="courseTypeAction_findAll"><%--隐藏域,存放当前页 --%><s:hidden id="pageNum" name="pageNum" value="1"></s:hidden><table width="88%" border="0" class="emp_table" style="width:80%;">  <tr>    <td width="10%">课程类别:</td>    <td><s:textfield name="courseName" size="30"></s:textfield></td>  </tr>  <tr>    <td >课程简介:</td>    <td ><s:textfield name="remark" size="30" ></s:textfield></td>  </tr>  <tr>      <td >总学时:</td>    <td >    <s:textfield name="totalStart" size="12"></s:textfield>      至      <s:textfield name="totalEnd" size="12"></s:textfield>    </td>  </tr>  <tr>    <td>课程费用:</td>    <td >    <s:textfield name="courseCostStart" size="12"></s:textfield>     至     <s:textfield name="courseCostEnd" size="12"></s:textfield>     </td>  </tr></table></s:form><%--条件查询 end --%><table border="0" cellspacing="0" cellpadding="0" style="margin-top:5px;">  <tr>    <td ><img src="${pageContext.request.contextPath}/images/result.gif"/></td>  </tr></table><table width="97%" border="1" >    <tr class="henglan" style="font-weight:bold;">    <td width="14%" align="center">名称</td>    <td width="33%" align="center">简介</td>    <td width="13%" align="center">总学时</td>    <td width="18%" align="center">收费标准</td><td width="11%" align="center">编辑</td>  </tr>  <%--数据展示,单行:tabtd1;双行:tabtd2 --%>  <s:iterator value="#pageBean.data" var="vs">   <tr class="<s:property value="#vs.even ? tabtd1 : tabtd2" />">    <td align="center"><s:property value="#vs.courseName" /> </td>    <td align="center"> <s:property value="#vs.remark" /></td>    <td align="center"><s:property value="#vs.total" /></td>    <td align="center"><s:property value="#vs.courseCost" /></td>  <td width="11%" align="center">    <s:a action="courseTypeAction_addOrEditUI" namespace="/">  <s:param name="courseTypeId"  value="courseTypeId"></s:param>  <img src="${pageContext.request.contextPath}/images/button/modify.gif" class="img" />  </s:a>  </td>  </tr>   </s:iterator> </table><table border="0" cellspacing="0" cellpadding="0" align="center">  <tr>     <td align="right">    <span>第<s:property value="#pageBean.pageNum" />/<s:property value="#pageBean.totalPage" />页</span>        <span>        <s:if test="#pageBean.pageNum gt 1">            <a href="javascript:void(0)" onclick="showPage(1)">[首页]</a>              <a href="javascript:void(0)" onclick="showPage(<s:property value="#pageBean.pageNum - 1" />)">[上一页]</a>          </s:if>                <%--动态显示条 --%>        <s:iterator begin="#pageBean.start" end="#pageBean.end" var="num">           <a href="javascript:void(0)" onclick="showPage(<s:property value="#num" />)"><s:property value="#num" /></a>          </s:iterator>                <s:if test="#pageBean.pageNum lt #pageBean.totalPage">           <a href="javascript:void(0)" onclick="showPage(<s:property value="#pageBean.pageNum + 1" />)">[下一页]</a>              <a href="javascript:void(0)" onclick="showPage(<s:property value="#pageBean.totalPage" />)">[尾页]</a>              </s:if>        </span>    </td>  </tr></table><script type="text/javascript">function showPage(num){//1 修改隐藏域的值document.getElementById("pageNum").value = num;//2 提交表单document.forms[0].submit();}</script></body></html>




1 0