这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.baeldung.spring.jdbc.queryforlist;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class QueryForListDemoApplication {
}
Original file line number Diff line number Diff line change
@@ -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');
Original file line number Diff line number Diff line change
@@ -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<String> names = jdbcTemplate.queryForList("SELECT NAME FROM STUDENT_TBL", String.class);
assertEquals(List.of("Kai", "Eric", "Kevin", "Liam"), names);

List<Integer> ids = jdbcTemplate.queryForList("SELECT ID FROM STUDENT_TBL", Integer.class);
assertEquals(List.of(1, 2, 3, 4), ids);
}

@Test
void whenUsingQueryForListToGetMap_thenCorrect() {
List<Map<String, Object>> 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<Map<String, Object>> 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<Student> 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<Student> students = jdbcTemplate.query("SELECT * FROM STUDENT_TBL", new BeanPropertyRowMapper<>(Student.class));

assertEquals(expected, students);
}

}
Original file line number Diff line number Diff line change
@@ -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;
}


}