[JAVA-2306] Moved articles from spring-persistence-simple
* https://www.baeldung.com/the-persistence-layer-with-spring-and-jpa went into spring-jpa-2 * https://www.baeldung.com/hibernate-5-spring went to spring-jpa-2 * https://www.baeldung.com/transaction-configuration-with-jpa-and-spring went to spring-jpa-2 * https://www.baeldung.com/persistence-layer-with-spring-and-hibernate went to spring-jpa-2 * https://www.baeldung.com/simplifying-the-data-access-layer-with-spring-and-java-generics went to spring-jpa-2 * https://www.baeldung.com/the-persistence-layer-with-spring-data-jpa went to spring-data-jpa-repo-2 * https://www.baeldung.com/spring-data-jpa-query went to spring-data-jpa-query-2 * https://www.baeldung.com/spring-jdbc-jdbctemplate moved to spring-jdbc * Removed spring-persistence-simple module as all articles have been moved
This commit is contained in:
+19
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.spring.jdbc;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.dao.DuplicateKeyException;
|
||||
import org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator;
|
||||
|
||||
public class CustomSQLErrorCodeTranslator extends SQLErrorCodeSQLExceptionTranslator {
|
||||
|
||||
@Override
|
||||
protected DataAccessException customTranslate(final String task, final String sql, final SQLException sqlException) {
|
||||
if (sqlException.getErrorCode() == 23505) {
|
||||
return new DuplicateKeyException("Custome Exception translator - Integrity contraint voilation.", sqlException);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.baeldung.spring.jdbc;
|
||||
|
||||
public class Employee {
|
||||
private int id;
|
||||
|
||||
private String firstName;
|
||||
|
||||
private String lastName;
|
||||
|
||||
private String address;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(final int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(final String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(final String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public String getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(final String address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
}
|
||||
+129
@@ -0,0 +1,129 @@
|
||||
package com.baeldung.spring.jdbc;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
|
||||
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
|
||||
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;
|
||||
|
||||
@Repository
|
||||
public class EmployeeDAO {
|
||||
|
||||
private JdbcTemplate jdbcTemplate;
|
||||
|
||||
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
|
||||
|
||||
private SimpleJdbcInsert simpleJdbcInsert;
|
||||
|
||||
private SimpleJdbcCall simpleJdbcCall;
|
||||
|
||||
@Autowired
|
||||
public void setDataSource(final DataSource dataSource) {
|
||||
jdbcTemplate = new JdbcTemplate(dataSource);
|
||||
final CustomSQLErrorCodeTranslator customSQLErrorCodeTranslator = new CustomSQLErrorCodeTranslator();
|
||||
jdbcTemplate.setExceptionTranslator(customSQLErrorCodeTranslator);
|
||||
|
||||
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() {
|
||||
return jdbcTemplate.queryForObject("SELECT COUNT(*) FROM EMPLOYEE", Integer.class);
|
||||
}
|
||||
|
||||
public List<Employee> getAllEmployees() {
|
||||
return jdbcTemplate.query("SELECT * FROM EMPLOYEE", new EmployeeRowMapper());
|
||||
}
|
||||
|
||||
public int addEmplyee(final int id) {
|
||||
return jdbcTemplate.update("INSERT INTO EMPLOYEE VALUES (?, ?, ?, ?)", id, "Bill", "Gates", "USA");
|
||||
}
|
||||
|
||||
public int addEmplyeeUsingSimpelJdbcInsert(final Employee emp) {
|
||||
final Map<String, Object> parameters = new HashMap<String, Object>();
|
||||
parameters.put("ID", emp.getId());
|
||||
parameters.put("FIRST_NAME", emp.getFirstName());
|
||||
parameters.put("LAST_NAME", emp.getLastName());
|
||||
parameters.put("ADDRESS", emp.getAddress());
|
||||
|
||||
return simpleJdbcInsert.execute(parameters);
|
||||
}
|
||||
|
||||
public Employee getEmployee(final int id) {
|
||||
final String query = "SELECT * FROM EMPLOYEE WHERE ID = ?";
|
||||
return jdbcTemplate.queryForObject(query, new Object[] { id }, new EmployeeRowMapper());
|
||||
}
|
||||
|
||||
public void addEmplyeeUsingExecuteMethod() {
|
||||
jdbcTemplate.execute("INSERT INTO EMPLOYEE VALUES (6, 'Bill', 'Gates', 'USA')");
|
||||
}
|
||||
|
||||
public String getEmployeeUsingMapSqlParameterSource() {
|
||||
final SqlParameterSource namedParameters = new MapSqlParameterSource().addValue("id", 1);
|
||||
|
||||
return namedParameterJdbcTemplate.queryForObject("SELECT FIRST_NAME FROM EMPLOYEE WHERE ID = :id", namedParameters, String.class);
|
||||
}
|
||||
|
||||
public int getEmployeeUsingBeanPropertySqlParameterSource() {
|
||||
final Employee employee = new Employee();
|
||||
employee.setFirstName("James");
|
||||
|
||||
final String SELECT_BY_ID = "SELECT COUNT(*) FROM EMPLOYEE WHERE FIRST_NAME = :firstName";
|
||||
|
||||
final SqlParameterSource namedParameters = new BeanPropertySqlParameterSource(employee);
|
||||
|
||||
return namedParameterJdbcTemplate.queryForObject(SELECT_BY_ID, namedParameters, Integer.class);
|
||||
}
|
||||
|
||||
public int[] batchUpdateUsingJDBCTemplate(final List<Employee> employees) {
|
||||
return jdbcTemplate.batchUpdate("INSERT INTO EMPLOYEE VALUES (?, ?, ?, ?)", new BatchPreparedStatementSetter() {
|
||||
|
||||
@Override
|
||||
public void setValues(final PreparedStatement ps, final int i) throws SQLException {
|
||||
ps.setInt(1, employees.get(i).getId());
|
||||
ps.setString(2, employees.get(i).getFirstName());
|
||||
ps.setString(3, employees.get(i).getLastName());
|
||||
ps.setString(4, employees.get(i).getAddress());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getBatchSize() {
|
||||
return 3;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public int[] batchUpdateUsingNamedParameterJDBCTemplate(final List<Employee> employees) {
|
||||
final SqlParameterSource[] batch = SqlParameterSourceUtils.createBatch(employees.toArray());
|
||||
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;
|
||||
}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.spring.jdbc;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
|
||||
public class EmployeeRowMapper implements RowMapper<Employee> {
|
||||
|
||||
@Override
|
||||
public Employee mapRow(final ResultSet rs, final int rowNum) throws SQLException {
|
||||
final Employee employee = new Employee();
|
||||
|
||||
employee.setId(rs.getInt("ID"));
|
||||
employee.setFirstName(rs.getString("FIRST_NAME"));
|
||||
employee.setLastName(rs.getString("LAST_NAME"));
|
||||
employee.setAddress(rs.getString("ADDRESS"));
|
||||
|
||||
return employee;
|
||||
}
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package com.baeldung.spring.jdbc.config;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan("com.baeldung.spring.jdbc")
|
||||
public class SpringJdbcConfig {
|
||||
|
||||
@Bean
|
||||
public DataSource dataSource() {
|
||||
return new EmbeddedDatabaseBuilder()
|
||||
.setType(EmbeddedDatabaseType.H2)
|
||||
.addScript("classpath:jdbc/schema.sql")
|
||||
.addScript("classpath:jdbc/test-data.sql")
|
||||
.build();
|
||||
}
|
||||
|
||||
// @Bean
|
||||
public DataSource mysqlDataSource() {
|
||||
final DriverManagerDataSource dataSource = new DriverManagerDataSource();
|
||||
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
|
||||
dataSource.setUrl("jdbc:mysql://localhost:3306/springjdbc");
|
||||
dataSource.setUsername("guest_user");
|
||||
dataSource.setPassword("guest_password");
|
||||
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
spring.datasource.url=jdbc:mysql://localhost:3306/springjdbc
|
||||
spring.datasource.username=guest_user
|
||||
spring.datasource.password=guest_password
|
||||
@@ -0,0 +1,7 @@
|
||||
CREATE TABLE EMPLOYEE
|
||||
(
|
||||
ID int NOT NULL PRIMARY KEY,
|
||||
FIRST_NAME varchar(255),
|
||||
LAST_NAME varchar(255),
|
||||
ADDRESS varchar(255)
|
||||
);
|
||||
@@ -0,0 +1,7 @@
|
||||
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');
|
||||
Reference in New Issue
Block a user