minor cleanup formatting work

This commit is contained in:
eugenp 2015-01-24 15:50:51 +02:00
parent 07f20a87bf
commit 4ad6cc93f2
6 changed files with 134 additions and 165 deletions

View File

@ -6,18 +6,14 @@ import org.springframework.dao.DataAccessException;
import org.springframework.dao.DuplicateKeyException; import org.springframework.dao.DuplicateKeyException;
import org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator; import org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator;
public class CustomSQLErrorCodeTranslator extends public class CustomSQLErrorCodeTranslator extends SQLErrorCodeSQLExceptionTranslator {
SQLErrorCodeSQLExceptionTranslator {
@Override @Override
protected DataAccessException customTranslate(final String task, protected DataAccessException customTranslate(final String task, final String sql, final SQLException sqlException) {
final String sql, final SQLException sqlException) { if (sqlException.getErrorCode() == -104) {
if (sqlException.getErrorCode() == -104) { return new DuplicateKeyException("Custome Exception translator - Integrity contraint voilation.", sqlException);
return new DuplicateKeyException( }
"Custome Exception translator - Integrity contraint voilation.", return null;
sqlException);
}
return null;
} }
} }

View File

@ -10,35 +10,35 @@ public class Employee {
private String address; private String address;
public int getId() { public int getId() {
return id; return id;
} }
public void setId(final int id) { public void setId(final int id) {
this.id = id; this.id = id;
} }
public String getFirstName() { public String getFirstName() {
return firstName; return firstName;
} }
public void setFirstName(final String firstName) { public void setFirstName(final String firstName) {
this.firstName = firstName; this.firstName = firstName;
} }
public String getLastName() { public String getLastName() {
return lastName; return lastName;
} }
public void setLastName(final String lastName) { public void setLastName(final String lastName) {
this.lastName = lastName; this.lastName = lastName;
} }
public String getAddress() { public String getAddress() {
return address; return address;
} }
public void setAddress(final String address) { public void setAddress(final String address) {
this.address = address; this.address = address;
} }
} }

View File

@ -30,103 +30,84 @@ public class EmployeeDAO {
@Autowired @Autowired
public void setDataSource(final DataSource dataSource) { public void setDataSource(final DataSource dataSource) {
jdbcTemplate = new JdbcTemplate(dataSource); jdbcTemplate = new JdbcTemplate(dataSource);
final CustomSQLErrorCodeTranslator customSQLErrorCodeTranslator = new CustomSQLErrorCodeTranslator(); final CustomSQLErrorCodeTranslator customSQLErrorCodeTranslator = new CustomSQLErrorCodeTranslator();
jdbcTemplate.setExceptionTranslator(customSQLErrorCodeTranslator); jdbcTemplate.setExceptionTranslator(customSQLErrorCodeTranslator);
namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource); namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
simpleJdbcInsert = new SimpleJdbcInsert(dataSource) simpleJdbcInsert = new SimpleJdbcInsert(dataSource).withTableName("EMPLOYEE");
.withTableName("EMPLOYEE");
} }
public int getCountOfEmployees() { public int getCountOfEmployees() {
return jdbcTemplate.queryForObject("SELECT COUNT(*) FROM EMPLOYEE", return jdbcTemplate.queryForObject("SELECT COUNT(*) FROM EMPLOYEE", Integer.class);
Integer.class);
} }
public List<Employee> getAllEmployees() { public List<Employee> getAllEmployees() {
return jdbcTemplate.query("SELECT * FROM EMPLOYEE", return jdbcTemplate.query("SELECT * FROM EMPLOYEE", new EmployeeRowMapper());
new EmployeeRowMapper());
} }
public int addEmplyee(final int id) { public int addEmplyee(final int id) {
return jdbcTemplate.update("INSERT INTO EMPLOYEE VALUES (?, ?, ?, ?)", return jdbcTemplate.update("INSERT INTO EMPLOYEE VALUES (?, ?, ?, ?)", id, "Bill", "Gates", "USA");
id, "Bill", "Gates", "USA");
} }
public int addEmplyeeUsingSimpelJdbcInsert(final Employee emp) { public int addEmplyeeUsingSimpelJdbcInsert(final Employee emp) {
final Map<String, Object> parameters = new HashMap<String, Object>(); final Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("ID", emp.getId()); parameters.put("ID", emp.getId());
parameters.put("FIRST_NAME", emp.getFirstName()); parameters.put("FIRST_NAME", emp.getFirstName());
parameters.put("LAST_NAME", emp.getLastName()); parameters.put("LAST_NAME", emp.getLastName());
parameters.put("ADDRESS", emp.getAddress()); parameters.put("ADDRESS", emp.getAddress());
return simpleJdbcInsert.execute(parameters); return simpleJdbcInsert.execute(parameters);
} }
public Employee getEmployee(final int id) { public Employee getEmployee(final int id) {
final String query = "SELECT * FROM EMPLOYEE WHERE ID = ?"; final String query = "SELECT * FROM EMPLOYEE WHERE ID = ?";
return jdbcTemplate.queryForObject(query, new Object[] { id }, return jdbcTemplate.queryForObject(query, new Object[] { id }, new EmployeeRowMapper());
new EmployeeRowMapper());
} }
public void addEmplyeeUsingExecuteMethod() { public void addEmplyeeUsingExecuteMethod() {
jdbcTemplate jdbcTemplate.execute("INSERT INTO EMPLOYEE VALUES (6, 'Bill', 'Gates', 'USA')");
.execute("INSERT INTO EMPLOYEE VALUES (6, 'Bill', 'Gates', 'USA')");
} }
public String getEmployeeUsingMapSqlParameterSource() { public String getEmployeeUsingMapSqlParameterSource() {
final SqlParameterSource namedParameters = new MapSqlParameterSource() final SqlParameterSource namedParameters = new MapSqlParameterSource().addValue("id", 1);
.addValue("id", 1);
return namedParameterJdbcTemplate.queryForObject( return namedParameterJdbcTemplate.queryForObject("SELECT FIRST_NAME FROM EMPLOYEE WHERE ID = :id", namedParameters, String.class);
"SELECT FIRST_NAME FROM EMPLOYEE WHERE ID = :id",
namedParameters, String.class);
} }
public int getEmployeeUsingBeanPropertySqlParameterSource() { public int getEmployeeUsingBeanPropertySqlParameterSource() {
final Employee employee = new Employee(); final Employee employee = new Employee();
employee.setFirstName("James"); employee.setFirstName("James");
final String SELECT_BY_ID = "SELECT COUNT(*) FROM EMPLOYEE WHERE FIRST_NAME = :firstName"; final String SELECT_BY_ID = "SELECT COUNT(*) FROM EMPLOYEE WHERE FIRST_NAME = :firstName";
final SqlParameterSource namedParameters = new BeanPropertySqlParameterSource( final SqlParameterSource namedParameters = new BeanPropertySqlParameterSource(employee);
employee);
return namedParameterJdbcTemplate.queryForObject(SELECT_BY_ID, return namedParameterJdbcTemplate.queryForObject(SELECT_BY_ID, namedParameters, Integer.class);
namedParameters, Integer.class);
} }
public int[] batchUpdateUsingJDBCTemplate(final List<Employee> employees) { public int[] batchUpdateUsingJDBCTemplate(final List<Employee> employees) {
return jdbcTemplate.batchUpdate( return jdbcTemplate.batchUpdate("INSERT INTO EMPLOYEE VALUES (?, ?, ?, ?)", new BatchPreparedStatementSetter() {
"INSERT INTO EMPLOYEE VALUES (?, ?, ?, ?)",
new BatchPreparedStatementSetter() {
@Override @Override
public void setValues(final PreparedStatement ps, public void setValues(final PreparedStatement ps, final int i) throws SQLException {
final int i) throws SQLException { ps.setInt(1, employees.get(i).getId());
ps.setInt(1, employees.get(i).getId()); ps.setString(2, employees.get(i).getFirstName());
ps.setString(2, employees.get(i).getFirstName()); ps.setString(3, employees.get(i).getLastName());
ps.setString(3, employees.get(i).getLastName()); ps.setString(4, employees.get(i).getAddress());
ps.setString(4, employees.get(i).getAddress()); }
}
@Override @Override
public int getBatchSize() { public int getBatchSize() {
return 3; return 3;
} }
}); });
} }
public int[] batchUpdateUsingNamedParameterJDBCTemplate( public int[] batchUpdateUsingNamedParameterJDBCTemplate(final List<Employee> employees) {
final List<Employee> employees) { final SqlParameterSource[] batch = SqlParameterSourceUtils.createBatch(employees.toArray());
final SqlParameterSource[] batch = SqlParameterSourceUtils final int[] updateCounts = namedParameterJdbcTemplate.batchUpdate("INSERT INTO EMPLOYEE VALUES (:id, :firstName, :lastName, :address)", batch);
.createBatch(employees.toArray()); return updateCounts;
final int[] updateCounts = namedParameterJdbcTemplate
.batchUpdate(
"INSERT INTO EMPLOYEE VALUES (:id, :firstName, :lastName, :address)",
batch);
return updateCounts;
} }
} }

View File

@ -8,15 +8,14 @@ import org.springframework.jdbc.core.RowMapper;
public class EmployeeRowMapper implements RowMapper<Employee> { public class EmployeeRowMapper implements RowMapper<Employee> {
@Override @Override
public Employee mapRow(final ResultSet rs, final int rowNum) public Employee mapRow(final ResultSet rs, final int rowNum) throws SQLException {
throws SQLException { final Employee employee = new Employee();
final Employee employee = new Employee();
employee.setId(rs.getInt("ID")); employee.setId(rs.getInt("ID"));
employee.setFirstName(rs.getString("FIRST_NAME")); employee.setFirstName(rs.getString("FIRST_NAME"));
employee.setLastName(rs.getString("LAST_NAME")); employee.setLastName(rs.getString("LAST_NAME"));
employee.setAddress(rs.getString("ADDRESS")); employee.setAddress(rs.getString("ADDRESS"));
return employee; return employee;
} }
} }

View File

@ -15,20 +15,18 @@ public class SpringJdbcConfig {
@Bean @Bean
public DataSource dataSource() { public DataSource dataSource() {
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL) return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL).addScript("classpath:jdbc/schema.sql").addScript("classpath:jdbc/test-data.sql").build();
.addScript("classpath:jdbc/schema.sql")
.addScript("classpath:jdbc/test-data.sql").build();
} }
// @Bean // @Bean
public DataSource mysqlDataSource() { public DataSource mysqlDataSource() {
final DriverManagerDataSource dataSource = new DriverManagerDataSource(); final DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/springjdbc"); dataSource.setUrl("jdbc:mysql://localhost:3306/springjdbc");
dataSource.setUsername("guest_user"); dataSource.setUsername("guest_user");
dataSource.setPassword("guest_password"); dataSource.setPassword("guest_password");
return dataSource; return dataSource;
} }
} }

View File

@ -22,123 +22,118 @@ public class EmployeeDAOTest {
@Test @Test
public void testGetCountOfEmployees() { public void testGetCountOfEmployees() {
Assert.assertEquals(employeeDao.getCountOfEmployees(), 9); Assert.assertEquals(employeeDao.getCountOfEmployees(), 9);
} }
@Test @Test
public void testQueryMethod() { public void testQueryMethod() {
Assert.assertEquals(employeeDao.getAllEmployees().size(), 4); Assert.assertEquals(employeeDao.getAllEmployees().size(), 4);
} }
@Test @Test
public void testUpdateMethod() { public void testUpdateMethod() {
Assert.assertEquals(employeeDao.addEmplyee(5), 1); Assert.assertEquals(employeeDao.addEmplyee(5), 1);
} }
@Test @Test
public void testAddEmployeeUsingSimpelJdbcInsert() { public void testAddEmployeeUsingSimpelJdbcInsert() {
final Employee emp = new Employee(); final Employee emp = new Employee();
emp.setId(11); emp.setId(11);
emp.setFirstName("testFirstName"); emp.setFirstName("testFirstName");
emp.setLastName("testLastName"); emp.setLastName("testLastName");
emp.setAddress("testAddress"); emp.setAddress("testAddress");
Assert.assertEquals(employeeDao.addEmplyeeUsingSimpelJdbcInsert(emp), 1); Assert.assertEquals(employeeDao.addEmplyeeUsingSimpelJdbcInsert(emp), 1);
} }
@Test @Test
public void testExecuteMethod() { public void testExecuteMethod() {
employeeDao.addEmplyeeUsingExecuteMethod(); employeeDao.addEmplyeeUsingExecuteMethod();
} }
@Test @Test
public void testMapSqlParameterSource() { public void testMapSqlParameterSource() {
Assert.assertEquals("James", Assert.assertEquals("James", employeeDao.getEmployeeUsingMapSqlParameterSource());
employeeDao.getEmployeeUsingMapSqlParameterSource());
} }
@Test @Test
public void testBeanPropertySqlParameterSource() { public void testBeanPropertySqlParameterSource() {
Assert.assertEquals(1, Assert.assertEquals(1, employeeDao.getEmployeeUsingBeanPropertySqlParameterSource());
employeeDao.getEmployeeUsingBeanPropertySqlParameterSource());
} }
@Test @Test
public void testCustomExceptionTranslator() { public void testCustomExceptionTranslator() {
employeeDao.addEmplyee(7); employeeDao.addEmplyee(7);
try { try {
employeeDao.addEmplyee(7); employeeDao.addEmplyee(7);
} catch (final DuplicateKeyException e) { } catch (final DuplicateKeyException e) {
System.out.println(e.getMessage()); System.out.println(e.getMessage());
Assert.assertTrue(e Assert.assertTrue(e.getMessage().contains("Custome Exception translator - Integrity contraint voilation."));
.getMessage() }
.contains(
"Custome Exception translator - Integrity contraint voilation."));
}
} }
@Test @Test
public void testBatchUpdateUsingJDBCTemplate() { public void testBatchUpdateUsingJDBCTemplate() {
final List<Employee> employees = new ArrayList<Employee>(); final List<Employee> employees = new ArrayList<Employee>();
final Employee emp1 = new Employee(); final Employee emp1 = new Employee();
emp1.setId(10); emp1.setId(10);
emp1.setFirstName("firstName1"); emp1.setFirstName("firstName1");
emp1.setLastName("lastName1"); emp1.setLastName("lastName1");
emp1.setAddress("address1"); emp1.setAddress("address1");
final Employee emp2 = new Employee(); final Employee emp2 = new Employee();
emp2.setId(20); emp2.setId(20);
emp2.setFirstName("firstName2"); emp2.setFirstName("firstName2");
emp2.setLastName("lastName2"); emp2.setLastName("lastName2");
emp2.setAddress("address2"); emp2.setAddress("address2");
final Employee emp3 = new Employee(); final Employee emp3 = new Employee();
emp3.setId(30); emp3.setId(30);
emp3.setFirstName("firstName3"); emp3.setFirstName("firstName3");
emp3.setLastName("lastName3"); emp3.setLastName("lastName3");
emp3.setAddress("address3"); emp3.setAddress("address3");
employees.add(emp1); employees.add(emp1);
employees.add(emp2); employees.add(emp2);
employees.add(emp3); employees.add(emp3);
employeeDao.batchUpdateUsingJDBCTemplate(employees); employeeDao.batchUpdateUsingJDBCTemplate(employees);
Assert.assertTrue(employeeDao.getEmployee(10) != null); Assert.assertTrue(employeeDao.getEmployee(10) != null);
Assert.assertTrue(employeeDao.getEmployee(20) != null); Assert.assertTrue(employeeDao.getEmployee(20) != null);
Assert.assertTrue(employeeDao.getEmployee(30) != null); Assert.assertTrue(employeeDao.getEmployee(30) != null);
} }
@Test @Test
public void testBatchUpdateUsingNamedParameterJDBCTemplate() { public void testBatchUpdateUsingNamedParameterJDBCTemplate() {
final List<Employee> employees = new ArrayList<Employee>(); final List<Employee> employees = new ArrayList<Employee>();
final Employee emp1 = new Employee(); final Employee emp1 = new Employee();
emp1.setId(40); emp1.setId(40);
emp1.setFirstName("firstName4"); emp1.setFirstName("firstName4");
emp1.setLastName("lastName4"); emp1.setLastName("lastName4");
emp1.setAddress("address4"); emp1.setAddress("address4");
final Employee emp2 = new Employee(); final Employee emp2 = new Employee();
emp2.setId(50); emp2.setId(50);
emp2.setFirstName("firstName5"); emp2.setFirstName("firstName5");
emp2.setLastName("lastName5"); emp2.setLastName("lastName5");
emp2.setAddress("address5"); emp2.setAddress("address5");
final Employee emp3 = new Employee(); final Employee emp3 = new Employee();
emp3.setId(60); emp3.setId(60);
emp3.setFirstName("firstName6"); emp3.setFirstName("firstName6");
emp3.setLastName("lastName6"); emp3.setLastName("lastName6");
emp3.setAddress("address6"); emp3.setAddress("address6");
employees.add(emp1); employees.add(emp1);
employees.add(emp2); employees.add(emp2);
employees.add(emp3); employees.add(emp3);
employeeDao.batchUpdateUsingNamedParameterJDBCTemplate(employees); employeeDao.batchUpdateUsingNamedParameterJDBCTemplate(employees);
Assert.assertTrue(employeeDao.getEmployee(40) != null); Assert.assertTrue(employeeDao.getEmployee(40) != null);
Assert.assertTrue(employeeDao.getEmployee(50) != null); Assert.assertTrue(employeeDao.getEmployee(50) != null);
Assert.assertTrue(employeeDao.getEmployee(60) != null); Assert.assertTrue(employeeDao.getEmployee(60) != null);
} }
} }