2024-01 spring-09-jdbc added

This commit is contained in:
stvort
2024-03-06 20:33:59 +04:00
parent 206745007b
commit 669d860448
56 changed files with 1367 additions and 0 deletions
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>ru.otus</groupId>
<artifactId>jdbc-class-work</artifactId>
<version>1.0</version>
</parent>
<artifactId>jdbc-demo-exercise</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>${h2.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,20 @@
package ru.otus.spring;
import org.h2.tools.Console;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import ru.otus.spring.dao.PersonDao;
@SpringBootApplication
public class Main {
public static void main(String[] args) throws Exception {
ApplicationContext context = SpringApplication.run(Main.class);
PersonDao dao = context.getBean(PersonDao.class);
Console.main(args);
}
}
@@ -0,0 +1,17 @@
package ru.otus.spring.dao;
import ru.otus.spring.domain.Person;
import java.util.List;
public interface PersonDao {
int count();
void insert(Person person);
Person getById(long id);
List<Person> getAll();
void deleteById(long id);
}
@@ -0,0 +1,41 @@
package ru.otus.spring.dao;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.stereotype.Repository;
import ru.otus.spring.domain.Person;
import java.util.List;
@Repository
public class PersonDaoJdbc implements PersonDao {
private final JdbcOperations jdbc;
public PersonDaoJdbc(JdbcOperations jdbcOperations) {
this.jdbc = jdbcOperations;
}
@Override
public int count() {
return 0;
}
@Override
public void insert(Person person) {
}
@Override
public Person getById(long id) {
return null;
}
@Override
public List<Person> getAll() {
return null;
}
@Override
public void deleteById(long id) {
}
}
@@ -0,0 +1,11 @@
package ru.otus.spring.domain;
import lombok.Data;
import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
@Data
public class Person {
private final long id;
private final String name;
}
@@ -0,0 +1,16 @@
spring:
datasource:
url: jdbc:h2:mem:testdb
#initialization-mode: always
#schema: schema.sql
#data: data.sql
sql:
init:
mode: always
data-locations: data.sql
schema-locations: schema.sql
h2:
console:
path: /h2-console
settings:
web-allow-others: true
@@ -0,0 +1,2 @@
insert into persons (id, `name`)
values (1, 'masha');
@@ -0,0 +1,6 @@
DROP TABLE IF EXISTS PERSONS;
CREATE TABLE PERSONS
(
ID BIGINT PRIMARY KEY,
NAME VARCHAR(255)
);
@@ -0,0 +1,92 @@
package ru.otus.spring.dao;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest;
import org.springframework.context.annotation.Import;
import ru.otus.spring.domain.Person;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
@DisplayName("Dao для работы с пёрсонами должно")
@JdbcTest
@Import(PersonDaoJdbc.class)
class PersonDaoJdbcTest {
private static final int EXPECTED_PERSONS_COUNT = 1;
private static final int EXISTING_PERSON_ID = 1;
private static final String EXISTING_PERSON_NAME = "Ivan";
@Autowired
private PersonDaoJdbc personDao;
@DisplayName("возвращать ожидаемое количество пёрсонов в БД")
@Test
void shouldReturnExpectedPersonCount() {
int actualPersonsCount = personDao.count();
assertThat(actualPersonsCount).isEqualTo(EXPECTED_PERSONS_COUNT);
}
@DisplayName("добавлять пёрсона в БД")
@Test
void shouldInsertPerson() {
int countBeforeInsert = personDao.count();
assertThat(countBeforeInsert).isEqualTo(EXPECTED_PERSONS_COUNT);
Person expectedPerson = new Person(2, "Igor");
personDao.insert(expectedPerson);
// Ошибка! Сейчас так проверяем т.к. больше нет других способов,
// когда появится getById, будем использовать его
int countAfterInsert = personDao.count();
assertThat(countAfterInsert).isEqualTo(countBeforeInsert + 1);
/*
Person actualPerson = personDao.getById(expectedPerson.getId());
assertThat(actualPerson).usingRecursiveComparison().isEqualTo(expectedPerson);
*/
}
@DisplayName("возвращать ожидаемого пёрсона по его id")
@Test
void shouldReturnExpectedPersonById() {
Person expectedPerson = new Person(EXISTING_PERSON_ID, EXISTING_PERSON_NAME);
Person actualPerson = personDao.getById(expectedPerson.getId());
assertThat(actualPerson).usingRecursiveComparison().isEqualTo(expectedPerson);
}
@DisplayName("удалять заданного пёрсона по его id")
@Test
void shouldCorrectDeletePersonById() {
// Ошибка! Сейчас так проверяем т.к. больше нет других способов,
// когда появится getById, тест будет выглядеть, как закомментированный блок ниже
int countBeforeDelete = personDao.count();
assertThat(countBeforeDelete).isEqualTo(EXPECTED_PERSONS_COUNT);
personDao.deleteById(EXISTING_PERSON_ID);
int countAfterDelete = personDao.count();
assertThat(countAfterDelete).isEqualTo(countBeforeDelete - 1);
/*
assertThatCode(() -> personDao.getById(EXISTING_PERSON_ID))
.doesNotThrowAnyException();
personDao.deleteById(EXISTING_PERSON_ID);
assertThatThrownBy(() -> personDao.getById(EXISTING_PERSON_ID))
.isInstanceOf(EmptyResultDataAccessException.class);
*/
}
@DisplayName("возвращать ожидаемый список пёрсонов")
@Test
void shouldReturnExpectedPersonsList() {
Person expectedPerson = new Person(EXISTING_PERSON_ID, EXISTING_PERSON_NAME);
List<Person> actualPersonList = personDao.getAll();
assertThat(actualPersonList)
.containsExactlyInAnyOrder(expectedPerson);
}
}
@@ -0,0 +1,10 @@
spring:
datasource:
url: jdbc:h2:mem:testdb
#initialization-mode: always
#data: data.sql
sql:
init:
mode: always
data-locations: data.sql
#schema-locations: schema.sql
@@ -0,0 +1,2 @@
insert into persons (id, `name`)
values (1, 'Ivan');
@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>ru.otus</groupId>
<artifactId>jdbc-class-work</artifactId>
<version>1.0</version>
</parent>
<artifactId>jdbc-demo-solution-3</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>${h2.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,22 @@
package ru.otus.spring;
import org.h2.tools.Console;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import ru.otus.spring.dao.PersonDao;
@SpringBootApplication
public class Main {
public static void main(String[] args) throws Exception {
ApplicationContext context = SpringApplication.run(Main.class);
PersonDao dao = context.getBean(PersonDao.class);
System.out.println("All count " + dao.count());
Console.main(args);
}
}
@@ -0,0 +1,18 @@
package ru.otus.spring.dao;
import ru.otus.spring.domain.Person;
import java.util.List;
public interface PersonDao {
int count();
void insert(Person person);
Person getById(long id);
List<Person> getAll();
void deleteById(long id);
}
@@ -0,0 +1,44 @@
package ru.otus.spring.dao;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.stereotype.Repository;
import ru.otus.spring.domain.Person;
import java.util.List;
@Repository
public class PersonDaoJdbc implements PersonDao {
private final JdbcOperations jdbc;
public PersonDaoJdbc(JdbcOperations jdbcOperations) {
this.jdbc = jdbcOperations;
}
@Override
public int count() {
Integer count = jdbc.queryForObject("select count(*) from persons", Integer.class);
return count == null ? 0 : count;
}
@Override
public void insert(Person person) {
}
@Override
public Person getById(long id) {
return null;
}
@Override
public List<Person> getAll() {
return null;
}
@Override
public void deleteById(long id) {
}
}
@@ -0,0 +1,11 @@
package ru.otus.spring.domain;
import lombok.Data;
import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
@Data
public class Person {
private final long id;
private final String name;
}
@@ -0,0 +1,16 @@
spring:
datasource:
url: jdbc:h2:mem:testdb
#initialization-mode: always
#schema: schema.sql
#data: data.sql
sql:
init:
mode: always
data-locations: data.sql
schema-locations: schema.sql
h2:
console:
path: /h2-console
settings:
web-allow-others: true
@@ -0,0 +1,2 @@
insert into persons (id, `name`)
values (1, 'masha');
@@ -0,0 +1,6 @@
DROP TABLE IF EXISTS PERSONS;
CREATE TABLE PERSONS
(
ID BIGINT PRIMARY KEY,
NAME VARCHAR(255)
);
@@ -0,0 +1,27 @@
package ru.otus.spring.dao;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest;
import org.springframework.context.annotation.Import;
import static org.assertj.core.api.Assertions.assertThat;
@DisplayName("Dao для работы с пёрсонами должно")
@JdbcTest
@Import(PersonDaoJdbc.class)
class PersonDaoJdbcTest {
private static final int EXPECTED_PERSONS_COUNT = 1;
@Autowired
private PersonDaoJdbc personDao;
@DisplayName("возвращать ожидаемое количество пёрсонов в БД")
@Test
void shouldReturnExpectedPersonCount() {
int actualPersonsCount = personDao.count();
assertThat(actualPersonsCount).isEqualTo(EXPECTED_PERSONS_COUNT);
}
}
@@ -0,0 +1,10 @@
spring:
datasource:
url: jdbc:h2:mem:testdb
#initialization-mode: always
#data: data.sql
sql:
init:
mode: always
data-locations: data.sql
#schema-locations: schema.sql
@@ -0,0 +1,2 @@
insert into persons (id, `name`)
values (1, 'Ivan');
@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>ru.otus</groupId>
<artifactId>jdbc-class-work</artifactId>
<version>1.0</version>
</parent>
<artifactId>jdbc-demo-solution-4</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>${h2.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,27 @@
package ru.otus.spring;
import org.h2.tools.Console;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import ru.otus.spring.dao.PersonDao;
import ru.otus.spring.domain.Person;
@SpringBootApplication
public class Main {
public static void main(String[] args) throws Exception {
ApplicationContext context = SpringApplication.run(Main.class);
PersonDao dao = context.getBean(PersonDao.class);
System.out.println("All count " + dao.count());
dao.insert(new Person(2, "ivan"));
System.out.println("All count " + dao.count());
Console.main(args);
}
}
@@ -0,0 +1,18 @@
package ru.otus.spring.dao;
import ru.otus.spring.domain.Person;
import java.util.List;
public interface PersonDao {
int count();
void insert(Person person);
Person getById(long id);
List<Person> getAll();
void deleteById(long id);
}
@@ -0,0 +1,45 @@
package ru.otus.spring.dao;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.stereotype.Repository;
import ru.otus.spring.domain.Person;
import java.util.List;
@Repository
public class PersonDaoJdbc implements PersonDao {
private final JdbcOperations jdbc;
public PersonDaoJdbc(JdbcOperations jdbcOperations) {
this.jdbc = jdbcOperations;
}
@Override
public int count() {
Integer count = jdbc.queryForObject("select count(*) from persons", Integer.class);
return count == null ? 0 : count;
}
@Override
public void insert(Person person) {
jdbc.update("insert into persons (id, name) values (?, ?)", person.getId(), person.getName());
}
@Override
public Person getById(long id) {
return null;
}
@Override
public List<Person> getAll() {
return null;
}
@Override
public void deleteById(long id) {
jdbc.update("delete from persons where id = ?", id);
}
}
@@ -0,0 +1,11 @@
package ru.otus.spring.domain;
import lombok.Data;
import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
@Data
public class Person {
private final long id;
private final String name;
}
@@ -0,0 +1,16 @@
spring:
datasource:
url: jdbc:h2:mem:testdb
#initialization-mode: always
#schema: schema.sql
#data: data.sql
sql:
init:
mode: always
data-locations: data.sql
schema-locations: schema.sql
h2:
console:
path: /h2-console
settings:
web-allow-others: true
@@ -0,0 +1,2 @@
insert into persons (id, `name`)
values (1, 'masha');
@@ -0,0 +1,6 @@
DROP TABLE IF EXISTS PERSONS;
CREATE TABLE PERSONS
(
ID BIGINT PRIMARY KEY,
NAME VARCHAR(255)
);
@@ -0,0 +1,73 @@
package ru.otus.spring.dao;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest;
import org.springframework.context.annotation.Import;
import ru.otus.spring.domain.Person;
import static org.assertj.core.api.Assertions.assertThat;
@DisplayName("Dao для работы с пёрсонами должно")
@JdbcTest
@Import(PersonDaoJdbc.class)
class PersonDaoJdbcTest {
private static final int EXPECTED_PERSONS_COUNT = 1;
private static final int EXISTING_PERSON_ID = 1;
private static final String EXISTING_PERSON_NAME = "Ivan";
@Autowired
private PersonDaoJdbc personDao;
@DisplayName("возвращать ожидаемое количество пёрсонов в БД")
@Test
void shouldReturnExpectedPersonCount() {
int actualPersonsCount = personDao.count();
assertThat(actualPersonsCount).isEqualTo(EXPECTED_PERSONS_COUNT);
}
@DisplayName("добавлять пёрсона в БД")
@Test
void shouldInsertPerson() {
int countBeforeInsert = personDao.count();
assertThat(countBeforeInsert).isEqualTo(EXPECTED_PERSONS_COUNT);
Person expectedPerson = new Person(2, "Igor");
personDao.insert(expectedPerson);
// Ошибка! Сейчас так проверяем т.к. больше нет других способов,
// когда появится getById, будем использовать его
int countAfterInsert = personDao.count();
assertThat(countAfterInsert).isEqualTo(countBeforeInsert + 1);
/*
Person actualPerson = personDao.getById(expectedPerson.getId());
assertThat(actualPerson).usingRecursiveComparison().isEqualTo(expectedPerson);
*/
}
@DisplayName("удалять заданного пёрсона по его id")
@Test
void shouldCorrectDeletePersonById() {
// Ошибка! Сейчас так проверяем т.к. больше нет других способов,
// когда появится getById, тест будет выглядеть, как закомментированный блок ниже
int countBeforeDelete = personDao.count();
assertThat(countBeforeDelete).isEqualTo(EXPECTED_PERSONS_COUNT);
personDao.deleteById(EXISTING_PERSON_ID);
int countAfterDelete = personDao.count();
assertThat(countAfterDelete).isEqualTo(countBeforeDelete - 1);
/*
assertThatCode(() -> personDao.getById(EXISTING_PERSON_ID))
.doesNotThrowAnyException();
personDao.deleteById(EXISTING_PERSON_ID);
assertThatThrownBy(() -> personDao.getById(EXISTING_PERSON_ID))
.isInstanceOf(EmptyResultDataAccessException.class);
*/
}
}
@@ -0,0 +1,10 @@
spring:
datasource:
url: jdbc:h2:mem:testdb
#initialization-mode: always
#data: data.sql
sql:
init:
mode: always
data-locations: data.sql
#schema-locations: schema.sql
@@ -0,0 +1,2 @@
insert into persons (id, `name`)
values (1, 'Ivan');
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>ru.otus</groupId>
<artifactId>jdbc-class-work</artifactId>
<version>1.0</version>
</parent>
<artifactId>jdbc-demo-solution-5</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>${h2.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,31 @@
package ru.otus.spring;
import org.h2.tools.Console;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import ru.otus.spring.dao.PersonDao;
import ru.otus.spring.domain.Person;
@SpringBootApplication
public class Main {
public static void main(String[] args) throws Exception {
ApplicationContext context = SpringApplication.run(Main.class);
PersonDao dao = context.getBean(PersonDao.class);
System.out.println("All count " + dao.count());
dao.insert(new Person(2, "ivan"));
System.out.println("All count " + dao.count());
Person ivan = dao.getById(2);
System.out.println("Ivan id: " + ivan.getId() + " name: " + ivan.getName());
Console.main(args);
}
}
@@ -0,0 +1,17 @@
package ru.otus.spring.dao;
import ru.otus.spring.domain.Person;
import java.util.List;
public interface PersonDao {
int count();
void insert(Person person);
Person getById(long id);
List<Person> getAll();
void deleteById(long id);
}
@@ -0,0 +1,57 @@
package ru.otus.spring.dao;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import ru.otus.spring.domain.Person;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
@Repository
public class PersonDaoJdbc implements PersonDao {
private final JdbcOperations jdbc;
public PersonDaoJdbc(JdbcOperations jdbcOperations) {
this.jdbc = jdbcOperations;
}
@Override
public int count() {
Integer count = jdbc.queryForObject("select count(*) from persons", Integer.class);
return count == null ? 0 : count;
}
@Override
public void insert(Person person) {
jdbc.update("insert into persons (id, name) values (?, ?)", person.getId(), person.getName());
}
@Override
public Person getById(long id) {
return jdbc.queryForObject("select id, name from persons where id = ?", new PersonMapper(), id);
}
@Override
public List<Person> getAll() {
return jdbc.query("select id, name from persons", new PersonMapper());
}
@Override
public void deleteById(long id) {
jdbc.update("delete from persons where id = ?", id);
}
private static class PersonMapper implements RowMapper<Person> {
@Override
public Person mapRow(ResultSet resultSet, int i) throws SQLException {
long id = resultSet.getLong("id");
String name = resultSet.getString("name");
return new Person(id, name);
}
}
}
@@ -0,0 +1,11 @@
package ru.otus.spring.domain;
import lombok.Data;
import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
@Data
public class Person {
private final long id;
private final String name;
}
@@ -0,0 +1,16 @@
spring:
datasource:
url: jdbc:h2:mem:testdb
#initialization-mode: always
#schema: schema.sql
#data: data.sql
sql:
init:
mode: always
data-locations: data.sql
schema-locations: schema.sql
h2:
console:
path: /h2-console
settings:
web-allow-others: true
@@ -0,0 +1,2 @@
insert into persons (id, `name`)
values (1, 'masha');
@@ -0,0 +1,6 @@
DROP TABLE IF EXISTS PERSONS;
CREATE TABLE PERSONS
(
ID BIGINT PRIMARY KEY,
NAME VARCHAR(255)
);
@@ -0,0 +1,71 @@
package ru.otus.spring.dao;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest;
import org.springframework.context.annotation.Import;
import org.springframework.dao.EmptyResultDataAccessException;
import ru.otus.spring.domain.Person;
import java.util.List;
import static org.assertj.core.api.Assertions.*;
@DisplayName("Dao для работы с пёрсонами должно")
@JdbcTest
@Import(PersonDaoJdbc.class)
class PersonDaoJdbcTest {
private static final int EXPECTED_PERSONS_COUNT = 1;
private static final int EXISTING_PERSON_ID = 1;
private static final String EXISTING_PERSON_NAME = "Ivan";
@Autowired
private PersonDaoJdbc personDao;
@DisplayName("возвращать ожидаемое количество пёрсонов в БД")
@Test
void shouldReturnExpectedPersonCount() {
int actualPersonsCount = personDao.count();
assertThat(actualPersonsCount).isEqualTo(EXPECTED_PERSONS_COUNT);
}
@DisplayName("добавлять пёрсона в БД")
@Test
void shouldInsertPerson() {
Person expectedPerson = new Person(2, "Igor");
personDao.insert(expectedPerson);
Person actualPerson = personDao.getById(expectedPerson.getId());
assertThat(actualPerson).usingRecursiveComparison().isEqualTo(expectedPerson);
}
@DisplayName("возвращать ожидаемого пёрсона по его id")
@Test
void shouldReturnExpectedPersonById() {
Person expectedPerson = new Person(EXISTING_PERSON_ID, EXISTING_PERSON_NAME);
Person actualPerson = personDao.getById(expectedPerson.getId());
assertThat(actualPerson).usingRecursiveComparison().isEqualTo(expectedPerson);
}
@DisplayName("удалять заданного пёрсона по его id")
@Test
void shouldCorrectDeletePersonById() {
assertThatCode(() -> personDao.getById(EXISTING_PERSON_ID))
.doesNotThrowAnyException();
personDao.deleteById(EXISTING_PERSON_ID);
assertThatThrownBy(() -> personDao.getById(EXISTING_PERSON_ID))
.isInstanceOf(EmptyResultDataAccessException.class);
}
@DisplayName("возвращать ожидаемый список пёрсонов")
@Test
void shouldReturnExpectedPersonsList() {
Person expectedPerson = new Person(EXISTING_PERSON_ID, EXISTING_PERSON_NAME);
List<Person> actualPersonList = personDao.getAll();
assertThat(actualPersonList)
.containsExactlyInAnyOrder(expectedPerson);
}
}
@@ -0,0 +1,10 @@
spring:
datasource:
url: jdbc:h2:mem:testdb
#initialization-mode: always
#data: data.sql
sql:
init:
mode: always
data-locations: data.sql
#schema-locations: schema.sql
@@ -0,0 +1,2 @@
insert into persons (id, `name`)
values (1, 'Ivan');
@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>ru.otus</groupId>
<artifactId>jdbc-class-work</artifactId>
<version>1.0</version>
</parent>
<artifactId>jdbc-demo-solution-final</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>${h2.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,33 @@
package ru.otus.spring;
import org.h2.tools.Console;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import ru.otus.spring.dao.PersonDao;
import ru.otus.spring.domain.Person;
@SpringBootApplication
public class Main {
public static void main(String[] args) throws Exception {
ApplicationContext context = SpringApplication.run(Main.class);
PersonDao dao = context.getBean(PersonDao.class);
System.out.println("All count " + dao.count());
dao.insert(new Person(2, "ivan"));
System.out.println("All count " + dao.count());
Person ivan = dao.getById(2);
System.out.println("Ivan id: " + ivan.getId() + " name: " + ivan.getName());
System.out.println(dao.getAll());
Console.main(args);
}
}
@@ -0,0 +1,18 @@
package ru.otus.spring.dao;
import ru.otus.spring.domain.Person;
import java.util.List;
public interface PersonDao {
int count();
void insert(Person person);
Person getById(long id);
List<Person> getAll();
void deleteById(long id);
}
@@ -0,0 +1,70 @@
package ru.otus.spring.dao;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
import org.springframework.stereotype.Repository;
import ru.otus.spring.domain.Person;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@Repository
public class PersonDaoJdbc implements PersonDao {
private final JdbcOperations jdbc;
private final NamedParameterJdbcOperations namedParameterJdbcOperations;
public PersonDaoJdbc(NamedParameterJdbcOperations namedParameterJdbcOperations) {
// Это просто оставили, чтобы не переписывать код
// В идеале всё должно быть на NamedParameterJdbcOperations
this.jdbc = namedParameterJdbcOperations.getJdbcOperations();
this.namedParameterJdbcOperations = namedParameterJdbcOperations;
}
@Override
public int count() {
Integer count = jdbc.queryForObject("select count(*) from persons", Integer.class);
return count == null ? 0 : count;
}
@Override
public void insert(Person person) {
namedParameterJdbcOperations.update("insert into persons (id, name) values (:id, :name)",
Map.of("id", person.getId(), "name", person.getName()));
}
@Override
public Person getById(long id) {
Map<String, Object> params = Collections.singletonMap("id", id);
return namedParameterJdbcOperations.queryForObject(
"select id, name from persons where id = :id", params, new PersonMapper()
);
}
@Override
public List<Person> getAll() {
return jdbc.query("select id, name from persons", new PersonMapper());
}
@Override
public void deleteById(long id) {
Map<String, Object> params = Collections.singletonMap("id", id);
namedParameterJdbcOperations.update(
"delete from persons where id = :id", params
);
}
private static class PersonMapper implements RowMapper<Person> {
@Override
public Person mapRow(ResultSet resultSet, int i) throws SQLException {
long id = resultSet.getLong("id");
String name = resultSet.getString("name");
return new Person(id, name);
}
}
}
@@ -0,0 +1,11 @@
package ru.otus.spring.domain;
import lombok.Data;
import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
@Data
public class Person {
private final long id;
private final String name;
}
@@ -0,0 +1,17 @@
spring:
datasource:
url: jdbc:h2:mem:testdb
#initialization-mode: always
#schema: schema.sql
#data: data.sql
sql:
init:
mode: always
data-locations: data.sql
schema-locations: schema.sql
h2:
console:
path: /h2-console
settings:
web-allow-others: true
@@ -0,0 +1,2 @@
insert into persons (id, `name`)
values (1, 'masha');
@@ -0,0 +1,6 @@
DROP TABLE IF EXISTS PERSONS;
CREATE TABLE PERSONS
(
ID BIGINT PRIMARY KEY,
NAME VARCHAR(255)
);
@@ -0,0 +1,86 @@
package ru.otus.spring.dao;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest;
import org.springframework.context.annotation.Import;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.test.context.transaction.AfterTransaction;
import org.springframework.test.context.transaction.BeforeTransaction;
import ru.otus.spring.domain.Person;
import java.util.List;
import static org.assertj.core.api.Assertions.*;
@DisplayName("Dao для работы с пёрсонами должно")
@JdbcTest
@Import(PersonDaoJdbc.class)
//@Transactional(propagation = Propagation.NOT_SUPPORTED)
class PersonDaoJdbcTest {
private static final int EXPECTED_PERSONS_COUNT = 1;
private static final int EXISTING_PERSON_ID = 1;
private static final String EXISTING_PERSON_NAME = "Ivan";
@Autowired
private PersonDaoJdbc personDao;
@BeforeTransaction
void beforeTransaction() {
System.out.println("beforeTransaction");
}
@AfterTransaction
void afterTransaction() {
System.out.println("afterTransaction");
}
@DisplayName("возвращать ожидаемое количество пёрсонов в БД")
@Test
void shouldReturnExpectedPersonCount() {
int actualPersonsCount = personDao.count();
assertThat(actualPersonsCount).isEqualTo(EXPECTED_PERSONS_COUNT);
}
//@Rollback(value = false)
//@Commit
@DisplayName("добавлять пёрсона в БД")
@Test
void shouldInsertPerson() {
Person expectedPerson = new Person(2, "Igor");
personDao.insert(expectedPerson);
Person actualPerson = personDao.getById(expectedPerson.getId());
assertThat(actualPerson).usingRecursiveComparison().isEqualTo(expectedPerson);
}
@DisplayName("возвращать ожидаемого пёрсона по его id")
@Test
void shouldReturnExpectedPersonById() {
Person expectedPerson = new Person(EXISTING_PERSON_ID, EXISTING_PERSON_NAME);
Person actualPerson = personDao.getById(expectedPerson.getId());
assertThat(actualPerson).usingRecursiveComparison().isEqualTo(expectedPerson);
}
@DisplayName("удалять заданного пёрсона по его id")
@Test
void shouldCorrectDeletePersonById() {
assertThatCode(() -> personDao.getById(EXISTING_PERSON_ID))
.doesNotThrowAnyException();
personDao.deleteById(EXISTING_PERSON_ID);
assertThatThrownBy(() -> personDao.getById(EXISTING_PERSON_ID))
.isInstanceOf(EmptyResultDataAccessException.class);
}
@DisplayName("возвращать ожидаемый список пёрсонов")
@Test
void shouldReturnExpectedPersonsList() {
Person expectedPerson = new Person(EXISTING_PERSON_ID, EXISTING_PERSON_NAME);
List<Person> actualPersonList = personDao.getAll();
assertThat(actualPersonList)
.containsExactlyInAnyOrder(expectedPerson);
}
}
@@ -0,0 +1,10 @@
spring:
datasource:
url: jdbc:h2:mem:testdb
#initialization-mode: always
#data: data.sql
sql:
init:
mode: always
data-locations: data.sql
#schema-locations: schema.sql
@@ -0,0 +1,2 @@
insert into persons (id, `name`)
values (1, 'Ivan');
+53
View File
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.2</version>
<relativePath/>
</parent>
<groupId>ru.otus</groupId>
<artifactId>jdbc-class-work</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<modules>
<module>jdbc-demo-exercise</module>
<module>jdbc-demo-solution-3</module>
<module>jdbc-demo-solution-4</module>
<module>jdbc-demo-solution-5</module>
<module>jdbc-demo-solution-final</module>
</modules>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<h2.version>2.2.220</h2.version>
<snakeyaml.version>2.0</snakeyaml.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>${snakeyaml.version}</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>${h2.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>