From c68c7648a3361ee26f35356e618ddc9ab189d3f0 Mon Sep 17 00:00:00 2001 From: Gang Wu Date: Sat, 18 Apr 2020 12:48:37 -0600 Subject: [PATCH] BAEL-3961 Remove address field of Employee class. --- .../main/java/com/baeldung/jdbc/Employee.java | 17 ++++------------- .../java/com/baeldung/jdbc/EmployeeDAO.java | 11 ++++++----- .../src/main/resources/jdbc/schema.sql | 3 +-- .../src/main/resources/jdbc/test-data.sql | 8 ++++---- 4 files changed, 15 insertions(+), 24 deletions(-) diff --git a/persistence-modules/spring-persistence-simple-2/src/main/java/com/baeldung/jdbc/Employee.java b/persistence-modules/spring-persistence-simple-2/src/main/java/com/baeldung/jdbc/Employee.java index d5f87ca3df..bd6fe0fb15 100644 --- a/persistence-modules/spring-persistence-simple-2/src/main/java/com/baeldung/jdbc/Employee.java +++ b/persistence-modules/spring-persistence-simple-2/src/main/java/com/baeldung/jdbc/Employee.java @@ -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; - } } diff --git a/persistence-modules/spring-persistence-simple-2/src/main/java/com/baeldung/jdbc/EmployeeDAO.java b/persistence-modules/spring-persistence-simple-2/src/main/java/com/baeldung/jdbc/EmployeeDAO.java index 25d280ad12..8637f6f851 100644 --- a/persistence-modules/spring-persistence-simple-2/src/main/java/com/baeldung/jdbc/EmployeeDAO.java +++ b/persistence-modules/spring-persistence-simple-2/src/main/java/com/baeldung/jdbc/EmployeeDAO.java @@ -30,23 +30,22 @@ public class EmployeeDAO { SqlParameterSource parameters = new MapSqlParameterSource("ids", ids); List 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 getEmployeesFromIdList(List ids) { String inSql = String.join(",", Collections.nCopies(ids.size(), "?")); - List employees = jdbcTemplate.query("SELECT * FROM EMPLOYEE WHERE id IN (" + inSql +")", + List 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 getEmployeesFromLargeIdList(List ids) { jdbcTemplate.execute("CREATE TEMPORARY TABLE IF NOT EXISTS employee_tmp (id INT NOT NULL)"); - jdbcTemplate.update("DELETE FROM employee_tmp"); List employeeIds = new ArrayList<>(); for (Integer id : ids) { @@ -55,7 +54,9 @@ public class EmployeeDAO { jdbcTemplate.batchUpdate("INSERT INTO employee_tmp VALUES(?)", employeeIds); List 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; } diff --git a/persistence-modules/spring-persistence-simple-2/src/main/resources/jdbc/schema.sql b/persistence-modules/spring-persistence-simple-2/src/main/resources/jdbc/schema.sql index 3401c5d963..be102431ca 100644 --- a/persistence-modules/spring-persistence-simple-2/src/main/resources/jdbc/schema.sql +++ b/persistence-modules/spring-persistence-simple-2/src/main/resources/jdbc/schema.sql @@ -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) ); \ No newline at end of file diff --git a/persistence-modules/spring-persistence-simple-2/src/main/resources/jdbc/test-data.sql b/persistence-modules/spring-persistence-simple-2/src/main/resources/jdbc/test-data.sql index c1669c156d..5421c09849 100644 --- a/persistence-modules/spring-persistence-simple-2/src/main/resources/jdbc/test-data.sql +++ b/persistence-modules/spring-persistence-simple-2/src/main/resources/jdbc/test-data.sql @@ -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'); \ No newline at end of file +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'); \ No newline at end of file