Skip to content

Commit

Permalink
Merge pull request #7179 from amit2103/BAEL-14274-12
Browse files Browse the repository at this point in the history
[BAEL-14274] - Fixed article code for https://www.baeldung.com/spring…
  • Loading branch information
lor6 committed Jul 17, 2019
2 parents fdf904f + 0dc22fb commit fdc96b0
Showing 1 changed file with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.jdbc.core.simple.SimpleJdbcCall;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.stereotype.Repository;

Expand All @@ -27,6 +28,8 @@ public class EmployeeDAO {
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

private SimpleJdbcInsert simpleJdbcInsert;

private SimpleJdbcCall simpleJdbcCall;

@Autowired
public void setDataSource(final DataSource dataSource) {
Expand All @@ -36,7 +39,9 @@ public void setDataSource(final DataSource dataSource) {

namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
simpleJdbcInsert = new SimpleJdbcInsert(dataSource).withTableName("EMPLOYEE");


// Commented as the database is H2, change the database and create procedure READ_EMPLOYEE before calling getEmployeeUsingSimpleJdbcCall
//simpleJdbcCall = new SimpleJdbcCall(dataSource).withProcedureName("READ_EMPLOYEE");
}

public int getCountOfEmployees() {
Expand Down Expand Up @@ -110,4 +115,15 @@ public int[] batchUpdateUsingNamedParameterJDBCTemplate(final List<Employee> emp
final int[] updateCounts = namedParameterJdbcTemplate.batchUpdate("INSERT INTO EMPLOYEE VALUES (:id, :firstName, :lastName, :address)", batch);
return updateCounts;
}

public Employee getEmployeeUsingSimpleJdbcCall(int id) {
SqlParameterSource in = new MapSqlParameterSource().addValue("in_id", id);
Map<String, Object> out = simpleJdbcCall.execute(in);

Employee emp = new Employee();
emp.setFirstName((String) out.get("FIRST_NAME"));
emp.setLastName((String) out.get("LAST_NAME"));

return emp;
}
}

0 comments on commit fdc96b0

Please sign in to comment.