43.0BaseDao抽取dao公共父类

43.1. 回顾

1. 把数据库表中查询的结果封装到一个实体类中。

命名规则:类名和表名一致 类中属性和表的字段对应。

表中的一条记录对应实体的一个对象  多条记录→集合

43.2. 正文

目录

43.1. 回顾

43.2. 正文

43.3. 抽取dao公共父类。

43.4. 引入数据源


43.3. 抽取dao公共父类。

通过昨天我们的操作,可以发现dao类中很多方法都有一些相同的内容。那么我们就可以把这些相同的代码抽取到一个公共父类中。只需要这些dao子类继承该父类,子类就无需再写这些公共代码。

 

public class BaseDao {
    //1.相同属性抽取过来
    protected Connection conn=null;
    protected PreparedStatement ps=null;
    protected ResultSet rs=null;
    private String url="jdbc:mysql://localhost:3306/myinfo";
    private String user="root";
    private String password="root";
    private static String driverName="com.mysql.cj.jdbc.Driver";
    //加载驱动放入静态代码块中--随着类的加载而被加载而且只会加载一次。 静态方法或静态代码只能调用静态成员
    static{
        try {
            Class.forName(driverName);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    //2.获取连接对象
    public void getConnection() throws SQLException {
        conn = DriverManager.getConnection(url, user, password);
    }


    //3.关闭资源
    public void closeAll(){
        //关闭连接数据库的资源
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

}

抽象一个公共的增删改方法,

//4. 公共的增删改操作 参数类型不同:Object  参数个数不确定 ...
    public int update(String sql,Object... params){
        try {
            //2. 获取连接数据库的对象
            getConnection();
            //3. 获取执行sql语句的对象 ?表示占位符
            ps = conn.prepareStatement(sql);
            //4. 为占位符赋值
            for(int i=0;i<params.length;i++) {
                ps.setObject(i+1, params[i]);
            }
            //5. 执行sql语句 把sql执行的结果封装到ResultSet中
            int row = ps.executeUpdate();
            return row;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }
        return 0;
    }

 

43.4. 引入数据源

数据源就是用来存放连接数据库的对象。也叫数据库连接池。 :

C3P0、DBCP 、BoneCP、Proxool、

DDConnectionBroker、DBPool、

XAPool、Primrose、SmartPool、

MiniConnectionPoolManager

Druid等。

 (1)引入jar包

(2)修改basedao代码

 上面选择的内容,我们发现写死再代码中,如果未来交付时,需要改为对应客户的信息。需要修改源码--一旦代码写完不允许修改源码。--我们可以把上面这些信息写在属性文件中。 XXX.properties

db.properties

 

driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/myinfo
username=root
password=root
# 初始化连接数量
initialSize=5
# 最大连接数
maxActive=10
# 最大等待时间
maxWait=3000

注意:名称必须为上面的名称→→ 值可以是你自己

 修改baseDao代码

public class BaseDao {
    //1.相同属性抽取过来
    protected Connection conn=null;
    protected PreparedStatement ps=null;
    protected ResultSet rs=null;
    //硬编码
    private static DataSource dataSource=null;
    //加载驱动放入静态代码块中--随着类的加载而被加载而且只会加载一次。 静态方法或静态代码只能调用静态成员
    static{
        try {
            //创建一个属性类
            Properties prop=new Properties();
            //加载属性文件
            prop.load(BaseDao.class.getClassLoader().getResourceAsStream("db.properties"));
            dataSource= DruidDataSourceFactory.createDataSource(prop);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //2.获取连接对象
    public void getConnection() throws SQLException {

        conn = dataSource.getConnection();
    }

    //4. 公共的增删改操作 参数类型不同:Object  参数个数不确定 ...
    public int update(String sql,Object... params){
        try {
            //2. 获取连接数据库的对象
            getConnection();
            //3. 获取执行sql语句的对象 ?表示占位符
            ps = conn.prepareStatement(sql);
            //4. 为占位符赋值
            for(int i=0;i<params.length;i++) {
                ps.setObject(i+1, params[i]);
            }
            //5. 执行sql语句 把sql执行的结果封装到ResultSet中
            int row = ps.executeUpdate();
            return row;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }
        return 0;
    }

    //3.关闭资源
    public void closeAll(){
        //关闭连接数据库的资源
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

}

相关推荐

  1. JDBC高级篇-JDBC工具DAO封装和BaseDAO工具

    2023-12-05 21:32:01       31 阅读
  2. 构建树

    2023-12-05 21:32:01       34 阅读
  3. 继承关系

    2023-12-05 21:32:01       37 阅读
  4. 通过定义日志输出

    2023-12-05 21:32:01       26 阅读
  5. 重写的方法

    2023-12-05 21:32:01       62 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-05 21:32:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-05 21:32:01       106 阅读
  3. 在Django里面运行非项目文件

    2023-12-05 21:32:01       87 阅读
  4. Python语言-面向对象

    2023-12-05 21:32:01       96 阅读

热门阅读

  1. Hadoop 概述

    2023-12-05 21:32:01       46 阅读
  2. C++:模板进阶

    2023-12-05 21:32:01       47 阅读
  3. vue 生命周期

    2023-12-05 21:32:01       55 阅读
  4. HuggingFace学习笔记--利用API实现简单的NLP任务

    2023-12-05 21:32:01       51 阅读
  5. HarmonyOS开发(八):动画及网络

    2023-12-05 21:32:01       40 阅读
  6. 用互斥锁、信号量、条件变量实现线程同步

    2023-12-05 21:32:01       52 阅读
  7. C++学习寄录(九.多态)

    2023-12-05 21:32:01       57 阅读
  8. 复杂sql分析 以及 索引合并

    2023-12-05 21:32:01       47 阅读
  9. bitnami Docker 安装ELK(elasticsearch, logstash, kibana)

    2023-12-05 21:32:01       59 阅读
  10. mysql多版本并发控制mvcc

    2023-12-05 21:32:01       58 阅读
  11. python代码块整行缩进与取消整行缩进快捷键

    2023-12-05 21:32:01       57 阅读
  12. 题目 异常处理

    2023-12-05 21:32:01       54 阅读