diff --git a/persistence-modules/spring-jdbc-2/src/main/java/com/baeldung/spring/jdbc/queryforlist/QueryForListDemoApplication.java b/persistence-modules/spring-jdbc-2/src/main/java/com/baeldung/spring/jdbc/queryforlist/QueryForListDemoApplication.java new file mode 100644 index 000000000000..f835e4d8c3fe --- /dev/null +++ b/persistence-modules/spring-jdbc-2/src/main/java/com/baeldung/spring/jdbc/queryforlist/QueryForListDemoApplication.java @@ -0,0 +1,7 @@ +package com.baeldung.spring.jdbc.queryforlist; + +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class QueryForListDemoApplication { +} \ No newline at end of file diff --git a/persistence-modules/spring-jdbc-2/src/main/resources/com/baeldung/spring/jdbc/queryforlist/init-student-data.sql b/persistence-modules/spring-jdbc-2/src/main/resources/com/baeldung/spring/jdbc/queryforlist/init-student-data.sql new file mode 100644 index 000000000000..208e4f8966cd --- /dev/null +++ b/persistence-modules/spring-jdbc-2/src/main/resources/com/baeldung/spring/jdbc/queryforlist/init-student-data.sql @@ -0,0 +1,11 @@ +CREATE TABLE STUDENT_TBL +( + ID int NOT NULL PRIMARY KEY, + NAME varchar(255), + MAJOR varchar(255) +); + +INSERT INTO STUDENT_TBL VALUES (1, 'Kai', 'Computer Science'); +INSERT INTO STUDENT_TBL VALUES (2, 'Eric', 'Computer Science'); +INSERT INTO STUDENT_TBL VALUES (3, 'Kevin', 'Banking'); +INSERT INTO STUDENT_TBL VALUES (4, 'Liam', 'Law'); \ No newline at end of file diff --git a/persistence-modules/spring-jdbc-2/src/test/java/com/baeldung/spring/jdbc/queryforlist/JdbcTemplateIntegrationTest.java b/persistence-modules/spring-jdbc-2/src/test/java/com/baeldung/spring/jdbc/queryforlist/JdbcTemplateIntegrationTest.java new file mode 100644 index 000000000000..eda3424982d0 --- /dev/null +++ b/persistence-modules/spring-jdbc-2/src/test/java/com/baeldung/spring/jdbc/queryforlist/JdbcTemplateIntegrationTest.java @@ -0,0 +1,80 @@ +package com.baeldung.spring.jdbc.queryforlist; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.List; +import java.util.Map; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest; +import org.springframework.jdbc.IncorrectResultSetColumnCountException; +import org.springframework.jdbc.core.BeanPropertyRowMapper; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.test.context.jdbc.Sql; + +@JdbcTest +@Sql(value = { "init-student-data.sql" }, executionPhase = Sql.ExecutionPhase.BEFORE_TEST_CLASS) +class JdbcTemplateIntegrationTest { + + @Autowired + private JdbcTemplate jdbcTemplate; + + @Test + void whenUsingQueryForListToGetStudentList_thenGotException() { + assertThrows(IncorrectResultSetColumnCountException.class, () -> jdbcTemplate.queryForList("SELECT * FROM STUDENT_TBL", Student.class)); + } + + @Test + void whenUsingQueryForListToSingleColumn_thenCorrect() { + List names = jdbcTemplate.queryForList("SELECT NAME FROM STUDENT_TBL", String.class); + assertEquals(List.of("Kai", "Eric", "Kevin", "Liam"), names); + + List ids = jdbcTemplate.queryForList("SELECT ID FROM STUDENT_TBL", Integer.class); + assertEquals(List.of(1, 2, 3, 4), ids); + } + + @Test + void whenUsingQueryForListToGetMap_thenCorrect() { + List> nameMajorRowMaps = jdbcTemplate.queryForList("SELECT NAME, MAJOR FROM STUDENT_TBL"); + + // @formatter:off + assertEquals(List.of( + Map.of("NAME", "Kai", "MAJOR", "Computer Science"), + Map.of("NAME", "Eric", "MAJOR", "Computer Science"), + Map.of("NAME", "Kevin", "MAJOR", "Banking"), + Map.of("NAME", "Liam", "MAJOR", "Law") + ), nameMajorRowMaps); + // @formatter:on + + List> rowMaps = jdbcTemplate.queryForList("SELECT * FROM STUDENT_TBL"); + + // @formatter:off + assertEquals(List.of( + Map.of("ID", 1, "NAME", "Kai", "MAJOR", "Computer Science"), + Map.of("ID", 2, "NAME", "Eric", "MAJOR", "Computer Science"), + Map.of("ID", 3, "NAME", "Kevin", "MAJOR", "Banking"), + Map.of("ID", 4, "NAME", "Liam", "MAJOR", "Law") + ), rowMaps); + // @formatter:on + } + + @Test + void whenUsingQueryWithRowMapperToGetStudentList_thenCorrect() { + // @formatter:off + List expected = List.of( + new Student(1, "Kai", "Computer Science"), + new Student(2, "Eric", "Computer Science"), + new Student(3, "Kevin", "Banking"), + new Student(4, "Liam", "Law") + ); + // @formatter:on + + List students = jdbcTemplate.query("SELECT * FROM STUDENT_TBL", new BeanPropertyRowMapper<>(Student.class)); + + assertEquals(expected, students); + } + +} \ No newline at end of file diff --git a/persistence-modules/spring-jdbc-2/src/test/java/com/baeldung/spring/jdbc/queryforlist/Student.java b/persistence-modules/spring-jdbc-2/src/test/java/com/baeldung/spring/jdbc/queryforlist/Student.java new file mode 100644 index 000000000000..cc820a17a7a5 --- /dev/null +++ b/persistence-modules/spring-jdbc-2/src/test/java/com/baeldung/spring/jdbc/queryforlist/Student.java @@ -0,0 +1,58 @@ +package com.baeldung.spring.jdbc.queryforlist; + +import java.util.Objects; + +public class Student { + + private Integer id; + private String name; + private String major; + + public Student() { + } + + public Student(Integer id, String name, String major) { + this.id = id; + this.name = name; + this.major = major; + } + + @Override + public boolean equals(Object o) { + if (!(o instanceof Student student)) { + return false; + } + return Objects.equals(id, student.id) && Objects.equals(name, student.name) && Objects.equals(major, student.major); + } + + @Override + public int hashCode() { + return Objects.hash(id, name, major); + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getMajor() { + return major; + } + + public void setMajor(String major) { + this.major = major; + } + + +} \ No newline at end of file