zoukankan      html  css  js  c++  java
  • 使用Jsp/Js/Ajax/Json/Jquery/Easyui + Servlet + JDBC + Lucene/Mysql/Oracle完成数据库分页

    package loaderman.action;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.LinkedHashMap;
    import java.util.Map;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import loaderman.entity.Page;
    import loaderman.service.impl.EmpService;
    import loaderman.service.inter.IEmpService;
    import net.sf.json.JSONArray;
    
    
    
    /**
     * 员工管理模块
     * 控制器
     */
    public class EmpServlet extends HttpServlet {
        public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
            this.doPost(request,response);
        }
        public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
            try {
                //设置编号方式
                request.setCharacterEncoding("UTF-8");
    
                //获取客户端传入的参数
                String strPage = request.getParameter("page");//rows
                if(strPage == null || strPage.trim().length()==0){
                    strPage = "1";
                }
                Integer currPageNO = Integer.parseInt(strPage);
    
                //调用业务层
                IEmpService iEmpService = new EmpService();
                Page page = iEmpService.show(currPageNO);
    
                //创建Map集合
                Map<String,Object> map = new LinkedHashMap<String,Object>();
                map.put("total",page.getAllRecordNO());
                map.put("rows",page.getEmpList());
    
                //使用第三方工具将map转成json文本
                JSONArray jsonArray = JSONArray.fromObject(map);
                String jsonJAVA = jsonArray.toString();
    
                //去掉二边的空格
                jsonJAVA = jsonJAVA.substring(1,jsonJAVA.length()-1);
                System.out.println("jsonJAVA=" + jsonJAVA);
    
                //以字符流的方式,将json字符串输出到客户端
                response.setContentType("text/html;charset=UTF-8");
                PrintWriter pw = response.getWriter();
                pw.write(jsonJAVA);
                pw.flush();
                pw.close();
    
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    package loaderman.dao.impl;
    
    import java.math.BigDecimal;
    import java.util.List;
    
    import loaderman.dao.inter.IEmpDao;
    import loaderman.entity.Emp;
    import loaderman.util.JdbcUtil;
    import org.apache.commons.dbutils.QueryRunner;
    import org.apache.commons.dbutils.handlers.BeanListHandler;
    import org.apache.commons.dbutils.handlers.ScalarHandler;
    
    
    /**
     * 员工管理模块
     * 持久层实现
     */
    public class EmpDao implements IEmpDao{
        public Integer getAllRecord() throws Exception {
            QueryRunner runner = new QueryRunner(JdbcUtil.getDataSource());
            String sql = "SELECT COUNT(EMPNO) FROM EMP";
            BigDecimal bigDecimal = (BigDecimal) runner.query(sql,new ScalarHandler());
            return bigDecimal.intValue();
        }
        public List<Emp> findAllRecord(int start, int end) throws Exception {
            QueryRunner runner = new QueryRunner(JdbcUtil.getDataSource());
            String sql = " SELECT XX.EMPNO,XX.ENAME,XX.JOB,XX.MGR,XX.HIREDATE,XX.SAL,XX.COMM,XX.DEPTNO" +
                    " FROM (SELECT ROWNUM ID,EMP.* FROM EMP WHERE ROWNUM<?) XX" +
                    " WHERE ID>?";
            Object[] params = {end,start};
            return runner.query(sql,new BeanListHandler<Emp>(Emp.class),params);
        }
    
    
    
    
    
    
    
    
    
        public static void main(String[] args) throws Exception{
            EmpDao dao = new EmpDao();
            System.out.println("共有" + dao.getAllRecord() + "个员工");
            System.out.println("------------------------------------------------第1页");
            for(Emp e : dao.findAllRecord(0,4)){
                System.out.println(e);
            }
            System.out.println("------------------------------------------------第2页");
            for(Emp e : dao.findAllRecord(3,7)){
                System.out.println(e);
            }
            System.out.println("------------------------------------------------第3页");
            for(Emp e : dao.findAllRecord(6,10)){
                System.out.println(e);
            }
            System.out.println("------------------------------------------------第4页");
            for(Emp e : dao.findAllRecord(9,13)){
                System.out.println(e);
            }
            System.out.println("------------------------------------------------第5页");
            for(Emp e : dao.findAllRecord(12,16)){
                System.out.println(e);
            }
        }
    }
    package loaderman.dao.inter;
    
    import loaderman.entity.Emp;
    
    import java.util.List;
    
    
    /**
     * 员工管理模块
     * 持久层接口
     */
    public interface IEmpDao {
        /**
         * 获取总记录数
         * @return 总记录数
         */
        public Integer getAllRecord() throws Exception;
        /**
         * 分批查询所有记录
         * @param start 表示 从第几条记录开始
         * @param end   表示 到第几条记录结束
         * @return      表示 start到end之间的记录集合,包含start和end
         */
        public List<Emp> findAllRecord(int start,int end) throws Exception;
    }
    package loaderman.entity;
    
    import java.util.Date;
    
    /**
     * 员工
     */
    public class Emp {
        private Integer empno;//编号
        private String ename;//姓名
        private String job;//工作
        private Integer mgr;//上级编号
        private Date hiredate;//入职时间
        private Integer sal;//月薪
        private Integer comm;//佣金
        private Integer deptno;//部门编号
        public Emp(){}
        public Integer getEmpno() {
            return empno;
        }
        public void setEmpno(Integer empno) {
            this.empno = empno;
        }
        public String getEname() {
            return ename;
        }
        public void setEname(String ename) {
            this.ename = ename;
        }
        public String getJob() {
            return job;
        }
        public void setJob(String job) {
            this.job = job;
        }
        public Integer getMgr() {
            return mgr;
        }
        public void setMgr(Integer mgr) {
            this.mgr = mgr;
        }
        public Date getHiredate() {
            return hiredate;
        }
        public void setHiredate(Date hiredate) {
            this.hiredate = hiredate;
        }
        public Integer getSal() {
            return sal;
        }
        public void setSal(Integer sal) {
            this.sal = sal;
        }
        public Integer getComm() {
            return comm;
        }
        public void setComm(Integer comm) {
            this.comm = comm;
        }
        public Integer getDeptno() {
            return deptno;
        }
        public void setDeptno(Integer deptno) {
            this.deptno = deptno;
        }
        @Override
        public String toString() {
            return this.empno+":"+this.ename+":"+this.sal+":"+this.hiredate+":"+this.deptno;
        }
    }
    package loaderman.entity;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * 分页类
     * @author AdminTC
     */
    public class Page {
        private Integer currPageNO;//当前页号OK
        private Integer perPageSize = 9;//每页显示记录数,默认为3条记录OK
        private Integer allRecordNO;//总记录数OK
        private Integer allPageNO;//总页号OK
        private List<Emp> empList = new ArrayList<Emp>();//该本页显示的内容OK
        public Page(){}
        public Integer getCurrPageNO() {
            return currPageNO;
        }
        public void setCurrPageNO(Integer currPageNO) {
            this.currPageNO = currPageNO;
        }
        public Integer getPerPageSize() {
            return perPageSize;
        }
        public void setPerPageSize(Integer perPageSize) {
            this.perPageSize = perPageSize;
        }
        public Integer getAllRecordNO() {
            return allRecordNO;
        }
        public void setAllRecordNO(Integer allRecordNO) {
            this.allRecordNO = allRecordNO;
        }
        public Integer getAllPageNO() {
            return allPageNO;
        }
        public void setAllPageNO(Integer allPageNO) {
            this.allPageNO = allPageNO;
        }
        public List<Emp> getEmpList() {
            return empList;
        }
        public void setEmpList(List<Emp> empList) {
            this.empList = empList;
        }
    }
    package loaderman.service.impl;
    
    import loaderman.dao.impl.EmpDao;
    import loaderman.dao.inter.IEmpDao;
    import loaderman.entity.Emp;
    import loaderman.entity.Page;
    import loaderman.service.inter.IEmpService;
    
    import java.util.List;
    
    
    /**
     * 员工管理模块
     * 业务层实现
     */
    public class EmpService implements IEmpService{
        private IEmpDao iEmpDao = new EmpDao();
        public Page show(int currPageNO) throws Exception {
            Page page = new Page();
    
            //封装当前页号
            page.setCurrPageNO(currPageNO);
    
            //封装总记录数
            Integer allRecordNO = iEmpDao.getAllRecord();
            page.setAllRecordNO(allRecordNO);
    
            //封装总页数
            Integer allPageNO = null;
            if(page.getAllRecordNO() % page.getPerPageSize() == 0){
                allPageNO = page.getAllRecordNO() / page.getPerPageSize();
            }else{
                allPageNO = page.getAllRecordNO() / page.getPerPageSize() + 1;
            }
            page.setAllPageNO(allPageNO);
    
            //封装该本显示的内容
            Integer start = (page.getCurrPageNO()-1) * page.getPerPageSize();
            Integer end = page.getCurrPageNO() * page.getPerPageSize() + 1;
            List<Emp> empList = iEmpDao.findAllRecord(start,end);
            page.setEmpList(empList);
    
            return page;
        }
    
    
    
    
    
    
    
        public static void main(String[] args) throws Exception{
            EmpService service = new EmpService();
            System.out.println("---------------------------------------NO1");
            Page page = service.show(1);
            for(Emp e : page.getEmpList()){
                System.out.println(e);
            }
            System.out.println("---------------------------------------NO2");
            page = service.show(2);
            for(Emp e : page.getEmpList()){
                System.out.println(e);
            }
            System.out.println("---------------------------------------NO3");
            page = service.show(3);
            for(Emp e : page.getEmpList()){
                System.out.println(e);
            }
            System.out.println("---------------------------------------NO4");
            page = service.show(4);
            for(Emp e : page.getEmpList()){
                System.out.println(e);
            }
            System.out.println("---------------------------------------NO5");
            page = service.show(5);
            for(Emp e : page.getEmpList()){
                System.out.println(e);
            }
        }
    }
    package loaderman.service.inter;
    
    
    import loaderman.entity.Page;
    /**
     * 员工管理模块
     * 业务层接口
     */
    public interface IEmpService {
        /**
         * 根据页号获取该页需要显示的内容
         * @param currPageNO 当前页号
         * @return 封装该页需要显示的内容
         */
        public Page show(int currPageNO) throws Exception;
    }
    package loaderman.util;
    
    import com.mchange.v2.c3p0.ComboPooledDataSource;
    /**
     * 工具类
     */
    public class JdbcUtil {
        /**
         * 加载src目录下的c3p0-config.xml文件
         */
        private static ComboPooledDataSource dataSource = new ComboPooledDataSource();
        /**
         * 获取数据源
         * @return 数据源
         */
        public static ComboPooledDataSource getDataSource() {
            return dataSource;
        }
    }
    <?xml version="1.0" encoding="UTF-8"?>
    <c3p0-config>
        <default-config>
            <property name="driverClass">oracle.jdbc.driver.OracleDriver</property>
            <property name="jdbcUrl">jdbc:oracle:thin:@127.0.0.1:1521:orcl</property>
            <property name="user">scott</property>
            <property name="password">tiger</property>
            <property name="initialPoolSize">5</property>
            <property name="maxPoolSize">5</property>
            <property name="minPoolSize">1</property>
            <property name="acquireIncrement">2</property>
        </default-config>
    </c3p0-config>
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
        http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
        <servlet>
            <servlet-name>EmpServlet</servlet-name>
            <servlet-class>loaderman.action.EmpServlet</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>EmpServlet</servlet-name>
            <url-pattern>/EmpServlet</url-pattern>
        </servlet-mapping>
    
    </web-app>
  • 相关阅读:
    PHP 小tip .(@)符号和 php if 赋值
    PHP定义字符串时单引号和双引号的区别
    笔记本 windows 10 安装
    unix_12c_db_init
    教你如何使用php session
    js和 php 介绍
    PHP 简单答题系统
    PHP list() 函数
    PHP集成支付宝快速实现充值功能
    eclipse如何导入PHP的项目
  • 原文地址:https://www.cnblogs.com/loaderman/p/10062797.html
Copyright © 2011-2022 走看看