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

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

光明是人的信仰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值