From 66bfe0102b9b46c796b5edea2eb6ab06471a107f Mon Sep 17 00:00:00 2001 From: parthiv39731 <70740707+parthiv39731@users.noreply.github.com> Date: Tue, 12 Sep 2023 10:12:53 -0700 Subject: [PATCH] BAEL-6880, How to replace deprecated jdbcTemplate.queryForObject and jdbcTemplate.query in spring boot 2.4.X and above --- .../ReplaceDeprecatedApplication.java | 14 ++ ...tDaoWithDeprecatedJdbcTemplateMethods.java | 54 ++++++++ ...ntDaoWithPreferredJdbcTemplateMethods.java | 56 ++++++++ .../jdbc/replacedeprecated/model/Student.java | 60 ++++++++ .../model/StudentResultExtractor.java | 26 ++++ .../model/StudentRowMapper.java | 20 +++ .../replacedeprecated/application.properties | 5 + .../jdbc/replacedeprecated/drop_student.sql | 1 + .../spring/jdbc/replacedeprecated/student.sql | 98 +++++++++++++ ...entDaoWithJdbcTemplateMethodsUnitTest.java | 131 ++++++++++++++++++ 10 files changed, 465 insertions(+) create mode 100644 persistence-modules/spring-jdbc/src/main/java/com/baeldung/spring/jdbc/replacedeprecated/ReplaceDeprecatedApplication.java create mode 100644 persistence-modules/spring-jdbc/src/main/java/com/baeldung/spring/jdbc/replacedeprecated/StudentDaoWithDeprecatedJdbcTemplateMethods.java create mode 100644 persistence-modules/spring-jdbc/src/main/java/com/baeldung/spring/jdbc/replacedeprecated/StudentDaoWithPreferredJdbcTemplateMethods.java create mode 100644 persistence-modules/spring-jdbc/src/main/java/com/baeldung/spring/jdbc/replacedeprecated/model/Student.java create mode 100644 persistence-modules/spring-jdbc/src/main/java/com/baeldung/spring/jdbc/replacedeprecated/model/StudentResultExtractor.java create mode 100644 persistence-modules/spring-jdbc/src/main/java/com/baeldung/spring/jdbc/replacedeprecated/model/StudentRowMapper.java create mode 100644 persistence-modules/spring-jdbc/src/main/resources/com/baeldung/spring/jdbc/replacedeprecated/application.properties create mode 100644 persistence-modules/spring-jdbc/src/main/resources/com/baeldung/spring/jdbc/replacedeprecated/drop_student.sql create mode 100644 persistence-modules/spring-jdbc/src/main/resources/com/baeldung/spring/jdbc/replacedeprecated/student.sql create mode 100644 persistence-modules/spring-jdbc/src/test/java/com/baeldung/spring/jdbc/replacedeprecated/StudentDaoWithJdbcTemplateMethodsUnitTest.java diff --git a/persistence-modules/spring-jdbc/src/main/java/com/baeldung/spring/jdbc/replacedeprecated/ReplaceDeprecatedApplication.java b/persistence-modules/spring-jdbc/src/main/java/com/baeldung/spring/jdbc/replacedeprecated/ReplaceDeprecatedApplication.java new file mode 100644 index 0000000000..f1f50dfbf9 --- /dev/null +++ b/persistence-modules/spring-jdbc/src/main/java/com/baeldung/spring/jdbc/replacedeprecated/ReplaceDeprecatedApplication.java @@ -0,0 +1,14 @@ +package com.baeldung.spring.jdbc.replacedeprecated; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.ComponentScan; + +@SpringBootApplication +@ComponentScan(basePackages = "com.baeldung.spring.jdbc.replacedeprecated") +public class ReplaceDeprecatedApplication { + + public static void main(String[] args) { + SpringApplication.run(ReplaceDeprecatedApplication.class, args); + } +} diff --git a/persistence-modules/spring-jdbc/src/main/java/com/baeldung/spring/jdbc/replacedeprecated/StudentDaoWithDeprecatedJdbcTemplateMethods.java b/persistence-modules/spring-jdbc/src/main/java/com/baeldung/spring/jdbc/replacedeprecated/StudentDaoWithDeprecatedJdbcTemplateMethods.java new file mode 100644 index 0000000000..74e1ea6a0a --- /dev/null +++ b/persistence-modules/spring-jdbc/src/main/java/com/baeldung/spring/jdbc/replacedeprecated/StudentDaoWithDeprecatedJdbcTemplateMethods.java @@ -0,0 +1,54 @@ +package com.baeldung.spring.jdbc.replacedeprecated; + +import com.baeldung.spring.jdbc.replacedeprecated.model.Student; +import com.baeldung.spring.jdbc.replacedeprecated.model.StudentResultExtractor; +import com.baeldung.spring.jdbc.replacedeprecated.model.StudentRowMapper; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.RowCountCallbackHandler; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Repository +public class StudentDaoWithDeprecatedJdbcTemplateMethods { + JdbcTemplate jdbcTemplate; + + @Autowired + public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; + } + + public List getStudentsOfAgeAndGender(Integer age, String gender) { + String sql = "select student_id, student_name, age, gender, grade, state from student where age= ? and gender = ?"; + Object[] args = {age, gender}; + return jdbcTemplate.query(sql, args, new StudentRowMapper()); + } + + public List getStudentsOfAgeGenderAndGrade(Integer age, String gender, Integer grade) { + String sql = "select student_id, student_name, age, gender, grade, state from student where age= ? and gender = ? and grade = ?"; + Object[] args = {age, gender, grade}; + return jdbcTemplate.query(sql, args, new StudentRowMapper()); + } + + public List getStudentsOfGradeAndState(Integer grade, String state) { + String sql = "select student_id, student_name, age, gender, grade, state from student where grade = ? and state = ?"; + Object[] args = {grade, state}; + return jdbcTemplate.query(sql, args, new StudentResultExtractor()); + } + + public Student getStudentOfStudentIDAndGrade(Integer studentID, Integer grade) { + String sql = "select student_id, student_name, age, gender, grade, state from student where student_id = ? and grade = ?"; + Object[] args = {studentID, grade}; + + return jdbcTemplate.queryForObject(sql, args, new StudentRowMapper()); + } + + public Integer getCountOfStudentsInAGradeFromAState(Integer grade, String state) { + String sql = "select student_id, student_name, age, gender, grade, state from student where grade = ? and state = ?"; + Object[] args = {grade, state}; + RowCountCallbackHandler countCallbackHandler = new RowCountCallbackHandler(); + jdbcTemplate.query(sql, args, countCallbackHandler); + return countCallbackHandler.getRowCount(); + } +} diff --git a/persistence-modules/spring-jdbc/src/main/java/com/baeldung/spring/jdbc/replacedeprecated/StudentDaoWithPreferredJdbcTemplateMethods.java b/persistence-modules/spring-jdbc/src/main/java/com/baeldung/spring/jdbc/replacedeprecated/StudentDaoWithPreferredJdbcTemplateMethods.java new file mode 100644 index 0000000000..afe8a8f2bd --- /dev/null +++ b/persistence-modules/spring-jdbc/src/main/java/com/baeldung/spring/jdbc/replacedeprecated/StudentDaoWithPreferredJdbcTemplateMethods.java @@ -0,0 +1,56 @@ +package com.baeldung.spring.jdbc.replacedeprecated; + +import com.baeldung.spring.jdbc.replacedeprecated.model.Student; +import com.baeldung.spring.jdbc.replacedeprecated.model.StudentResultExtractor; +import com.baeldung.spring.jdbc.replacedeprecated.model.StudentRowMapper; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.RowCountCallbackHandler; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Repository +public class StudentDaoWithPreferredJdbcTemplateMethods { + JdbcTemplate jdbcTemplate; + + @Autowired + public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; + } + + public List getStudentsOfAgeAndGender(Integer age, String gender) { + String sql = "select student_id, student_name, age, gender, grade, state from student where age= ? and gender = ?"; + return jdbcTemplate.query(sql, new StudentRowMapper(), age, gender); + } + + public List getStudentsOfAgeGenderAndGrade(Integer age, String gender, Integer grade) { + String sql = "select student_id, student_name, age, gender, grade, state from student where age= ? and gender = ? and grade = ?"; + return jdbcTemplate.query(sql, new StudentRowMapper(), age, gender, grade); + } + + public List getStudentsOfGradeAndState(Integer grade, String state) { + String sql = "select student_id, student_name, age, gender, grade, state from student where grade = ? and state = ?"; + return jdbcTemplate.query(sql, new StudentResultExtractor(), grade, state); + } + + public Student getStudentOfStudentIDAndGrade(Integer studentID, Integer grade) { + String sql = "select student_id, student_name, age, gender, grade, state from student where student_id = ? and grade = ?"; + + return jdbcTemplate.queryForObject(sql, new StudentRowMapper(), studentID, grade); + } + + public Integer getCountOfGenderInAGrade(String gender, Integer grade) { + String sql = "select count(1) as total from student where gender = ? and grade = ?"; + + return jdbcTemplate.queryForObject(sql, Integer.class, gender, grade); + } + + public Integer getCountOfStudentsInAGradeFromAState(Integer grade, String state) { + String sql = "select student_id, student_name, age, gender, grade, state from student where grade = ? and state = ?"; + + RowCountCallbackHandler countCallbackHandler = new RowCountCallbackHandler(); + jdbcTemplate.query(sql, countCallbackHandler, grade, state); + return countCallbackHandler.getRowCount(); + } +} diff --git a/persistence-modules/spring-jdbc/src/main/java/com/baeldung/spring/jdbc/replacedeprecated/model/Student.java b/persistence-modules/spring-jdbc/src/main/java/com/baeldung/spring/jdbc/replacedeprecated/model/Student.java new file mode 100644 index 0000000000..001089d830 --- /dev/null +++ b/persistence-modules/spring-jdbc/src/main/java/com/baeldung/spring/jdbc/replacedeprecated/model/Student.java @@ -0,0 +1,60 @@ +package com.baeldung.spring.jdbc.replacedeprecated.model; + +public class Student { + private Integer studentId; + private String studentName; + private String studentGender; + private Integer age; + private Integer grade; + + public Integer getStudentId() { + return studentId; + } + + public void setStudentId(Integer studentId) { + this.studentId = studentId; + } + + public String getStudentName() { + return studentName; + } + + public void setStudentName(String studentName) { + this.studentName = studentName; + } + + public String getStudentGender() { + return studentGender; + } + + public void setStudentGender(String studentGender) { + this.studentGender = studentGender; + } + + public Integer getAge() { + return age; + } + + public void setAge(Integer age) { + this.age = age; + } + + public Integer getGrade() { + return grade; + } + + public void setGrade(Integer grade) { + this.grade = grade; + } + + public String getState() { + return state; + } + + public void setState(String state) { + this.state = state; + } + + private String state; + +} diff --git a/persistence-modules/spring-jdbc/src/main/java/com/baeldung/spring/jdbc/replacedeprecated/model/StudentResultExtractor.java b/persistence-modules/spring-jdbc/src/main/java/com/baeldung/spring/jdbc/replacedeprecated/model/StudentResultExtractor.java new file mode 100644 index 0000000000..d2da59e5f9 --- /dev/null +++ b/persistence-modules/spring-jdbc/src/main/java/com/baeldung/spring/jdbc/replacedeprecated/model/StudentResultExtractor.java @@ -0,0 +1,26 @@ +package com.baeldung.spring.jdbc.replacedeprecated.model; + +import org.springframework.jdbc.core.ResultSetExtractor; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +public class StudentResultExtractor implements ResultSetExtractor> { + @Override + public List extractData(ResultSet rs) throws SQLException { + List students = new ArrayList(); + while(rs.next()) { + Student student = new Student(); + student.setStudentId(rs.getInt("student_id")); + student.setStudentName(rs.getString("student_name")); + student.setAge(rs.getInt("age")); + student.setStudentGender(rs.getString("gender")); + student.setGrade(rs.getInt("grade")); + student.setState(rs.getString("state")); + students.add(student); + } + return students; + } +} diff --git a/persistence-modules/spring-jdbc/src/main/java/com/baeldung/spring/jdbc/replacedeprecated/model/StudentRowMapper.java b/persistence-modules/spring-jdbc/src/main/java/com/baeldung/spring/jdbc/replacedeprecated/model/StudentRowMapper.java new file mode 100644 index 0000000000..bf2419e663 --- /dev/null +++ b/persistence-modules/spring-jdbc/src/main/java/com/baeldung/spring/jdbc/replacedeprecated/model/StudentRowMapper.java @@ -0,0 +1,20 @@ +package com.baeldung.spring.jdbc.replacedeprecated.model; + +import org.springframework.jdbc.core.RowMapper; + +import java.sql.ResultSet; +import java.sql.SQLException; + +public class StudentRowMapper implements RowMapper { + @Override + public Student mapRow(ResultSet rs, int rowNum) throws SQLException { + Student student = new Student(); + student.setStudentId(rs.getInt("student_id")); + student.setStudentName(rs.getString("student_name")); + student.setAge(rs.getInt("age")); + student.setStudentGender(rs.getString("gender")); + student.setGrade(rs.getInt("grade")); + student.setState(rs.getString("state")); + return student; + } +} diff --git a/persistence-modules/spring-jdbc/src/main/resources/com/baeldung/spring/jdbc/replacedeprecated/application.properties b/persistence-modules/spring-jdbc/src/main/resources/com/baeldung/spring/jdbc/replacedeprecated/application.properties new file mode 100644 index 0000000000..04c963ebf4 --- /dev/null +++ b/persistence-modules/spring-jdbc/src/main/resources/com/baeldung/spring/jdbc/replacedeprecated/application.properties @@ -0,0 +1,5 @@ +# DataSource Configuration +spring.datasource.url=jdbc:h2:mem:testdb +spring.datasource.driverClassName=org.h2.Driver +spring.datasource.username=user +spring.datasource.password= # Leave this empty \ No newline at end of file diff --git a/persistence-modules/spring-jdbc/src/main/resources/com/baeldung/spring/jdbc/replacedeprecated/drop_student.sql b/persistence-modules/spring-jdbc/src/main/resources/com/baeldung/spring/jdbc/replacedeprecated/drop_student.sql new file mode 100644 index 0000000000..954545a862 --- /dev/null +++ b/persistence-modules/spring-jdbc/src/main/resources/com/baeldung/spring/jdbc/replacedeprecated/drop_student.sql @@ -0,0 +1 @@ +DROP TABLE student; \ No newline at end of file diff --git a/persistence-modules/spring-jdbc/src/main/resources/com/baeldung/spring/jdbc/replacedeprecated/student.sql b/persistence-modules/spring-jdbc/src/main/resources/com/baeldung/spring/jdbc/replacedeprecated/student.sql new file mode 100644 index 0000000000..0e137f5445 --- /dev/null +++ b/persistence-modules/spring-jdbc/src/main/resources/com/baeldung/spring/jdbc/replacedeprecated/student.sql @@ -0,0 +1,98 @@ + +CREATE TABLE student ( + student_id INT AUTO_INCREMENT PRIMARY KEY, + student_name VARCHAR(255) NOT NULL, + age INT, + grade INT NOT NULL, + gender VARCHAR(10) NOT NULL, + state VARCHAR(100) NOT NULL +); +-- Student 1 +INSERT INTO student (student_name, age, grade, gender, state) VALUES ('John Smith', 18, 3, 'Male', 'California'); + +-- Student 2 +INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Emily Johnson', 17, 2, 'Female', 'New York'); + +-- Student 3 +INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Michael Davis', 4, 1, 'Male', 'Texas'); + +-- Student 4 +INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Sophia Martinez', 2, 1, 'Female', 'Florida'); + +-- Student 5 +INSERT INTO student (student_name, age, grade, gender, state) VALUES ('William Brown', 5, 5, 'Male', 'California'); + +-- Student 6 +INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Olivia Garcia', 4, 2, 'Female', 'Texas'); + +-- Student 7 +INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Ethan Rodriguez', 3, 1, 'Male', 'New York'); + +-- Student 8 +INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Ava Hernandez', 2, 1, 'Female', 'Florida'); + +-- Student 9 +INSERT INTO student (student_name, age, grade, gender, state) VALUES ('James Wilson', 5, 4, 'Male', 'Texas'); + +-- Student 10 +INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Emma Miller', 3, 1, 'Female', 'California'); + +-- Student 11 +INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Benjamin Brown', 4, 1, 'Male', 'New York'); + +-- Student 12 +INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Mia Smith', 2, 1, 'Female', 'Florida'); + +-- Student 13 +INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Daniel Johnson', 5, 4, 'Male', 'California'); + +-- Student 14 +INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Ava Davis', 4, 2, 'Female', 'Texas'); + +-- Student 15 +INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Matthew Martinez', 3, 1, 'Male', 'New York'); + +-- Student 16 +INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Sophia Taylor', 2, 1, 'Female', 'Florida'); + +-- Student 17 +INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Alexander White', 5, 4, 'Male', 'California'); + +-- Student 18 +INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Olivia Johnson', 4, 2, 'Female', 'Texas'); + +-- Student 19 +INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Christopher Lee', 3, 1, 'Male', 'New York'); + +-- Student 20 +INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Emma Wilson', 2, 1, 'Female', 'Florida'); + +-- Student 21 +INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Elijah Smith', 5, 3, 'Male', 'Texas'); + +-- Student 22 +INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Isabella Davis', 4, 2, 'Female', 'California'); + +-- Student 23 +INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Liam Johnson', 3, 1, 'Male', 'New York'); + +-- Student 24 +INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Olivia Garcia', 2, 1, 'Female', 'Florida'); + +-- Student 25 +INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Noah Rodriguez', 5, 3, 'Male', 'Texas'); + +-- Student 26 +INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Sophia Hernandez', 4, 2, 'Female', 'California'); + +-- Student 27 +INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Mason Smith', 3, 1, 'Male', 'New York'); + +-- Student 28 +INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Ava Taylor', 2, 1, 'Female', 'Florida'); + +-- Student 29 +INSERT INTO student (student_name, age, grade, gender, state) VALUES ('William Brown', 5, 5, 'Male', 'Texas'); + +-- Student 30 +INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Olivia Martinez', 4, 4, 'Female', 'California'); diff --git a/persistence-modules/spring-jdbc/src/test/java/com/baeldung/spring/jdbc/replacedeprecated/StudentDaoWithJdbcTemplateMethodsUnitTest.java b/persistence-modules/spring-jdbc/src/test/java/com/baeldung/spring/jdbc/replacedeprecated/StudentDaoWithJdbcTemplateMethodsUnitTest.java new file mode 100644 index 0000000000..ea1a4a8659 --- /dev/null +++ b/persistence-modules/spring-jdbc/src/test/java/com/baeldung/spring/jdbc/replacedeprecated/StudentDaoWithJdbcTemplateMethodsUnitTest.java @@ -0,0 +1,131 @@ +package com.baeldung.spring.jdbc.replacedeprecated; + +import com.baeldung.spring.jdbc.replacedeprecated.model.Student; +import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest; +import org.springframework.test.context.jdbc.Sql; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +@JdbcTest +@Sql(value = "student.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD) +@Sql(value = "drop_student.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD) +public class StudentDaoWithJdbcTemplateMethodsUnitTest { + private static final Logger logger = LoggerFactory.getLogger(StudentDaoWithJdbcTemplateMethodsUnitTest.class); + @Autowired + StudentDaoWithDeprecatedJdbcTemplateMethods studentDaoWithDeprecatedJdbcTemplateMethods; + @Autowired + StudentDaoWithPreferredJdbcTemplateMethods studentDaoWithPreferredJdbcTemplateMethods; + + @Test + public void givenPreferredMethodQuery_whenArgsAgeAndGender_thenReturnStudents() { + List students = studentDaoWithPreferredJdbcTemplateMethods.getStudentsOfAgeAndGender(4, "Female"); + + for (Student student: students) { + logger.info("Student Name: " + student.getStudentName() + " Student gender: " + student.getStudentGender()); + } + assertEquals(6, students.size()); + } + + @Test + public void givenPreferredMethodQuery_whenArgsAgeGenderAndGrade_thenReturnStudents() { + List students = studentDaoWithPreferredJdbcTemplateMethods.getStudentsOfAgeGenderAndGrade(4, "Female", 2); + + for (Student student: students) { + logger.info("Student Name: " + student.getStudentName() + " Student gender: " + student.getStudentGender() + + " Student grade: " + student.getGrade()); + } + assertEquals(5, students.size()); + } + + @Test + public void givenDeprecatedMethodQuery_whenArgsAgeGenderAndGrade_thenReturnStudents() { + List students = studentDaoWithDeprecatedJdbcTemplateMethods.getStudentsOfAgeGenderAndGrade(4, "Female", 2); + + for (Student student: students) { + logger.info("Student Name: " + student.getStudentName() + " Student gender: " + student.getStudentGender() + + " Student grade: " + student.getGrade()); + } + assertEquals(5, students.size()); + } + + @Test + public void givenDeprecatedMethodQuery_whenArgsAgeAndGender_thenReturnStudents() { + List students = studentDaoWithDeprecatedJdbcTemplateMethods.getStudentsOfAgeAndGender(4, "Female"); + for (Student student: students) { + logger.info("Student Name: " + student.getStudentName() + " Student gender: " + student.getStudentGender()); + } + assertEquals(6, students.size()); + } + + @Test + public void givenDeprecatedMethodQuery_whenArgsGradeAndState_thenReturnStudents() { + List students = studentDaoWithDeprecatedJdbcTemplateMethods.getStudentsOfGradeAndState(1, "New York"); + for (Student student: students) { + logger.info("Student Name: " + student.getStudentName() + + " Student grade: " + student.getStudentGender() + + " Student State: " + student.getState()); + } + assertEquals(6, students.size()); + } + + @Test + public void givenPreferredMethodQuery_whenArgsGradeAndState_thenReturnStudents() { + List students = studentDaoWithPreferredJdbcTemplateMethods.getStudentsOfGradeAndState(1, "New York"); + for (Student student: students) { + logger.info("Student Name: " + student.getStudentName() + + " Student grade: " + student.getStudentGender() + + " Student State: " + student.getState()); + } + assertEquals(6, students.size()); + } + + @Test + public void givenDeprecatedMethodQuery_whenArgsGradeAndState_thenReturnCount() { + Integer count = studentDaoWithDeprecatedJdbcTemplateMethods.getCountOfStudentsInAGradeFromAState(1, "New York"); + logger.info("Total students of grade 1 from New York: " + count); + assertEquals(6, count); + } + @Test + public void givenPreferredMethodQuery_whenArgsGradeAndState_thenReturnCount() { + Integer count = studentDaoWithPreferredJdbcTemplateMethods.getCountOfStudentsInAGradeFromAState(1, "New York"); + logger.info("Total students of grade 1 from New York: " + count); + assertEquals(6, count); + } + @Test + public void givenDeprecatedMethodQueryForObject_whenArgsStudentIDAndGrade_thenReturnStudent() { + Student student = studentDaoWithDeprecatedJdbcTemplateMethods.getStudentOfStudentIDAndGrade(4, 1); + assertEquals(1, student.getGrade()); + assertEquals(4, student.getStudentId()); + logger.info("Student ID: " + student.getStudentId() + + " Student Name: " + student.getStudentName() + " Student grade: " + student.getGrade()); + } + + @Test + public void givenPreferredMethodQueryForObject_whenArgsStudentIDAndGrade_thenReturnStudent() { + Student student = studentDaoWithPreferredJdbcTemplateMethods.getStudentOfStudentIDAndGrade(4, 1); + assertEquals(1, student.getGrade()); + assertEquals(4, student.getStudentId()); + logger.info("Student ID: " + student.getStudentId() + + " Student Name: " + student.getStudentName() + " Student grade: " + student.getGrade()); + } + + @Test + public void givenPreferredMethodQueryForObject_whenArgsGenderAndGrade_thenReturnCount() { + Integer count = studentDaoWithPreferredJdbcTemplateMethods.getCountOfGenderInAGrade("Female", 2); + assertEquals(6, count); + logger.info("Total number of Female Students: " + count); + } + + @Test + public void givenDeprecatedMethodQueryForObject_whenArgsGenderAndGrade_thenReturnCount() { + Integer count = studentDaoWithPreferredJdbcTemplateMethods.getCountOfGenderInAGrade("Female", 2); + assertEquals(6, count); + logger.info("Total number of Female Students: " + count); + } +}