DBUtils中BeanListHandler接口的使用

2020-03-28 16:02:18来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

DBUtils中BeanListHandler接口的使用

原文链接:http://www.yiidian.com/dbutils/dbutils-beanlisthandler.html

org.apache.commons.dbutils.BeanListHandler是ResultSetHandler接口的实现,并负责ResultSet结果级的所有记录转换成JavaBean的List集合。此类是线程安全的。

1 BeanListHandler的语法

List<Customer> custList= queryRunner.query(conn, "SELECT * FROM customer", resultHandler); 

2 BeanListHandler的示例

2.1 编写Customer实体类

package com.yiidian.domain;

/**
 * 一点教程网 - http://www.yiidian.com
 */
public class Customer {
    private Integer id;
    private String name;
    private String gender;
    private String telephone;
    private String address;

    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 getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

2.2 编写核心类

MainApp:

package com.yiidian.dbutils;

import com.yiidian.domain.Customer;
import org.apache.commons.dbutils.AsyncQueryRunner;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.*;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

/**
 * 一点教程网 - http://www.yiidian.com
 */
public class MainApp {
    // 驱动程序
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    // URL连接
    static final String DB_URL = "jdbc:mysql://localhost:3306/test";

    //数据库信息
    static final String USER = "root";
    static final String PASS = "root";

    public static void main(String[] args) throws SQLException {
        Connection conn = null;

        QueryRunner queryRunner = new QueryRunner();

        DbUtils.loadDriver(JDBC_DRIVER);

        conn = DriverManager.getConnection(DB_URL, USER, PASS);

        ResultSetHandler<List<Customer>> resultHandler = new BeanListHandler<Customer>(Customer.class);

        try {
            List<Customer> custList = queryRunner.query(conn, "SELECT * FROM customer", resultHandler);
            for(Customer customer: custList ) {

                System.out.print("编号: " + customer.getId());
                System.out.print(", 用户名: " + customer.getName());
                System.out.print(", 性别: " + customer.getGender());
                System.out.print(", 联系电话: " + customer.getTelephone());
                System.out.println(", 住址: " + customer.getAddress());
            }
        } finally {
            DbUtils.close(conn);
        }
    }
}

2.3 运行测试

file

file

欢迎关注我的公众号::一点教程。获得独家整理的学习资源和日常干货推送。
如果您对我的系列教程感兴趣,也可以关注我的网站:yiidian.com


原文链接:https://www.cnblogs.com/yiidian/p/12585672.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:javaEE常用开源框架的认识及概述

下一篇:SpringCloud2020精彩视频资源!!!