Java 29169 :- Upgrade spring-jdbc to Spring Boot 3 (#15423)

This commit is contained in:
Amit Pandey 2023-12-28 20:24:32 +05:30 committed by GitHub
parent 05aa00c4c8
commit 8e1e357c9d
9 changed files with 18 additions and 14 deletions

View File

@ -8,11 +8,15 @@
<parent> <parent>
<groupId>com.baeldung</groupId> <groupId>com.baeldung</groupId>
<artifactId>parent-boot-2</artifactId> <artifactId>parent-boot-3</artifactId>
<version>0.0.1-SNAPSHOT</version> <version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-boot-2</relativePath> <relativePath>../../parent-boot-3</relativePath>
</parent> </parent>
<properties>
<start-class>com.baeldung.spring.jdbc.batch.SpringJdbcBatchPerformanceApplication</start-class>
</properties>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.springframework.data</groupId> <groupId>org.springframework.data</groupId>

View File

@ -1,6 +1,7 @@
package com.baeldung.spring.jdbc.autogenkey.repository; package com.baeldung.spring.jdbc.autogenkey.repository;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.Statement;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
@ -21,7 +22,8 @@ public class MessageRepositoryJDBCTemplate {
KeyHolder keyHolder = new GeneratedKeyHolder(); KeyHolder keyHolder = new GeneratedKeyHolder();
jdbcTemplate.update(connection -> { jdbcTemplate.update(connection -> {
PreparedStatement ps = connection.prepareStatement(INSERT_MESSAGE_SQL); PreparedStatement ps = connection.prepareStatement(INSERT_MESSAGE_SQL,
Statement.RETURN_GENERATED_KEYS);
ps.setString(1, message); ps.setString(1, message);
return ps; return ps;
}, keyHolder); }, keyHolder);
@ -32,7 +34,7 @@ public class MessageRepositoryJDBCTemplate {
final String SELECT_BY_ID = "select message from sys_message where id = ?"; final String SELECT_BY_ID = "select message from sys_message where id = ?";
public String getMessageById(long id) { public String getMessageById(long id) {
return this.jdbcTemplate.queryForObject(SELECT_BY_ID, String.class, new Object[] { id }); return this.jdbcTemplate.queryForObject(SELECT_BY_ID, String.class, id);
} }
} }

View File

@ -20,7 +20,7 @@ public class MessageRepositorySimpleJDBCInsert {
} }
public long insert(String message) { public long insert(String message) {
Map<String, Object> parameters = new HashMap<String, Object>(1); Map<String, Object> parameters = new HashMap<>(1);
parameters.put("message", message); parameters.put("message", message);
Number newId = messageInsert.executeAndReturnKey(parameters); Number newId = messageInsert.executeAndReturnKey(parameters);
return (long) newId; return (long) newId;

View File

@ -10,7 +10,7 @@ import java.util.List;
public class StudentResultExtractor implements ResultSetExtractor<List<Student>> { public class StudentResultExtractor implements ResultSetExtractor<List<Student>> {
@Override @Override
public List<Student> extractData(ResultSet rs) throws SQLException { public List<Student> extractData(ResultSet rs) throws SQLException {
List<Student> students = new ArrayList<Student>(); List<Student> students = new ArrayList<>();
while(rs.next()) { while(rs.next()) {
Student student = new Student(); Student student = new Student();
student.setStudentId(rs.getInt("student_id")); student.setStudentId(rs.getInt("student_id"));

View File

@ -57,7 +57,7 @@ public class EmployeeDAO {
} }
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<>();
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());
@ -68,7 +68,7 @@ public class EmployeeDAO {
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 }, new EmployeeRowMapper()); return jdbcTemplate.queryForObject(query, new EmployeeRowMapper(), id);
} }
public void addEmplyeeUsingExecuteMethod() { public void addEmplyeeUsingExecuteMethod() {

View File

@ -35,8 +35,7 @@ public class EmployeeDAO {
String inSql = String.join(",", Collections.nCopies(ids.size(), "?")); String inSql = String.join(",", Collections.nCopies(ids.size(), "?"));
List<Employee> employees = jdbcTemplate.query( List<Employee> employees = jdbcTemplate.query(
String.format("SELECT * FROM EMPLOYEE WHERE id IN (%s)", inSql), 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")), ids.toArray());
(rs, rowNum) -> new Employee(rs.getInt("id"), rs.getString("first_name"), rs.getString("last_name")));
return employees; return employees;
} }

View File

@ -1,5 +1,5 @@
CREATE TABLE IF NOT EXISTS sys_message ( CREATE TABLE IF NOT EXISTS sys_message (
id bigint(20) NOT NULL AUTO_INCREMENT, id bigint NOT NULL AUTO_INCREMENT,
message varchar(100) NOT NULL, message varchar(100) NOT NULL,
PRIMARY KEY (id) PRIMARY KEY (id)
); );

View File

@ -7,7 +7,6 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import com.baeldung.spring.jdbc.autogenkey.repository.MessageRepositoryJDBCTemplate; import com.baeldung.spring.jdbc.autogenkey.repository.MessageRepositoryJDBCTemplate;

View File

@ -81,7 +81,7 @@ public class EmployeeDAOIntegrationTest {
@Test @Test
public void testBatchUpdateUsingJDBCTemplate() { public void testBatchUpdateUsingJDBCTemplate() {
final List<Employee> employees = new ArrayList<Employee>(); final List<Employee> employees = new ArrayList<>();
final Employee emp1 = new Employee(); final Employee emp1 = new Employee();
emp1.setId(10); emp1.setId(10);
emp1.setFirstName("firstName1"); emp1.setFirstName("firstName1");
@ -113,7 +113,7 @@ public class EmployeeDAOIntegrationTest {
@Test @Test
public void testBatchUpdateUsingNamedParameterJDBCTemplate() { public void testBatchUpdateUsingNamedParameterJDBCTemplate() {
final List<Employee> employees = new ArrayList<Employee>(); final List<Employee> employees = new ArrayList<>();
final Employee emp1 = new Employee(); final Employee emp1 = new Employee();
emp1.setId(40); emp1.setId(40);
emp1.setFirstName("firstName4"); emp1.setFirstName("firstName4");