自定义BasicRowProcessor
之前我们使用BeanHandler封装结果集的时候,默认是表的列名和JavaBean的属性名是一致的。但是,如果表中的列名和JavaBean的属性名不一致的情况,则可以使用自定义的BasicRowProcessor对象来映射它们。
1 自定义BasicRowProcessor的示例
1.1 编写Customer实体类
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;
}
}
1.2 编写自定义BeanHandler
CustomerHandler:
package com.yiidian.dbutils;
import com.yiidian.domain.Customer;
import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.BeanProcessor;
import org.apache.commons.dbutils.handlers.BeanHandler;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
/**
* 一点教程网 - http://www.yiidian.com
*/
public class CustomerHandler extends BeanHandler<Customer>{
public CustomerHandler() {
super(Customer.class, new BasicRowProcessor(new BeanProcessor(mapColumnsToFields())));
}
@Override
public Customer handle(ResultSet rs) throws SQLException {
Customer customer = super.handle(rs);
customer.setName("一点教程网:"+customer.getName());
return customer;
}
public static Map<String, String> mapColumnsToFields() {
Map<String, String> columnsToFieldsMap = new HashMap<>();
/**
* key:表的列名称
* value:JavaBean的属性名
*/
columnsToFieldsMap.put("ID", "id");
columnsToFieldsMap.put("NAME", "name");
return columnsToFieldsMap;
}
}
1.3 编写核心类
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.ArrayListHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import java.sql.*;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
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);
CustomerHandler customerHandler = new CustomerHandler();
try {
Customer customer = queryRunner.query(conn, "SELECT * FROM customer WHERE name=?",
customerHandler, "张三");
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);
}
}
}
1.4 运行测试
热门文章
优秀文章