mirror of
https://github.com/OtusTeam/Spring.git
synced 2026-05-30 10:50:42 +00:00
2021-03 spring-09-jdbc added
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
.idea/
|
||||
*.iml
|
||||
|
||||
target/
|
||||
@@ -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>
|
||||
|
||||
<groupId>ru.otus</groupId>
|
||||
<artifactId>jdbc-demo-exercise</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.4.4</version>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<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>1.4.200</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.18</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
+20
@@ -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);
|
||||
}
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
package ru.otus.spring.dao;
|
||||
|
||||
public interface PersonDao {
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package ru.otus.spring.dao;
|
||||
|
||||
import org.springframework.jdbc.core.JdbcOperations;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@SuppressWarnings({"SqlNoDataSourceInspection", "ConstantConditions", "SqlDialectInspection"})
|
||||
@Repository
|
||||
public class PersonDaoJdbc implements PersonDao {
|
||||
private final JdbcOperations jdbc;
|
||||
|
||||
public PersonDaoJdbc(JdbcOperations jdbcOperations)
|
||||
{
|
||||
this.jdbc = jdbcOperations;
|
||||
}
|
||||
}
|
||||
+11
@@ -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;
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
spring:
|
||||
datasource:
|
||||
url: jdbc:h2:mem:testdb
|
||||
|
||||
h2:
|
||||
console:
|
||||
path: /h2-console
|
||||
settings:
|
||||
web-allow-others: true
|
||||
@@ -0,0 +1 @@
|
||||
insert into persons (id, `name`) values (1, 'masha');
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
DROP TABLE IF EXISTS PERSONS;
|
||||
CREATE TABLE PERSONS(ID BIGINT PRIMARY KEY, NAME VARCHAR(255));
|
||||
@@ -0,0 +1,4 @@
|
||||
.idea/
|
||||
*.iml
|
||||
|
||||
target/
|
||||
@@ -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>
|
||||
|
||||
<groupId>ru.otus</groupId>
|
||||
<artifactId>jdbc-demo-solution-3</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.4.4</version>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<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>1.4.200</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.18</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
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());
|
||||
|
||||
Console.main(args);
|
||||
}
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
package ru.otus.spring.dao;
|
||||
|
||||
public interface PersonDao {
|
||||
|
||||
int count();
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package ru.otus.spring.dao;
|
||||
|
||||
import org.springframework.jdbc.core.JdbcOperations;
|
||||
import org.springframework.stereotype.Repository;
|
||||
import ru.otus.spring.domain.Person;
|
||||
|
||||
@SuppressWarnings({"SqlNoDataSourceInspection", "ConstantConditions", "SqlDialectInspection"})
|
||||
@Repository
|
||||
public class PersonDaoJdbc implements PersonDao {
|
||||
|
||||
private final JdbcOperations jdbc;
|
||||
|
||||
public PersonDaoJdbc(JdbcOperations jdbcOperations)
|
||||
{
|
||||
this.jdbc = jdbcOperations;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int count() {
|
||||
return jdbc.queryForObject("select count(*) from persons", Integer.class);
|
||||
}
|
||||
|
||||
}
|
||||
+11
@@ -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;
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
spring:
|
||||
datasource:
|
||||
url: jdbc:h2:mem:testdb
|
||||
|
||||
h2:
|
||||
console:
|
||||
path: /h2-console
|
||||
settings:
|
||||
web-allow-others: true
|
||||
+1
@@ -0,0 +1 @@
|
||||
insert into persons (id, `name`) values (1, 'masha');
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
DROP TABLE IF EXISTS PERSONS;
|
||||
CREATE TABLE PERSONS(ID BIGINT PRIMARY KEY, NAME VARCHAR(255));
|
||||
@@ -0,0 +1,4 @@
|
||||
.idea/
|
||||
*.iml
|
||||
|
||||
target/
|
||||
@@ -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>
|
||||
|
||||
<groupId>ru.otus</groupId>
|
||||
<artifactId>jdbc-demo-solution-4</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.4.4</version>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<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>1.4.200</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.18</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
+27
@@ -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);
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package ru.otus.spring.dao;
|
||||
|
||||
import ru.otus.spring.domain.Person;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface PersonDao {
|
||||
|
||||
int count();
|
||||
|
||||
void insert(Person person);
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
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;
|
||||
|
||||
@SuppressWarnings({"SqlNoDataSourceInspection", "ConstantConditions", "SqlDialectInspection"})
|
||||
@Repository
|
||||
public class PersonDaoJdbc implements PersonDao {
|
||||
|
||||
private final JdbcOperations jdbc;
|
||||
|
||||
public PersonDaoJdbc(JdbcOperations jdbcOperations)
|
||||
{
|
||||
this.jdbc = jdbcOperations;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int count() {
|
||||
return jdbc.queryForObject("select count(*) from persons", Integer.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void insert(Person person) {
|
||||
jdbc.update("insert into persons (id, `name`) values (?, ?)", person.getId(), person.getName());
|
||||
}
|
||||
|
||||
}
|
||||
+11
@@ -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;
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
spring:
|
||||
datasource:
|
||||
url: jdbc:h2:mem:testdb
|
||||
|
||||
h2:
|
||||
console:
|
||||
path: /h2-console
|
||||
settings:
|
||||
web-allow-others: true
|
||||
+1
@@ -0,0 +1 @@
|
||||
insert into persons (id, `name`) values (1, 'masha');
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
DROP TABLE IF EXISTS PERSONS;
|
||||
CREATE TABLE PERSONS(ID BIGINT PRIMARY KEY, NAME VARCHAR(255));
|
||||
@@ -0,0 +1,4 @@
|
||||
.idea/
|
||||
*.iml
|
||||
|
||||
target/
|
||||
@@ -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>
|
||||
|
||||
<groupId>ru.otus</groupId>
|
||||
<artifactId>jdbc-demo-solution-5</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.4.4</version>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<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>1.4.200</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.18</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
+31
@@ -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);
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
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();
|
||||
}
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
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;
|
||||
|
||||
@SuppressWarnings({"SqlNoDataSourceInspection", "ConstantConditions", "SqlDialectInspection"})
|
||||
@Repository
|
||||
public class PersonDaoJdbc implements PersonDao {
|
||||
|
||||
private final JdbcOperations jdbc;
|
||||
|
||||
public PersonDaoJdbc(JdbcOperations jdbcOperations)
|
||||
{
|
||||
this.jdbc = jdbcOperations;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int count() {
|
||||
return jdbc.queryForObject("select count(*) from persons", Integer.class);
|
||||
}
|
||||
|
||||
@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 * from persons where id = ?", new Object[] {id}, new PersonMapper());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Person> getAll() {
|
||||
return jdbc.query("select * from persons", new PersonMapper());
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
@@ -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;
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
spring:
|
||||
datasource:
|
||||
url: jdbc:h2:mem:testdb
|
||||
|
||||
h2:
|
||||
console:
|
||||
path: /h2-console
|
||||
settings:
|
||||
web-allow-others: true
|
||||
+1
@@ -0,0 +1 @@
|
||||
insert into persons (id, `name`) values (1, 'masha');
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
DROP TABLE IF EXISTS PERSONS;
|
||||
CREATE TABLE PERSONS(ID BIGINT PRIMARY KEY, NAME VARCHAR(255));
|
||||
@@ -0,0 +1,4 @@
|
||||
.idea/
|
||||
*.iml
|
||||
|
||||
target/
|
||||
@@ -0,0 +1,58 @@
|
||||
<?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>
|
||||
|
||||
<groupId>ru.otus</groupId>
|
||||
<artifactId>jdbc-demo-solution-final</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.4.4</version>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<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>1.4.200</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.18</version>
|
||||
<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>
|
||||
+33
@@ -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);
|
||||
}
|
||||
}
|
||||
+18
@@ -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);
|
||||
}
|
||||
+71
@@ -0,0 +1,71 @@
|
||||
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;
|
||||
|
||||
@SuppressWarnings({"SqlNoDataSourceInspection", "ConstantConditions", "SqlDialectInspection"})
|
||||
@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() {
|
||||
return jdbc.queryForObject("select count(*) from persons", Integer.class);
|
||||
}
|
||||
|
||||
@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 * from persons where id = :id", params, new PersonMapper()
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Person> getAll() {
|
||||
return jdbc.query("select * 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
@@ -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;
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
spring:
|
||||
datasource:
|
||||
url: jdbc:h2:mem:testdb
|
||||
|
||||
h2:
|
||||
console:
|
||||
path: /h2-console
|
||||
settings:
|
||||
web-allow-others: true
|
||||
+1
@@ -0,0 +1 @@
|
||||
insert into persons (id, `name`) values (1, 'masha');
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
DROP TABLE IF EXISTS PERSONS;
|
||||
CREATE TABLE PERSONS(ID BIGINT PRIMARY KEY, NAME VARCHAR(255));
|
||||
+93
@@ -0,0 +1,93 @@
|
||||
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.annotation.Commit;
|
||||
import org.springframework.test.annotation.Rollback;
|
||||
import org.springframework.test.context.transaction.AfterTransaction;
|
||||
import org.springframework.test.context.transaction.BeforeTransaction;
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
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)
|
||||
.usingFieldByFieldElementComparator()
|
||||
.containsExactlyInAnyOrder(expectedPerson);
|
||||
}
|
||||
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
spring:
|
||||
datasource:
|
||||
url: jdbc:h2:mem:testdb
|
||||
initialization-mode: always
|
||||
data: data.sql
|
||||
+1
@@ -0,0 +1 @@
|
||||
insert into persons (id, `name`) values (1, 'Ivan');
|
||||
@@ -0,0 +1,20 @@
|
||||
<?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>
|
||||
|
||||
<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>
|
||||
</project>
|
||||
Reference in New Issue
Block a user