Apache Commons DbUtils学习

DbUtils简介

DbUtils 库是一个很小的类的集合的设计,使使用 JDBC 更容易的工作。
DbUtils的优点:

  • 没有资源泄露的责任
  • 干净,清晰持久的代码
  • 从ResultSet中自动填充 JavaBean

DbUtils 核心API

  • org.apache.commons.dbutils.QueryRunner
  • org.apache.commons.dbutils.ResultSetHandler
  • org.apache.commons.dbutils.DbUtils

DbUtils 实例

public class Article {

    private Integer id;

    private String title;

    private String content;

    private String author;

    private BigDecimal payment;

    private String editDate;
    // getter setter
}
public class Test1 {

    public static final String DRIVER = "com.mysql.jdbc.Driver";

    public static final String DB_URL = "jdbc:mysql://localhost:3306/auth";

    public static final String USERNAME = "root";

    public static final String PASSWORD = "123456";

    public static void main(String[] args) {
        QueryRunner queryRunner = new QueryRunner();
        try {
            Class.forName(DRIVER);
            Connection connection = DriverManager.getConnection(DB_URL, USERNAME, PASSWORD);
            ResultSetHandler<Article> resultSetHandler = new BeanHandler<>(Article.class);
            String sql = "select * from blog limit 1";
            Article article = queryRunner.query(connection, sql, resultSetHandler);
            System.out.println(article);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

自定义 Handler

public class ArticleHandler extends BeanHandler<Article> {

    public ArticleHandler() {
        super(Article.class);
    }
}
public class Test1 {

    public static final String DRIVER = "com.mysql.jdbc.Driver";

    public static final String DB_URL = "jdbc:mysql://localhost:3306/auth";

    public static final String USERNAME = "root";

    public static final String PASSWORD = "123456";

    public static void main(String[] args) {
        QueryRunner queryRunner = new QueryRunner();
        try {
            Class.forName(DRIVER);
            Connection connection = DriverManager.getConnection(DB_URL, USERNAME, PASSWORD);
            //ResultSetHandler<Article> resultSetHandler = new BeanHandler<>(Article.class);
            ArticleHandler articleHandler = new ArticleHandler();
            String sql = "select * from blog limit 1";
            Article article = queryRunner.query(connection, sql, articleHandler);
            System.out.println(article);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

使用DBCP数据源

引入dbcp依赖

public class DataSourceUtils {

    private static final BasicDataSource basicDataSource;

    public static final String DRIVER = "com.mysql.jdbc.Driver";

    public static final String DB_URL = "jdbc:mysql://localhost:3306/auth";

    public static final String USERNAME = "root";

    public static final String PASSWORD = "123456";

    static {
        basicDataSource = new BasicDataSource();
        basicDataSource.setDriverClassName(DRIVER);
        basicDataSource.setUrl(DB_URL);
        basicDataSource.setUsername(USERNAME);
        basicDataSource.setPassword(PASSWORD);
    }

    public static DataSource getInstance() {
        return basicDataSource;
    }
}
public class Test1 {

    public static final String DRIVER = "com.mysql.jdbc.Driver";

    public static final String DB_URL = "jdbc:mysql://localhost:3306/auth";

    public static final String USERNAME = "root";

    public static final String PASSWORD = "123456";

    public static void main(String[] args) {
        QueryRunner queryRunner = new QueryRunner(DataSourceUtils.getInstance());
        try {
           // Class.forName(DRIVER);
            //Connection connection = DriverManager.getConnection(DB_URL, USERNAME, PASSWORD);
            //ResultSetHandler<Article> resultSetHandler = new BeanHandler<>(Article.class);
            ArticleHandler articleHandler = new ArticleHandler();
            String sql = "select * from blog limit 1";
            // Article article = queryRunner.query(connection, sql, articleHandler);
            Article article = queryRunner.query(sql, articleHandler);
            System.out.println(article);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

设计

struct.jpg

QueryRunner : 执行SQL

ResultSetHandler:处理结果集

添加新评论