【JDBC】使用DBUtils编写DAO

定义DAO接口

package com.lhk;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public interface DBUtilsDAO<T> {

    /**
     * 批量处理的方法
     * @param connection
     * @param sql
     * @param args:填充占位符的Object []类型的可变参数
     */
    void batch(Connection connection, String sql, Object[] ...args);


    /**
     * 返回具体的一个值,例如,总人数,平均工资等
     * @param connection
     * @param sql
     * @param args
     * @return
     */
    <E> E getForValue(Connection connection, String sql, Object ...args);


    /**
     * 返回T的一个集合
     * @param connection
     * @param sql
     * @param args
     * @return
     */
    List<T> getForList(Connection connection, String sql, Object ...args);


    /**
     * 返回一个T对象
     * @param connection
     * @param sql
     * @param args
     * @return
     * @throws SQLException 
     */
    T get(Connection connection, String sql, Object ...args) throws SQLException;

    /**
     * INSERT,UPDATE,DELETE
     * @param connection 数据库连接
     * @param sql SQL语句
     * @param args 填充占位符的可变参数
     */
    void update(Connection connection, String sql, Object ...args);
}

编写DAO的实现类DBUtilsImp.java

package com.lhk;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

/**
 *使用QueryRunner提供具体的实现
 * @param <T>: 子类需传入的泛型类型
 */
public class DBUtilsDAOImp<T> implements DBUtilsDAO<T> {

    private QueryRunner queryRunner = null;
    private Class<T> type;

    public DBUtilsDAOImp() {
        queryRunner = new QueryRunner();
        type = ReflectionUtils.getSuperGenericType(getClass());
    }

    @Override
    public void batch(Connection connection, String sql, Object[]... args) {


    }

    @Override
    public <E> E getForValue(Connection connection, String sql, Object... args) {

        return null;
    }

    @Override
    public List<T> getForList(Connection connection, String sql, Object... args) {

        return null;
    }

    @Override
    public T get(Connection connection, String sql, Object... args) throws SQLException {

        return queryRunner.query(connection, sql, new BeanHandler<>(type), args);
    }

    @Override
    public void update(Connection connection, String sql, Object... args) {

    }

}

Customer类

package com.lhk;

import java.sql.Date;

public class Customer {

    private Integer id;
    private String name;
    private String email;
    private Date birth;

    public Customer() {
        super();
    }

    public Customer(Integer id, String name, String email, Date birth) {
        super();
        this.id = id;
        this.name = name;
        this.email = email;
        this.birth = birth;
    }

    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public Date getBirth() {
        return birth;
    }
    public void setBirth(Date birth) {
        this.birth = birth;
    }

    @Override
    public String toString() {
        return "Customer [id=" + id + ", name=" + name + ", email=" + email + ", birth=" + birth + "]";
    }


}

编写CustomerDAO来继承这个实现类

package com.lhk;

public class CustomerDAO extends DBUtilsDAOImp<Customer> {

}

编写CustomerDAOTest单元测试类

package com.lhk;

import static org.junit.Assert.*;

import java.sql.Connection;

import org.junit.Test;

public class CustomerDAOTest {

    CustomerDAO customerDao = new CustomerDAO();

    @Test
    public void testBatch() {
        fail("Not yet implemented");
    }

    @Test
    public void testGetForValue() {
        fail("Not yet implemented");
    }

    @Test
    public void testGetForList() {
        fail("Not yet implemented");
    }

    @Test
    public void testGet() {
        Connection connection = null;

        try {
            connection = DBTools.getConnection();
            String sql = "SELECT id, name, email, birth"
                    + " FROM customer WHERE id = ?";
            Customer customer = customerDao.get(connection, sql, 4);
            System.out.println(customer);
        } catch (Exception e) {
            e.getMessage();
        } finally {
            DBTools.close(connection, null, null);
        }
    }

    @Test
    public void testUpdate() {
        fail("Not yet implemented");
    }

}

查询结果

Customer [id=4, name=XiaoMing, email=351666845, birth=2018-05-11]

猜你喜欢

转载自blog.csdn.net/qq_37308779/article/details/80437847