当前位置: 首页 > article >正文

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();
            }
        }
    }

}


http://www.kler.cn/a/152473.html

相关文章:

  • 大数据(十一):概率统计基础
  • STM32/GD32_分散加载
  • Paraformer 语音识别原理
  • Android Audio实战——音频焦点监听(十)
  • 从薛定谔的猫——量子理论基础
  • 设计模式详解(二):抽象工厂——Abstract Factory
  • JavaEE——简单认识CSS
  • oracle impdp 导入元数据表空间异常增大的解决办法
  • 党建引领·和谐共建——赤岗街首届微型社区养老服务公益博览会开幕
  • (2)(2.2) Lightware SF45/B(350度)
  • 2的幂运算
  • IC修真院 | 芯片嵌入式课程重磅上线!
  • 中伟视界:AI盒子中的报警预录像功能能解决什么问题?实现原理是怎样的?
  • 关于微信小程序中如何实现数据可视化-echarts动态渲染
  • java21虚拟线程
  • Windows平台下的oracle 11G-11.2.0.4补丁升级操作指南
  • java对xml压缩
  • GPLT(有空就写)
  • Java之Stream的实用语法
  • 掌握区块链技术将推进2024年市场发展脚步