BAEL-970 A Guide to Apache Commons DbUtils (#2125)
* BAEL-970 A Guide to Apache Commons DbUtils * BAEL-970 A Guide to Apache Commons DbUtils * BAEL-970 A Guide to Apache Commons DbUtils - Added employeeId to Email class - Minor corrections
This commit is contained in:
committed by
yetanotherallisonf
parent
1b9353c83e
commit
9c643cd652
@@ -0,0 +1,38 @@
|
||||
package com.baeldung.commons.dbutils;
|
||||
|
||||
public class Email {
|
||||
private Integer id;
|
||||
private Integer employeeId;
|
||||
private String address;
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Integer getEmployeeId() {
|
||||
return employeeId;
|
||||
}
|
||||
|
||||
public void setEmployeeId(Integer employeeId) {
|
||||
this.employeeId = employeeId;
|
||||
}
|
||||
|
||||
|
||||
public String getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(String address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Email{" + "id=" + id + ", address=" + address + '}';
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package com.baeldung.commons.dbutils;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
public class Employee {
|
||||
private Integer id;
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
private Double salary;
|
||||
private Date hiredDate;
|
||||
private List<Email> emails;
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public Double getSalary() {
|
||||
return salary;
|
||||
}
|
||||
|
||||
public void setSalary(Double salary) {
|
||||
this.salary = salary;
|
||||
}
|
||||
|
||||
public Date getHiredDate() {
|
||||
return hiredDate;
|
||||
}
|
||||
|
||||
public void setHiredDate(Date hiredDate) {
|
||||
this.hiredDate = hiredDate;
|
||||
}
|
||||
|
||||
public List<Email> getEmails() {
|
||||
return emails;
|
||||
}
|
||||
|
||||
public void setEmails(List<Email> emails) {
|
||||
this.emails = emails;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Employee{" + "id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", salary=" + salary + ", hiredDate=" + hiredDate + '}';
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.baeldung.commons.dbutils;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import org.apache.commons.dbutils.BasicRowProcessor;
|
||||
import org.apache.commons.dbutils.BeanProcessor;
|
||||
|
||||
import org.apache.commons.dbutils.QueryRunner;
|
||||
import org.apache.commons.dbutils.handlers.BeanListHandler;
|
||||
|
||||
public class EmployeeHandler extends BeanListHandler<Employee> {
|
||||
|
||||
private Connection connection;
|
||||
|
||||
public EmployeeHandler(Connection con) {
|
||||
super(Employee.class, new BasicRowProcessor(new BeanProcessor(getColumnsToFieldsMap())));
|
||||
this.connection = con;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Employee> handle(ResultSet rs) throws SQLException {
|
||||
List<Employee> employees = super.handle(rs);
|
||||
|
||||
QueryRunner runner = new QueryRunner();
|
||||
BeanListHandler<Email> handler = new BeanListHandler<>(Email.class);
|
||||
String query = "SELECT * FROM email WHERE employeeid = ?";
|
||||
for (Employee employee : employees) {
|
||||
List<Email> emails = runner.query(connection, query, handler, employee.getId());
|
||||
employee.setEmails(emails);
|
||||
}
|
||||
return employees;
|
||||
}
|
||||
|
||||
public static Map<String, String> getColumnsToFieldsMap() {
|
||||
Map<String, String> columnsToFieldsMap = new HashMap<>();
|
||||
columnsToFieldsMap.put("FIRST_NAME", "firstName");
|
||||
columnsToFieldsMap.put("LAST_NAME", "lastName");
|
||||
columnsToFieldsMap.put("HIRED_DATE", "hiredDate");
|
||||
return columnsToFieldsMap;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user