[BAEL-14274] - Fixed article code for https://www.baeldung.com/spring-jdbc-jdbctemplate
This commit is contained in:
parent
9d7d7b4dde
commit
0dc22fbc7a
|
@ -16,6 +16,7 @@ import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
|
||||||
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
|
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
|
||||||
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
|
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
|
||||||
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
|
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
|
||||||
|
import org.springframework.jdbc.core.simple.SimpleJdbcCall;
|
||||||
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
|
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
@ -28,6 +29,8 @@ public class EmployeeDAO {
|
||||||
|
|
||||||
private SimpleJdbcInsert simpleJdbcInsert;
|
private SimpleJdbcInsert simpleJdbcInsert;
|
||||||
|
|
||||||
|
private SimpleJdbcCall simpleJdbcCall;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public void setDataSource(final DataSource dataSource) {
|
public void setDataSource(final DataSource dataSource) {
|
||||||
jdbcTemplate = new JdbcTemplate(dataSource);
|
jdbcTemplate = new JdbcTemplate(dataSource);
|
||||||
|
@ -37,6 +40,8 @@ public class EmployeeDAO {
|
||||||
namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
|
namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
|
||||||
simpleJdbcInsert = new SimpleJdbcInsert(dataSource).withTableName("EMPLOYEE");
|
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() {
|
public int getCountOfEmployees() {
|
||||||
|
@ -110,4 +115,15 @@ public class EmployeeDAO {
|
||||||
final int[] updateCounts = namedParameterJdbcTemplate.batchUpdate("INSERT INTO EMPLOYEE VALUES (:id, :firstName, :lastName, :address)", batch);
|
final int[] updateCounts = namedParameterJdbcTemplate.batchUpdate("INSERT INTO EMPLOYEE VALUES (:id, :firstName, :lastName, :address)", batch);
|
||||||
return updateCounts;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue