自定义持久层(dao)框架

来源:互联网 发布:英雄联盟 知乎 编辑:程序博客网 时间:2024/05/21 22:57

2. 自定义持久层(dao)框架

更新案例:(insert/update/delete)

1.       加载驱动

2.       获取连接

3.       创建pstmt对象

4.      执行sql:  pstmt.executeUpdate();

5.      关闭

 

查询:(select)

1.       加载驱动

2.       获取连接

3.       创建pstmt对象

4.      执行sql:  pstmt.executeQuery();

5.      获取结果集对象: 封装对象、再添加到集合中,最后返回

6.      关闭

 

优化,实现一些通用的方法:
       1. 获取连接

1.      关闭连接

2.      更新

3.      查询

 

 

元数据:

       概念:数据库自身的定义, 例如数据库、表、列的信息

       分类:

1.      数据库元数据

2.      参数元数据

3.      结果集元数据

      

 

 

 

Jdbc通用类:

/**

 * 通过一个baseDao,处理所有与数据库的操作

 * 1. 查询的通用方法

 * 2. 更新的通用方法

 * @author Administrator

 *

 */

public class BaseDao {

   

    // 连接对象

    Connection con= null;

    PreparedStatement pstmt = null;

    ResultSet rs = null;

   

    // c3p0数据源创建

    private static DataSource ds;

    static {

        ds = new ComboPooledDataSource();

    }

 

    /**

     * 实现查询的通用方法

     * @param<T>  传入的对象类型,  在调用这个方法的时候,确定类型

     * @param sql  要查询的sql

     * @param clazz传入的对象类型,的字节码

     * @param paramValue   占位符(查询条件)对应的值

     * @return

     */

    public <T> List<T> query(String sql,Object[] paramValue,Class<T> clazz) {

        // 返回的数据

        List<T> list = new ArrayList<T>();

       

       

        try {

            //1. 获取连接

            con = ds.getConnection();

            //2. 创建stmt对象

            pstmt = con.prepareStatement(sql);

           

            // 通过参数元数据,获取占位符个数

            int count =pstmt.getParameterMetaData().getParameterCount();

            for (int i=0; i<count; i++) {

                // 设置参数值.....

                pstmt.setObject(i+1, paramValue[i]);

            }

           

            //3. 获取结果集

            rs = pstmt.executeQuery();

            // 获取结果集元数据

            ResultSetMetaData rsmd = rs.getMetaData();

            // 从结果集元数据中,获取列个数

            int columnCount = rsmd.getColumnCount();

           

            // 遍历结果集

            while (rs.next()) {

               

                // 创建对象

                T t = clazz.newInstance();

 

                // 遍历当前行的每一列

                for (int i=0; i<columnCount; i++) {

                    // 列名称 【对象属性】

                    String columnName = rsmd.getColumnName(i+1);

                    // 列值     【对象属性,对应的值】

                    Object columnValue = rs.getObject(columnName);

                   

                    // 通过BeanUtils把指定的值拷贝到指定名称的对象中

                    BeanUtils.copyProperty(t, columnName, columnValue);

                }

               

                // 把封装后的对象(t),添加到集合中

                list.add(t);

            }

           

        } catch (Exception e) {

            throw new RuntimeException(e);

        } finally {

            this.closeAll(con,pstmt, rs);

        }

        return list;

    }

   

    /**

     *

     * @param sql  更新的sql语句

     * @param paramValues   sql语句参数对应的值

     */

    public int update(String sql,Object[] paramValues){

       

        try {

            //1. 获取连接

            con = ds.getConnection();

            //2. 创建stmt对象

            pstmt = con.prepareStatement(sql);

            // 获取参数元数据

            int count =pstmt.getParameterMetaData().getParameterCount();

            for (int i=0; i<count; i++) {

                // 设置参数值

                pstmt.setObject(i+1, paramValues[i]);

            }

            //3. 执行sql,返回受影响行数

            returnpstmt.executeUpdate();

        } catch (Exception e) {

            throw new RuntimeException(e);

        } finally {

            this.closeAll(con,pstmt, null);

        }

    }

   

   

   

   

    public void closeAll(Connection con, Statement stmt, ResultSet rs) {

        try {

            if (rs !=null) {

                rs.close();

                rs = null;

            }

            if (stmt !=null) {

                stmt.close();

                stmt = null;

            }

            if (con !=null) {

                con.close();

                con = null;

            }

        } catch (SQLException e) {

            throw new RuntimeException(e);

        }

    }

}

 

0 0
原创粉丝点击