mysql DAO basedao

The main function

The method of operation of the database package

CRUD are inside basedao inside 

dbutils inside update additions and deletions can be achieved

dbutils inside the query can be achieved inquiry line and multi-line

Query result is returned bean package object.

Use the time to achieve specific method basedao other needs of inheritance

public class UserDaoImpl extends BaseDao<User> implements UserDao
com.itstaredu.bookstore.dao Package; 

/ ** 
 * @version 1.0 
 * @date 2019/12/5 9:32 PM 
 ** / 

Import com.itstaredu.bookstore.utils.JDBCUtils; 
Import org.apache.commons.dbutils .QueryRunner; 
Import org.apache.commons.dbutils.handlers.BeanHandler; 
Import org.apache.commons.dbutils.handlers.BeanListHandler; 

Import java.lang.reflect.ParameterizedType; 
Import java.lang.reflect.Type; 
Import Java. sql.Connection; 
Import java.sql.SQLException; 
Import java.util.List; 

/ ** 
 * the method of encapsulation of basic operation of the database 
 * line all CRUD table there are calls 
 * object's method 
 * 
 * / 
public class BaseDao <T> { 
    Private class <T> type;
    QueryRunner queryRunner new new QueryRunner = Private (); 

    public BaseDao () { 
        Class = this.getClass CLA () <BaseDao the extends?>; 
        / ** 
         * with generic parent 
         * / 
        ParameterizedType for TP = (ParameterizedType for) cla.getGenericSuperclass (); 
        the type [] types tp.getActualTypeArguments = (); 
        type = (class <T>) types [0]; 
    } 

    / ** 
     * tools for database operations to provide dbutils 
     * CRUD 
     * / 
    public int Update (String SQL, the params Object ...) { 
        / ** 
         * not outwardly polishing process is troublesome in the processing 
         * / 
        int Update = 0;  
        Connection Connect JDBCUtils.getConnect = ();
        the try {
            update = queryRunner.update(connect, sql, params);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.releaseConnection(connect);
        }
        return update;
    }

    /**
     * ResultSetHandler 将结果转换为对象的处理程序
     * The Class that objects returned from handle() are created from
     */
    public T getBean(String sql, Object... params) {
        Connection conn = JDBCUtils.getConnect();

        T t = null;
        try {
            t = queryRunner.query(conn, sql, new BeanHandler<T>(type), params);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.releaseConnection(conn);
        }
        return t;
    }

    public List<T> getListBean(String sql, Object... params) {
        Connection conn = JDBCUtils.getConnect();
        List<T> list = null;
        try {
            list = queryRunner.query(conn, sql, new BeanListHandler<T>(type), params);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.releaseConnection(conn);
        }
        return list;
    }
}

  

Guess you like

Origin www.cnblogs.com/liubosong/p/11993705.html