BAEL-3961 Remove address field of Employee class.

This commit is contained in:
Gang Wu 2020-04-18 12:48:37 -06:00
parent 78e180f35d
commit c68c7648a3
4 changed files with 15 additions and 24 deletions

View File

@ -7,13 +7,11 @@ public class Employee {
private String lastName;
private String address;
public Employee(int id, String firstName, String lastName, String address) {
setId(id);
setFirstName(firstName);
setLastName(lastName);
setAddress(address);
public Employee(int id, String firstName, String lastName) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
public int getId() {
@ -40,12 +38,5 @@ public class Employee {
this.lastName = lastName;
}
public String getAddress() {
return address;
}
public void setAddress(final String address) {
this.address = address;
}
}

View File

@ -30,23 +30,22 @@ public class EmployeeDAO {
SqlParameterSource parameters = new MapSqlParameterSource("ids", ids);
List<Employee> employees = namedJdbcTemplate.query("SELECT * FROM EMPLOYEE WHERE id IN (:ids)",
parameters,
(rs, rowNum) -> new Employee(rs.getInt("id"), rs.getString("first_name"), rs.getString("last_name"), rs.getString("address")));
(rs, rowNum) -> new Employee(rs.getInt("id"), rs.getString("first_name"), rs.getString("last_name")));
return employees;
}
public List<Employee> getEmployeesFromIdList(List<Integer> ids) {
String inSql = String.join(",", Collections.nCopies(ids.size(), "?"));
List<Employee> employees = jdbcTemplate.query("SELECT * FROM EMPLOYEE WHERE id IN (" + inSql +")",
List<Employee> employees = jdbcTemplate.query(String.format("SELECT * FROM EMPLOYEE WHERE id IN (%s)", inSql),
ids.toArray(),
(rs, rowNum) -> new Employee(rs.getInt("id"), rs.getString("first_name"), rs.getString("last_name"), rs.getString("address")));
(rs, rowNum) -> new Employee(rs.getInt("id"), rs.getString("first_name"), rs.getString("last_name")));
return employees;
}
public List<Employee> getEmployeesFromLargeIdList(List<Integer> ids) {
jdbcTemplate.execute("CREATE TEMPORARY TABLE IF NOT EXISTS employee_tmp (id INT NOT NULL)");
jdbcTemplate.update("DELETE FROM employee_tmp");
List<Object[]> employeeIds = new ArrayList<>();
for (Integer id : ids) {
@ -55,7 +54,9 @@ public class EmployeeDAO {
jdbcTemplate.batchUpdate("INSERT INTO employee_tmp VALUES(?)", employeeIds);
List<Employee> employees = jdbcTemplate.query("SELECT * FROM EMPLOYEE WHERE id IN (SELECT id FROM employee_tmp)",
(rs, rowNum) -> new Employee(rs.getInt("id"), rs.getString("first_name"), rs.getString("last_name"), rs.getString("address")));
(rs, rowNum) -> new Employee(rs.getInt("id"), rs.getString("first_name"), rs.getString("last_name")));
jdbcTemplate.update("DELETE FROM employee_tmp");
return employees;
}

View File

@ -2,6 +2,5 @@ CREATE TABLE EMPLOYEE
(
ID int NOT NULL PRIMARY KEY,
FIRST_NAME varchar(255),
LAST_NAME varchar(255),
ADDRESS varchar(255)
LAST_NAME varchar(255)
);

View File

@ -1,4 +1,4 @@
INSERT INTO EMPLOYEE VALUES (1, 'James', 'Gosling', 'Canada');
INSERT INTO EMPLOYEE VALUES (2, 'Donald', 'Knuth', 'USA');
INSERT INTO EMPLOYEE VALUES (3, 'Linus', 'Torvalds', 'Finland');
INSERT INTO EMPLOYEE VALUES (4, 'Dennis', 'Ritchie', 'USA');
INSERT INTO EMPLOYEE VALUES (1, 'James', 'Gosling');
INSERT INTO EMPLOYEE VALUES (2, 'Donald', 'Knuth');
INSERT INTO EMPLOYEE VALUES (3, 'Linus', 'Torvalds');
INSERT INTO EMPLOYEE VALUES (4, 'Dennis', 'Ritchie');