Java进阶学习第十天(连接池、分页)

一、连接池

1、思考:程序中连接如何管理?
① 分析:涉及频繁的连接的打开、关闭,影响程序的运行效率!
② 连接管理:预先创建一组连接,用的时候每次取出一个; 用完后,再放回

2、学习连接池:
① 自定义一个连接池
② 学习优秀的连接池组件:DBCP、C3P0
③ 自定义连接池

3、自定义连接池
① 程序实现思路
◆ 指定“初始化连接数”(APP启动时就执行创建)
◆ 指定“最大连接数”
◆ 指定“当前使用个数”(不能超出最大连接数)
② 代码实现
◆ MyPool.java连接池类
◆ 指定全局参数:初始化数目、最大连接数、当前连接、连接池集合
◆ 构造函数:循环创建多个连接
◆ 写出一个创建连接方法
◆ 获取连接(有连接:直接拿;没有连接:判断是否达到最大连接数,达到就抛出异常,没有达到就创建新连接)
◆ 释放连接:连接放回集合中(判断是否达到初始化数目)

public class MyPool {
    private int init_count = 3;     // 初始化连接数目
    private int max_count = 6;      // 最大连接数
    private int current_count = 0;  // 记录当前使用连接数
    // 连接池 (存放所有的初始化连接)
    private LinkedList<Connection> pool = new LinkedList<Connection>(); 
    //1.构造函数中,初始化连接放入连接池
    public MyPool() {
        // 初始化连接
        for (int i=0; i<init_count; i++){
            // 记录当前连接数目
            current_count++;
            // 创建原始的连接对象
            Connection con = createConnection();
            // 把连接加入连接池
            pool.addLast(con);
        }
    }

    //2.创建一个新的连接的方法
    private Connection createConnection(){
        try {
            Class.forName("com.mysql.jdbc.Driver");
            // 原始的目标对象
            final Connection con = DriverManager.getConnection("jdbc:mysql:///jdbc_demo", "root", "root");      
            /**********对con对象代理**************/  
            // 对con创建其代理对象
            Connection proxy = (Connection) Proxy.newProxyInstance( 
                    con.getClass().getClassLoader(),    // 类加载器
                    //con.getClass().getInterfaces(),   // 当目标对象是一个具体的类的时候 
                    new Class[]{Connection.class},      // 目标对象实现的接口                
                    new InvocationHandler() {// 当调用con对象方法的时候, 自动触发事务处理器
                        @Override
                        public Object invoke(Object proxy, Method method, Object[] args)
                                throws Throwable {
                            // 方法返回值
                            Object result = null;
                            // 当前执行的方法的方法名
                            String methodName = method.getName();   
                            // 判断当执行了close方法的时候,把连接放入连接池
                            if ("close".equals(methodName)) {
                                System.out.println("begin:当前执行close方法开始!");
                                // 连接放入连接池 (判断..)
                                pool.addLast(con);
                                System.out.println("end: 当前连接已经放入连接池了!");
                            } else {
                                // 调用目标对象方法
                                result = method.invoke(con, args);
                            }
                            return result;
                        }
                    }
            );
            return proxy;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    //3. 获取连接
    public Connection getConnection(){
        // 3.1 判断连接池中是否有连接, 如果有连接,就直接从连接池取出
        if (pool.size() > 0){
            return pool.removeFirst();
        }   
        // 3.2 连接池中没有连接: 判断,如果没有达到最大连接数,创建;
        if (current_count < max_count) {
            // 记录当前使用的连接数
            current_count++;
            // 创建连接
            return createConnection();
        }
        // 3.3 如果当前已经达到最大连接数,抛出异常
        throw new RuntimeException("当前连接已经达到最大连接数目!");
    }

    //4. 释放连接
    public void realeaseConnection(Connection con) {
        // 4.1 判断:池的数目如果小于初始化连接,就放入池中
        if (pool.size() < init_count){
            pool.addLast(con);
        } else {
            try {
                // 4.2 关闭 
                current_count--;
                con.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public static void main(String[] args) throws SQLException {
        MyPool pool = new MyPool();
        System.out.println("当前连接: " + pool.current_count); // 3

        // 使用连接
        pool.getConnection();
        pool.getConnection();
        Connection con4 = pool.getConnection();
        Connection con3 = pool.getConnection();
        Connection con2 = pool.getConnection();
        Connection con1 = pool.getConnection();

        // 释放连接, 连接放回连接池
        // pool.realeaseConnection(con1);
        /*
         * 希望:当关闭连接的时候,要把连接放入连接池!【当调用Connection接口的close方法时候,希望触发pool.addLast(con);操作】
         *                                                                          把连接放入连接池
         * 解决1:实现Connection接口,重写close方法(不行)
         * 解决2:动态代理
         */
        con1.close();
        // 再获取
        pool.getConnection();
        System.out.println("连接池:" + pool.pool.size());   // 0
        System.out.println("当前连接: " + pool.current_count); // 3
    }
}

4、代理:如果对某个接口中的某个指定的方法的功能进行扩展,而不想实现接口里所有方法,可以使用(动态)代理模式
① Java中代理模式:静态、动态、Cglib代理(spring)
② 使用动态代理,可以监测接口中方法的执行!

5、如何对Connection对象,生成一个代理对象

static Object newProxyInstance(
    ClassLoader loader,    -- 当前使用的类加载器
    Class<?>[] interfaces,  --  目标对象(Connection)实现的接口类型
    InvocationHandler h    -- 事件处理器:当执行上面接口中的方法的时候,就会自动触发事件处理器代码,把当前执行的方法(method)作为参数传入
)  

6、代理的总结:(了解会用)
使用代理,可以在不实现接口的情况,对接口的方法进行扩展,添加额外的用户需要的业务逻辑

二、开源的连接池技术

1、Sun公司约定: 如果是连接池技术,需要实现一个接口x.sql.sql.DataSource;

2、DBCP连接池:
① DBCP 是 Apache 软件基金组织下的开源连接池实现,使用DBCP数据源,应用程序应在系统中增加如下两个 jar 文 件:
◆ Commons-dbcp.jar:连接池的实现
◆ Commons-pool.jar:连接池实现的依赖库
② Tomcat 的连接池正是采用该连接池来实现的。该数据库连接池既可以与应用服务器整合使用,也可由应用程序独立使用。
③ 核心类:BasicDataSource
④ 使用步骤
◆ 引入jar文件
commons-dbcp-1.4.jar
commons-pool-1.5.6.j

public class App_DBCP {

    // 1. 硬编码方式实现连接池
    @Test
    public void testDbcp() throws Exception {
        // DBCP连接池核心类
        BasicDataSource dataSouce = new BasicDataSource();

        // 连接池参数配置:初始化连接数、最大连接数、连接字符串、驱动、用户、密码
        dataSouce.setUrl("jdbc:mysql:///jdbc_demo");//数据库连接字符串
        dataSouce.setDriverClassName("com.mysql.jdbc.Driver");//数据库驱动
        dataSouce.setUsername("root");  //数据库连接用户
        dataSouce.setPassword("root");  //数据库连接密码
        dataSouce.setInitialSize(3);  // 初始化连接
        dataSouce.setMaxActive(6);    // 最大连接
        dataSouce.setMaxIdle(3000);   // 最大空闲时间

        // 获取连接
        Connection con = dataSouce.getConnection();
        con.prepareStatement("delete from admin where id=3").executeUpdate();
        // 关闭
        con.close();
    }

    @Test
    // 2. 【推荐】配置方式实现连接池,便于维护
    public void testProp() throws Exception {
        // 加载prop配置文件
        Properties prop = new Properties();
        // 获取文件流
        InputStream inStream = App_DBCP.class.getResourceAsStream("db.properties");
        // 加载属性配置文件
        prop.load(inStream);
        // 根据prop配置,直接创建数据源对象
        DataSource dataSouce = BasicDataSourceFactory.createDataSource(prop);
        // 获取连接
        Connection con = dataSouce.getConnection();
        con.prepareStatement("delete from admin where id=4").executeUpdate();
        // 关闭
        con.close();
    }
}

◆ 配置方式实现DBCP连接池, 配置文件中的key与BaseDataSouce中的属性一样(db.properties):

url=jdbc:mysql:///jdbc_demo
driverClassName=com.mysql.jdbc.Driver
username=root
password=root
initialSize=3
maxActive=6
maxIdle=3000

3、C3P0连接池
① 最常用的连接池技术,Spring框架,默认支持C3P0连接池技术!
② 核心类:CombopooledDataSource ds;
③ 使用
◆ 下载,引入jar文件:c3p0-0.9.1.2.jar
◆ 使用连接池,创建连接
◇ 硬编码方式
◇ 配置方式(xml)

public class App {
    @Test
    //1. 硬编码方式,使用C3P0连接池管理连接
    public void testCode() throws Exception {
        // 创建连接池核心工具类
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        // 设置连接参数:url、驱动、用户密码、初始连接数、最大连接数
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/jdbc_demo");
        dataSource.setDriverClass("com.mysql.jdbc.Driver");
        dataSource.setUser("root");
        dataSource.setPassword("root");
        dataSource.setInitialPoolSize(3);
        dataSource.setMaxPoolSize(6);
        dataSource.setMaxIdleTime(1000);
        // ---> 从连接池对象中,获取连接对象
        Connection con = dataSource.getConnection();
        // 执行更新
        con.prepareStatement("delete from admin where id=7").executeUpdate();
        // 关闭
        con.close();
    }

    @Test
    //2. XML配置方式,使用C3P0连接池管理连接
    public void testXML() throws Exception {
        // 创建c3p0连接池核心工具类
        // 自动加载src下c3p0的配置文件【c3p0-config.xml】
        ComboPooledDataSource dataSource = new ComboPooledDataSource();  //使用默认的配置  
        // 获取连接
        Connection con = dataSource.getConnection();
        // 执行更新
        con.prepareStatement("delete from admin where id=5").executeUpdate();
        // 关闭 
        con.close();    
    }
}

◆ c3p0-config.xml:

<c3p0-config>
    <default-config>
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/jdbc_demo</property>
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="user">root</property>
        <property name="password">root</property>
        <property name="initialPoolSize">3</property>
        <property name="maxPoolSize">6</property>
        <property name="maxIdleTime">1000</property>
    </default-config>

    <named-config name="oracle_config">
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/jdbc_demo</property>
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="user">root</property>
        <property name="password">root</property>
        <property name="initialPoolSize">3</property>
        <property name="maxPoolSize">6</property>
        <property name="maxIdleTime">1000</property>
    </named-config>
</c3p0-config>

三、分页技术 ##

1、分页技术好处: 利于页面布局,且显示的效率高!

2、分页关键点
① 分页SQL语句;
② 后台处理:dao/service/servlet/JSP

3、实现步骤
① 环境准备
◆ 引入jar文件及引入配置文件:数据库驱动包、C3P0连接池jar文件及配置文件、DbUtis组件:QueryRunner qr = new QueryRuner(dataSouce);qr.update(sql);
◆ 公用类:JdbcUtils.java
② 先设计:PageBean.java
③ Dao接口设计/实现:2个方法(分页查数据、总记录数)
④ Service、servlet
⑤ JSP

4、utils工具类
① JdbcUtils.java

/**
 * 工具类
 * 1. 初始化C3P0连接池
 * 2. 创建DbUtils核心工具类对象
 */
public class JdbcUtils {
    /**
     *  1. 初始化C3P0连接池
     */
    private static  DataSource dataSource;
    static {
        dataSource = new ComboPooledDataSource();
    }
    /**
     * 2. 创建DbUtils核心工具类对象
     */
    public static QueryRunner getQueryRuner(){
        // 创建QueryRunner对象,传入连接池对象
        // 在创建QueryRunner对象的时候,如果传入了数据源对象;
        // 那么在使用QueryRunner对象方法的时候,就不需要传入连接对象;
        // 会自动从数据源中获取连接(不用关闭连接)
        return new QueryRunner(dataSource);
    }
}

② PageBean.java

/**
 * 封装分页的参数
 */
public class PageBean<T> {
    private int currentPage = 1; // 当前页, 默认显示第一页
    private int pageCount = 4;   // 每页显示的行数(查询返回的行数), 默认每页显示4行
    private int totalCount;      // 总记录数
    private int totalPage;       // 总页数 = 总记录数 / 每页显示的行数  (+ 1)
    private List<T> pageData;    // 分页查询到的数据
    // 返回总页数
    public int getTotalPage() {
        if (totalCount % pageCount == 0) {
            totalPage = totalCount / pageCount;
        } else {
            totalPage = totalCount / pageCount + 1;
        }
        return totalPage;
    }
    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }
    public int getCurrentPage() {
        return currentPage;
    }
    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }
    public int getPageCount() {
        return pageCount;
    }
    public void setPageCount(int pageCount) {
        this.pageCount = pageCount;
    }
    public int getTotalCount() {
        return totalCount;
    }
    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }   
    public List<T> getPageData() {
        return pageData;
    }
    public void setPageData(List<T> pageData) {
        this.pageData = pageData;
    }
}

5、Employee.java

/**
 * 1. 实体类设计 (因为用了DbUtils组件,属性要与数据库中字段一致)
 */
public class Employee {
    private int empId;          // 员工id
    private String empName;     // 员工名称
    private int dept_id;        // 部门id
    public int getEmpId() {
        return empId;
    }
    public void setEmpId(int empId) {
        this.empId = empId;
    }
    public String getEmpName() {
        return empName;
    }
    public void setEmpName(String empName) {
        this.empName = empName;
    }
    public int getDept_id() {
        return dept_id;
    }
    public void setDept_id(int deptId) {
        dept_id = deptId;
    }
}

6、数据访问层接口设计
① EmployeeDao.java

/**
 * 2. 数据访问层,接口设计
 */
public interface IEmployeeDao {
    //分页查询数据
    public void getAll(PageBean<Employee> pb);
    //查询总记录数
    public int getTotalCount();
}

② EmployeeDaoImpl.java

/**
 * 2. 数据访问层实现
 */
public class EmployeeDaoImpl implements EmployeeDao {
    @Override
    public void getAll(PageBean<Employee> pb) {
        //2.查询总记录数:设置到pb对象中
        int totalCount = this.getTotalCount();
        pb.setTotalCount(totalCount);
        /*
         * 问题: jsp页面,如果当前页为首页,再点击上一页报错!
         *              如果当前页为末页,再点下一页显示有问题!
         * 解决:
         *     1. 如果当前页 <= 0;       当前页设置当前页为1;
         *     2. 如果当前页 > 最大页数;  当前页设置为最大页数
         */
        // 判断
        if (pb.getCurrentPage() <=0) {
            pb.setCurrentPage(1);// 把当前页设置为1
        } else if (pb.getCurrentPage() > pb.getTotalPage()){
            pb.setCurrentPage(pb.getTotalPage());// 把当前页设置为最大页数
        }
        //1.获取当前页:计算查询的起始行、返回的行数
        int currentPage = pb.getCurrentPage();
        int index = (currentPage -1 ) * pb.getPageCount();//查询的起始行
        int count = pb.getPageCount();// 查询返回的行数
        //3.分页查询数据:把查询到的数据设置到pb对象中
        String sql = "select * from employee limit ?,?";
        try {
            // 得到Queryrunner对象
            QueryRunner qr = JdbcUtils.getQueryRuner();
            // 根据当前页,查询当前页数据(一页数据)
            List<Employee> pageData = qr.query(sql, new BeanListHandler<Employee>(Employee.class), index, count);
            // 设置到pb对象中
            pb.setPageData(pageData);   
        } catch (Exception e) {
            throw new RuntimeException(e);
        }   
    }
    @Override
    public int getTotalCount() {
        String sql = "select count(*) from employee";
        try {
            // 创建QueryRunner对象
            QueryRunner qr = JdbcUtils.getQueryRuner();
            // 执行查询, 返回结果的第一行的第一列
            Long count = qr.query(sql, new ScalarHandler<Long>());
            return count.intValue();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

7、业务逻辑层
① IEmployeeService.java

/**
 * 3. 业务逻辑层接口设计
 */
public interface EmployeeService {
    //分页查询数据
    public void getAll(PageBean<Employee> pb);
}

② EmployeeServiceImpl.java

/**
 * 3. 业务逻辑层,实现
 */
public class EmployeeServiceImpl implements EmployeeService {
    // 创建Dao实例
    private IEmployeeDao employeeDao = new EmployeeDao();
    @Override
    public void getAll(PageBean<Employee> pb) {
        try {
            employeeDao.getAll(pb);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

8、控制层:IndexServlet.java

/**
 * 4. 控制层开发
 */
public class IndexServlet extends HttpServlet {
    // 创建Service实例
    private EmployeeService employeeService = new EmployeeServiceImpl();
    // 跳转资源
    private String uri;
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            //1. 获取“当前页”参数;(第一次访问当前页为null) 
            String currPage = request.getParameter("currentPage");
            // 判断
            if (currPage == null || "".equals(currPage.trim())){
                currPage = "1";  // 第一次访问,设置当前页为1;
            }
            // 转换
            int currentPage = Integer.parseInt(currPage);
            //2. 创建PageBean对象,设置当前页参数; 传入service方法参数
            PageBean<Employee> pageBean = new PageBean<Employee>();
            pageBean.setCurrentPage(currentPage);
            //3. 调用service  
            employeeService.getAll(pageBean); // 【pageBean已经被dao填充了数据】
            //4. 保存pageBean对象,到request域中
            request.setAttribute("pageBean", pageBean);
            //5. 跳转 
            uri = "/WEB-INF/list.jsp";
        } catch (Exception e) {
            e.printStackTrace();  // 测试使用
            // 出现错误,跳转到错误页面;给用户友好提示
            uri = "/error/error.jsp";
        }
        request.getRequestDispatcher(uri).forward(request, response);   
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doGet(request, response);
    }
}

9、list.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!-- 引入jstl核心标签库 -->
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>分页查询数据</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
  </head>  
  <body>
    <table border="1" width="80%" align="center" cellpadding="5" cellspacing="0">
        <tr>
            <td>序号</td>
            <td>员工编号</td>
            <td>员工姓名</td>
        </tr>
        <!-- 迭代数据 -->
        <c:choose>
            <c:when test="${not empty requestScope.pageBean.pageData}">
                <c:forEach var="emp" items="${requestScope.pageBean.pageData}" varStatus="vs">
                    <tr>
                        <td>${vs.count }</td>
                        <td>${emp.empId }</td>
                        <td>${emp.empName }</td>
                    </tr>
                </c:forEach>
            </c:when>
            <c:otherwise>
                <tr>
                    <td colspan="3">对不起,没有你要找的数据</td>
                </tr>
            </c:otherwise>
        </c:choose>
        <tr>
            <td colspan="3" align="center">
                当前${requestScope.pageBean.currentPage }/${requestScope.pageBean.totalPage }页     &nbsp;&nbsp;
                <a href="${pageContext.request.contextPath }/index?currentPage=1">首页</a>
                <a href="${pageContext.request.contextPath }/index?currentPage=${requestScope.pageBean.currentPage-1}">上一页 </a>
                <a href="${pageContext.request.contextPath }/index?currentPage=${requestScope.pageBean.currentPage+1}">下一页 </a>
                <a href="${pageContext.request.contextPath }/index?currentPage=${requestScope.pageBean.totalPage}">末页</a>
            </td>
        </tr>
    </table>
  </body>
</html>

猜你喜欢

转载自blog.csdn.net/Mr_GaoYang/article/details/82557670