mirror of
https://github.com/OtusTeam/Spring.git
synced 2026-05-30 10:50:42 +00:00
2022-08 spring-17-mvc added
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
.idea/
|
||||
*.iml
|
||||
|
||||
target/
|
||||
@@ -0,0 +1,4 @@
|
||||
.idea/
|
||||
*.iml
|
||||
|
||||
target/
|
||||
@@ -0,0 +1,55 @@
|
||||
<?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>demo</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.4.5</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-data-jpa</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,75 @@
|
||||
package ru.otus.springdata;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.data.domain.Example;
|
||||
import org.springframework.data.domain.ExampleMatcher;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
import ru.otus.springdata.domain.Email;
|
||||
import ru.otus.springdata.domain.Person;
|
||||
import ru.otus.springdata.repository.EmailRepository;
|
||||
import ru.otus.springdata.repository.PersonRepository;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static ru.otus.springdata.repository.PersonSpecification.emailAddressLike;
|
||||
import static ru.otus.springdata.repository.PersonSpecification.nameLike;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
ConfigurableApplicationContext context = SpringApplication.run(Main.class);
|
||||
|
||||
PersonRepository personRepository = context.getBean(PersonRepository.class);
|
||||
EmailRepository emailRepository = context.getBean(EmailRepository.class);
|
||||
|
||||
var pushkin = new Person("Александр Сергеевич Пушкин", new Email("alex.pushkin@mail.ru"));
|
||||
var block = new Person("Александр Александрович Блок", new Email("alex.block@mail.ru"));
|
||||
var lermontov = new Person("Михаил Юрьевич Лермонтов", new Email("michail.lermontov@bk.ru"));
|
||||
var gorbachev = new Person("Михаил Сергеевич Горбачев", new Email("gorbachev@mail.ru"));
|
||||
var bulgakov = new Person("Михаил Афанасьевич Булгаков", new Email("bulgakov@mail.ru"));
|
||||
|
||||
emailRepository.save(pushkin.getEmail());
|
||||
emailRepository.save(block.getEmail());
|
||||
emailRepository.save(lermontov.getEmail());
|
||||
emailRepository.save(gorbachev.getEmail());
|
||||
emailRepository.save(bulgakov.getEmail());
|
||||
|
||||
personRepository.save(pushkin);
|
||||
personRepository.save(block);
|
||||
personRepository.save(lermontov);
|
||||
personRepository.save(gorbachev);
|
||||
personRepository.save(bulgakov);
|
||||
|
||||
System.out.println("\n\nИщем почту Горбачева по его id");
|
||||
emailRepository.findByPersonId(gorbachev.getId())
|
||||
.ifPresent(System.out::println);
|
||||
|
||||
|
||||
System.out.println("\n\nС помощью Example ищем всех пёрсонов с именем \"Михаил\" и почтой на \"mail.ru\"");
|
||||
ExampleMatcher ignoringExampleMatcher = ExampleMatcher.matchingAll()
|
||||
.withMatcher("email.address", ExampleMatcher.GenericPropertyMatchers.contains().ignoreCase())
|
||||
.withMatcher("name", ExampleMatcher.GenericPropertyMatchers.contains().ignoreCase())
|
||||
.withIgnorePaths("id", "email.id");
|
||||
|
||||
Example<Person> example = Example.of(new Person("Михаил", new Email(0, "mail.ru")), ignoringExampleMatcher);
|
||||
|
||||
System.out.println(personRepository.findAll(example).stream().map(Objects::toString)
|
||||
.collect(Collectors.joining("\n")));
|
||||
|
||||
|
||||
System.out.println("\n\nС помощью Specification ищем всех пёрсонов с именем \"Александр\" или с почтой на \"bk.ru\"");
|
||||
|
||||
Specification<Person> specification = Specification.where(nameLike("Александр"))
|
||||
.or(emailAddressLike("bk.ru"));
|
||||
|
||||
System.out.println(personRepository.findAll(specification).stream().map(Objects::toString)
|
||||
.collect(Collectors.joining("\n")));
|
||||
|
||||
System.out.println("\n\n");
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package ru.otus.springdata.domain;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Entity
|
||||
public class Email {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private long id;
|
||||
|
||||
private String address;
|
||||
|
||||
public Email(String address) {
|
||||
this.address = address;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package ru.otus.springdata.domain;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Entity
|
||||
public class Person {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private long id;
|
||||
|
||||
private String name;
|
||||
|
||||
@OneToOne(orphanRemoval = true)
|
||||
@JoinColumn(name = "email_id")
|
||||
private Email email;
|
||||
|
||||
public Person(String name, Email email) {
|
||||
this.name = name;
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package ru.otus.springdata.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Modifying;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import ru.otus.springdata.domain.Email;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface EmailRepository extends JpaRepository<Email, Long>, EmailRepositoryCustom {
|
||||
|
||||
@Query("select e from Email e where e.address = :address")
|
||||
Optional<Email> findByEmailAddress(@Param("address") String email);
|
||||
|
||||
@Modifying
|
||||
@Transactional
|
||||
@Query("update Email e set e.address = :address where e.id = :id")
|
||||
void updateEmailById(@Param("id") long id, @Param("address") String address);
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package ru.otus.springdata.repository;
|
||||
|
||||
import ru.otus.springdata.domain.Email;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface EmailRepositoryCustom {
|
||||
Optional<Email> findByPersonId(long personId);
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package ru.otus.springdata.repository;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Repository;
|
||||
import ru.otus.springdata.domain.Email;
|
||||
import ru.otus.springdata.domain.Person;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Repository
|
||||
@RequiredArgsConstructor
|
||||
public class EmailRepositoryCustomImpl implements EmailRepositoryCustom {
|
||||
|
||||
private final PersonRepository personRepository;
|
||||
|
||||
@Override
|
||||
public Optional<Email> findByPersonId(long personId) {
|
||||
return personRepository.findById(personId).map(Person::getEmail);
|
||||
}
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package ru.otus.springdata.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.EntityGraph;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import ru.otus.springdata.domain.Person;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface PersonRepository extends JpaRepository<Person, Long>, JpaSpecificationExecutor<Person> {
|
||||
|
||||
@EntityGraph(attributePaths = "email")
|
||||
List<Person> findAll();
|
||||
|
||||
Optional<Person> findByName(String s);
|
||||
|
||||
Optional<Person> findByEmailAddress(String email);
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package ru.otus.springdata.repository;
|
||||
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
import ru.otus.springdata.domain.Person;
|
||||
|
||||
public class PersonSpecification {
|
||||
|
||||
public static Specification<Person> nameLike(String name) {
|
||||
if (name == null) {
|
||||
return null;
|
||||
}
|
||||
return (root, query, cb) -> cb.like(root.get("name"), "%" + name + "%");
|
||||
}
|
||||
|
||||
public static Specification<Person> emailAddressLike(String address) {
|
||||
if (address == null) {
|
||||
return null;
|
||||
}
|
||||
return (root, query, cb) -> cb.like(root.join("email").get("address"), "%" + address + "%");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
spring:
|
||||
datasource:
|
||||
url: jdbc:h2:mem:testdb
|
||||
initialization-mode: never
|
||||
|
||||
jpa:
|
||||
generate-ddl: true
|
||||
hibernate:
|
||||
ddl-auto: create
|
||||
|
||||
properties:
|
||||
hibernate:
|
||||
format_sql: false
|
||||
|
||||
show-sql: true
|
||||
|
||||
|
||||
logging:
|
||||
level:
|
||||
ROOT: ERROR
|
||||
@@ -0,0 +1,4 @@
|
||||
.idea/
|
||||
*.iml
|
||||
|
||||
target/
|
||||
@@ -0,0 +1,50 @@
|
||||
<?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>exercise</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.4.5</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-data-jpa</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,23 @@
|
||||
package ru.otus.springdata;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
|
||||
|
||||
|
||||
@SpringBootApplication
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
ConfigurableApplicationContext context = SpringApplication.run(Main.class);
|
||||
//PersonRepository personRepository = context.getBean(PersonRepository.class);
|
||||
//EmailRepository emailRepository = context.getBean(EmailRepository.class);
|
||||
|
||||
// personRepository.save(new Person("Александр Сергеевич Пушкин"));
|
||||
// personRepository.save(new Person("Михаил Юрьевич Лермонтов"));
|
||||
// personRepository.save(new Person("Михаил Сергеевич Горбачев"));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package ru.otus.springdata.domain;
|
||||
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class Email {
|
||||
|
||||
private long id;
|
||||
|
||||
private String address;
|
||||
|
||||
public Email(String address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package ru.otus.springdata.domain;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Entity
|
||||
public class Person {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private long id;
|
||||
|
||||
private String name;
|
||||
|
||||
public Person(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
package ru.otus.springdata.repository;
|
||||
|
||||
public interface EmailRepository {
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
package ru.otus.springdata.repository;
|
||||
|
||||
public interface PersonRepository {
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
spring:
|
||||
datasource:
|
||||
url: jdbc:h2:mem:testdb
|
||||
initialization-mode: never
|
||||
|
||||
jpa:
|
||||
generate-ddl: true
|
||||
hibernate:
|
||||
ddl-auto: create
|
||||
|
||||
properties:
|
||||
hibernate:
|
||||
format_sql: false
|
||||
|
||||
show-sql: true
|
||||
|
||||
|
||||
logging:
|
||||
level:
|
||||
ROOT: ERROR
|
||||
@@ -0,0 +1,21 @@
|
||||
<?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>spring-11-data-jpa</artifactId>
|
||||
<version>1.0</version>
|
||||
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<modules>
|
||||
<module>exercise</module>
|
||||
<module>solution-01</module>
|
||||
<module>solution-02</module>
|
||||
<module>solution-03</module>
|
||||
<module>solution-04</module>
|
||||
<module>demo</module>
|
||||
</modules>
|
||||
</project>
|
||||
@@ -0,0 +1,4 @@
|
||||
.idea/
|
||||
*.iml
|
||||
|
||||
target/
|
||||
@@ -0,0 +1,55 @@
|
||||
<?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>solution-01</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.4.5</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-data-jpa</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,37 @@
|
||||
package ru.otus.springdata;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import ru.otus.springdata.domain.Person;
|
||||
import ru.otus.springdata.repository.PersonRepository;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
ConfigurableApplicationContext context = SpringApplication.run(Main.class);
|
||||
|
||||
PersonRepository personRepository = context.getBean(PersonRepository.class);
|
||||
|
||||
|
||||
personRepository.save(new Person("Александр Сергеевич Пушкин"));
|
||||
personRepository.save(new Person("Михаил Юрьевич Лермонтов"));
|
||||
personRepository.save(new Person("Михаил Сергеевич Горбачев"));
|
||||
|
||||
System.out.println("\n\nИщем всех пёрсонов");
|
||||
System.out.println(personRepository.findAll().stream().map(Objects::toString)
|
||||
.collect(Collectors.joining("\n")));
|
||||
|
||||
System.out.println("\n\nИщем Пушкина");
|
||||
personRepository.findByName("Александр Сергеевич Пушкин")
|
||||
.ifPresent(System.out::println);
|
||||
|
||||
|
||||
System.out.println("\n\n");
|
||||
|
||||
}
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package ru.otus.springdata.domain;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class Email {
|
||||
|
||||
private long id;
|
||||
|
||||
private String address;
|
||||
|
||||
public Email(String address) {
|
||||
this.address = address;
|
||||
}
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package ru.otus.springdata.domain;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Entity
|
||||
public class Person {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private long id;
|
||||
|
||||
private String name;
|
||||
|
||||
public Person(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package ru.otus.springdata.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import ru.otus.springdata.domain.Email;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface EmailRepository extends JpaRepository<Email, Long> {
|
||||
|
||||
@Override
|
||||
List<Email> findAll();
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package ru.otus.springdata.repository;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import ru.otus.springdata.domain.Person;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface PersonRepository extends CrudRepository<Person, Long> {
|
||||
|
||||
@Override
|
||||
List<Person> findAll();
|
||||
|
||||
Optional<Person> findByName(String s);
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
spring:
|
||||
datasource:
|
||||
url: jdbc:h2:mem:testdb
|
||||
initialization-mode: never
|
||||
|
||||
jpa:
|
||||
generate-ddl: true
|
||||
hibernate:
|
||||
ddl-auto: create
|
||||
|
||||
properties:
|
||||
hibernate:
|
||||
format_sql: false
|
||||
|
||||
show-sql: true
|
||||
|
||||
|
||||
logging:
|
||||
level:
|
||||
ROOT: ERROR
|
||||
@@ -0,0 +1,4 @@
|
||||
.idea/
|
||||
*.iml
|
||||
|
||||
target/
|
||||
@@ -0,0 +1,55 @@
|
||||
<?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>solution-02</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.4.5</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-data-jpa</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,53 @@
|
||||
package ru.otus.springdata;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import ru.otus.springdata.domain.Email;
|
||||
import ru.otus.springdata.domain.Person;
|
||||
import ru.otus.springdata.repository.EmailRepository;
|
||||
import ru.otus.springdata.repository.PersonRepository;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
ConfigurableApplicationContext context = SpringApplication.run(Main.class);
|
||||
PersonRepository personRepository = context.getBean(PersonRepository.class);
|
||||
EmailRepository emailRepository = context.getBean(EmailRepository.class);
|
||||
|
||||
var pushkinEmail = new Email("alex.pushkin@mail.ru");
|
||||
var lermontovEmail = new Email("michail.lermontov@mail.ru");
|
||||
var gorbachevEmail = new Email("gorbachev@mail.ru");
|
||||
|
||||
var pushkin = new Person("Александр Сергеевич Пушкин");
|
||||
var lermontov = new Person("Михаил Юрьевич Лермонтов");
|
||||
var gorbachev = new Person("Михаил Сергеевич Горбачев");
|
||||
|
||||
emailRepository.save(pushkinEmail);
|
||||
emailRepository.save(lermontovEmail);
|
||||
emailRepository.save(gorbachevEmail);
|
||||
|
||||
personRepository.save(pushkin);
|
||||
personRepository.save(lermontov);
|
||||
personRepository.save(gorbachev);
|
||||
|
||||
System.out.println("\n\nИщем всех пёрсонов");
|
||||
System.out.println(personRepository.findAll().stream().map(Objects::toString)
|
||||
.collect(Collectors.joining("\n")));
|
||||
|
||||
System.out.println("\n\nИщем Пушкина");
|
||||
personRepository.findByName("Александр Сергеевич Пушкин")
|
||||
.ifPresent(System.out::println);
|
||||
|
||||
System.out.println("\n\nИщем все почты");
|
||||
System.out.println(emailRepository.findAll().stream().map(Objects::toString)
|
||||
.collect(Collectors.joining("\n")));
|
||||
|
||||
System.out.println("\n\n");
|
||||
}
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package ru.otus.springdata.domain;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Entity
|
||||
public class Email {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private long id;
|
||||
|
||||
private String address;
|
||||
|
||||
public Email(String address) {
|
||||
this.address = address;
|
||||
}
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package ru.otus.springdata.domain;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Entity
|
||||
public class Person {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private long id;
|
||||
|
||||
private String name;
|
||||
|
||||
public Person(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package ru.otus.springdata.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Modifying;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import ru.otus.springdata.domain.Email;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface EmailRepository extends JpaRepository<Email, Long>{
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package ru.otus.springdata.repository;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import ru.otus.springdata.domain.Person;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface PersonRepository extends CrudRepository<Person, Long> {
|
||||
|
||||
List<Person> findAll();
|
||||
|
||||
Optional<Person> findByName(String s);
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
spring:
|
||||
datasource:
|
||||
url: jdbc:h2:mem:testdb
|
||||
initialization-mode: never
|
||||
|
||||
jpa:
|
||||
generate-ddl: true
|
||||
hibernate:
|
||||
ddl-auto: create
|
||||
|
||||
properties:
|
||||
hibernate:
|
||||
format_sql: false
|
||||
|
||||
show-sql: true
|
||||
|
||||
|
||||
logging:
|
||||
level:
|
||||
ROOT: ERROR
|
||||
@@ -0,0 +1,4 @@
|
||||
.idea/
|
||||
*.iml
|
||||
|
||||
target/
|
||||
@@ -0,0 +1,55 @@
|
||||
<?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>solution-03</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.4.5</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-data-jpa</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,55 @@
|
||||
package ru.otus.springdata;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.data.domain.Example;
|
||||
import org.springframework.data.domain.ExampleMatcher;
|
||||
import ru.otus.springdata.domain.Email;
|
||||
import ru.otus.springdata.domain.Person;
|
||||
import ru.otus.springdata.repository.EmailRepository;
|
||||
import ru.otus.springdata.repository.PersonRepository;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
ConfigurableApplicationContext context = SpringApplication.run(Main.class);
|
||||
|
||||
PersonRepository personRepository = context.getBean(PersonRepository.class);
|
||||
EmailRepository emailRepository = context.getBean(EmailRepository.class);
|
||||
|
||||
var pushkin = new Person("Александр Сергеевич Пушкин", new Email("alex.pushkin@mail.ru"));
|
||||
var lermontov = new Person("Михаил Юрьевич Лермонтов", new Email("michail.lermontov@mail.ru"));
|
||||
var gorbachev = new Person("Михаил Сергеевич Горбачев", new Email("gorbachev@mail.ru"));
|
||||
|
||||
emailRepository.save(pushkin.getEmail());
|
||||
emailRepository.save(lermontov.getEmail());
|
||||
emailRepository.save(gorbachev.getEmail());
|
||||
|
||||
personRepository.save(pushkin);
|
||||
personRepository.save(lermontov);
|
||||
personRepository.save(gorbachev);
|
||||
|
||||
System.out.println("\n\nИщем всех пёрсонов");
|
||||
System.out.println(personRepository.findAll().stream().map(Objects::toString)
|
||||
.collect(Collectors.joining("\n")));
|
||||
|
||||
System.out.println("\n\nИщем Пушкина");
|
||||
personRepository.findByName("Александр Сергеевич Пушкин")
|
||||
.ifPresent(System.out::println);
|
||||
|
||||
System.out.println("\n\nИщем все почты");
|
||||
System.out.println(emailRepository.findAll().stream().map(Objects::toString)
|
||||
.collect(Collectors.joining("\n")));
|
||||
|
||||
System.out.println("\n\nИщем Пушкина по его почте");
|
||||
personRepository.findByEmailAddress("alex.pushkin@mail.ru")
|
||||
.ifPresent(System.out::println);
|
||||
|
||||
System.out.println("\n\n");
|
||||
}
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package ru.otus.springdata.domain;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Entity
|
||||
public class Email {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private long id;
|
||||
|
||||
private String address;
|
||||
|
||||
public Email(String address) {
|
||||
this.address = address;
|
||||
}
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package ru.otus.springdata.domain;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Entity
|
||||
public class Person {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private long id;
|
||||
|
||||
private String name;
|
||||
|
||||
@OneToOne(orphanRemoval = true)
|
||||
@JoinColumn(name = "email_id")
|
||||
private Email email;
|
||||
|
||||
public Person(String name, Email email) {
|
||||
this.name = name;
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package ru.otus.springdata.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Modifying;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import ru.otus.springdata.domain.Email;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface EmailRepository extends JpaRepository<Email, Long> {
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package ru.otus.springdata.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.EntityGraph;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import ru.otus.springdata.domain.Person;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface PersonRepository extends CrudRepository<Person, Long> {
|
||||
|
||||
@EntityGraph(attributePaths = "email")
|
||||
List<Person> findAll();
|
||||
|
||||
Optional<Person> findByName(String s);
|
||||
|
||||
Optional<Person> findByEmailAddress(String email);
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
spring:
|
||||
datasource:
|
||||
url: jdbc:h2:mem:testdb
|
||||
initialization-mode: never
|
||||
|
||||
jpa:
|
||||
generate-ddl: true
|
||||
hibernate:
|
||||
ddl-auto: create
|
||||
|
||||
properties:
|
||||
hibernate:
|
||||
format_sql: false
|
||||
|
||||
show-sql: true
|
||||
|
||||
|
||||
logging:
|
||||
level:
|
||||
ROOT: ERROR
|
||||
@@ -0,0 +1,4 @@
|
||||
.idea/
|
||||
*.iml
|
||||
|
||||
target/
|
||||
@@ -0,0 +1,55 @@
|
||||
<?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>solution-04</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.4.5</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-data-jpa</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,64 @@
|
||||
package ru.otus.springdata;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.data.domain.Example;
|
||||
import org.springframework.data.domain.ExampleMatcher;
|
||||
import ru.otus.springdata.domain.Email;
|
||||
import ru.otus.springdata.domain.Person;
|
||||
import ru.otus.springdata.repository.EmailRepository;
|
||||
import ru.otus.springdata.repository.PersonRepository;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
ConfigurableApplicationContext context = SpringApplication.run(Main.class);
|
||||
|
||||
PersonRepository personRepository = context.getBean(PersonRepository.class);
|
||||
EmailRepository emailRepository = context.getBean(EmailRepository.class);
|
||||
|
||||
var pushkin = new Person("Александр Сергеевич Пушкин", new Email("alex.pushkin@mail.ru"));
|
||||
var lermontov = new Person("Михаил Юрьевич Лермонтов", new Email("michail.lermontov@mail.ru"));
|
||||
var gorbachev = new Person("Михаил Сергеевич Горбачев", new Email("gorbachev@mail.ru"));
|
||||
|
||||
emailRepository.save(pushkin.getEmail());
|
||||
emailRepository.save(lermontov.getEmail());
|
||||
emailRepository.save(gorbachev.getEmail());
|
||||
|
||||
personRepository.save(pushkin);
|
||||
personRepository.save(lermontov);
|
||||
personRepository.save(gorbachev);
|
||||
|
||||
System.out.println("\n\nИщем всех пёрсонов");
|
||||
System.out.println(personRepository.findAll().stream().map(Objects::toString)
|
||||
.collect(Collectors.joining("\n")));
|
||||
|
||||
System.out.println("\n\nИщем Пушкина");
|
||||
personRepository.findByName("Александр Сергеевич Пушкин")
|
||||
.ifPresent(System.out::println);
|
||||
|
||||
System.out.println("\n\nИщем все почты");
|
||||
System.out.println(emailRepository.findAll().stream().map(Objects::toString)
|
||||
.collect(Collectors.joining("\n")));
|
||||
|
||||
System.out.println("\n\nИщем Пушкина по его почте");
|
||||
personRepository.findByEmailAddress("alex.pushkin@mail.ru")
|
||||
.ifPresent(System.out::println);
|
||||
|
||||
System.out.println("\n\nОбновляем почту Лермонтову");
|
||||
System.out.println("До обновления: " + lermontov.getEmail());
|
||||
emailRepository.updateEmailById(lermontov.getId(), "michail1984@lermontov.ru");
|
||||
|
||||
System.out.println("\n\nИщем почту Лермонтова по новому адресу");
|
||||
emailRepository.findByEmailAddress("michail1984@lermontov.ru")
|
||||
.ifPresent(e -> System.out.println("После обновления: " + e));
|
||||
|
||||
System.out.println("\n\n");
|
||||
|
||||
}
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package ru.otus.springdata.domain;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Entity
|
||||
public class Email {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private long id;
|
||||
|
||||
private String address;
|
||||
|
||||
public Email(String address) {
|
||||
this.address = address;
|
||||
}
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package ru.otus.springdata.domain;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Entity
|
||||
public class Person {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private long id;
|
||||
|
||||
private String name;
|
||||
|
||||
@OneToOne(orphanRemoval = true)
|
||||
@JoinColumn(name = "email_id")
|
||||
private Email email;
|
||||
|
||||
public Person(String name, Email email) {
|
||||
this.name = name;
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package ru.otus.springdata.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Modifying;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import ru.otus.springdata.domain.Email;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface EmailRepository extends JpaRepository<Email, Long> {
|
||||
|
||||
@Query("select e from Email e where e.address = :address")
|
||||
Optional<Email> findByEmailAddress(@Param("address") String email);
|
||||
|
||||
@Modifying
|
||||
@Transactional
|
||||
@Query("update Email e set e.address = :address where e.id = :id")
|
||||
void updateEmailById(@Param("id") long id, @Param("address") String address);
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package ru.otus.springdata.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.EntityGraph;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import ru.otus.springdata.domain.Person;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface PersonRepository extends CrudRepository<Person, Long> {
|
||||
|
||||
@EntityGraph(attributePaths = "email")
|
||||
List<Person> findAll();
|
||||
|
||||
Optional<Person> findByName(String s);
|
||||
|
||||
Optional<Person> findByEmailAddress(String email);
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
spring:
|
||||
datasource:
|
||||
url: jdbc:h2:mem:testdb
|
||||
initialization-mode: never
|
||||
|
||||
jpa:
|
||||
generate-ddl: true
|
||||
hibernate:
|
||||
ddl-auto: create
|
||||
|
||||
properties:
|
||||
hibernate:
|
||||
format_sql: false
|
||||
|
||||
show-sql: true
|
||||
|
||||
|
||||
logging:
|
||||
level:
|
||||
ROOT: ERROR
|
||||
@@ -0,0 +1,17 @@
|
||||
<?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>spring-data-keyvalue-class-work</artifactId>
|
||||
<version>1.0</version>
|
||||
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<modules>
|
||||
<module>spring-data-keyvalue-exercise</module>
|
||||
<module>spring-data-keyvalue-solution</module>
|
||||
</modules>
|
||||
</project>
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
<?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>spring-data-keyvalue-exercise</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.2.1.RELEASE</version>
|
||||
<relativePath />
|
||||
</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.data</groupId>-->
|
||||
<!--<artifactId>spring-data-keyvalue</artifactId>-->
|
||||
<!--<version>2.2.1.RELEASE</version>-->
|
||||
<!--</dependency>-->
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
package ru.otus.spring;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.data.map.repository.config.EnableMapRepositories;
|
||||
import ru.otus.spring.domain.Person;
|
||||
import ru.otus.spring.repostory.PersonRepository;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Main.class);
|
||||
}
|
||||
|
||||
@SuppressWarnings("SpringJavaAutowiredFieldsWarningInspection")
|
||||
@Autowired
|
||||
private PersonRepository repository;
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
repository.save(new Person(1, "Pushkin"));
|
||||
|
||||
repository.findAll();
|
||||
}
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package ru.otus.spring.domain;
|
||||
|
||||
public class Email {
|
||||
|
||||
private int id;
|
||||
|
||||
private String email;
|
||||
|
||||
public Email(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public Email(int id, String email) {
|
||||
this.id = id;
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
package ru.otus.spring.domain;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.keyvalue.annotation.KeySpace;
|
||||
|
||||
public class Person {
|
||||
|
||||
private int id;
|
||||
private String name;
|
||||
|
||||
public Person(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Person(int id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package ru.otus.spring.repostory;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import ru.otus.spring.domain.Person;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface PersonRepository extends CrudRepository<Person, Integer> {
|
||||
|
||||
List<Person> findAll();
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
<?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>spring-data-keyvalue-solution</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.2.1.RELEASE</version>
|
||||
<relativePath />
|
||||
</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.data</groupId>
|
||||
<artifactId>spring-data-keyvalue</artifactId>
|
||||
<version>2.2.1.RELEASE</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
package ru.otus.spring;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.data.map.repository.config.EnableMapRepositories;
|
||||
import ru.otus.spring.domain.Email;
|
||||
import ru.otus.spring.domain.Person;
|
||||
import ru.otus.spring.repostory.EmailRepository;
|
||||
import ru.otus.spring.repostory.PersonRepository;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableMapRepositories
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Main.class);
|
||||
}
|
||||
|
||||
@SuppressWarnings("SpringJavaAutowiredFieldsWarningInspection")
|
||||
@Autowired
|
||||
private PersonRepository repository;
|
||||
|
||||
@Autowired
|
||||
private EmailRepository emailRepository;
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
repository.save(new Person(1, "Pushkin"));
|
||||
repository.save(new Person(2, "Lermontov"));
|
||||
System.out.println(repository.findAll());
|
||||
|
||||
emailRepository.save(new Email(1, "alex@pushkin.com"));
|
||||
emailRepository.save(new Email(2, "micha@pushkin.com"));
|
||||
System.out.println(emailRepository.findAll());
|
||||
}
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
package ru.otus.spring.domain;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.keyvalue.annotation.KeySpace;
|
||||
|
||||
@KeySpace("email")
|
||||
public class Email {
|
||||
@Id
|
||||
private int id;
|
||||
|
||||
private String email;
|
||||
|
||||
public Email(int id, String email) {
|
||||
this.id = id;
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public Email(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Email{" +
|
||||
"id=" + id +
|
||||
", email='" + email + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
package ru.otus.spring.domain;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.keyvalue.annotation.KeySpace;
|
||||
|
||||
@KeySpace("person")
|
||||
public class Person {
|
||||
|
||||
@Id
|
||||
private int id;
|
||||
private String name;
|
||||
|
||||
public Person(int id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Person(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Person{" +
|
||||
"id=" + id +
|
||||
", name='" + name + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package ru.otus.spring.repostory;
|
||||
|
||||
import org.springframework.data.keyvalue.repository.KeyValueRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
import ru.otus.spring.domain.Email;
|
||||
import ru.otus.spring.domain.Person;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface EmailRepository {
|
||||
|
||||
List<Email> findAll();
|
||||
Email save(Email email);
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package ru.otus.spring.repostory;
|
||||
|
||||
import org.springframework.data.keyvalue.core.KeyValueOperations;
|
||||
import org.springframework.stereotype.Repository;
|
||||
import ru.otus.spring.domain.Email;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
public class EmailRepositoryImpl implements EmailRepository {
|
||||
|
||||
final private KeyValueOperations keyValueTemplate;
|
||||
|
||||
public EmailRepositoryImpl(KeyValueOperations keyValueTemplate) {
|
||||
this.keyValueTemplate = keyValueTemplate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Email> findAll() {
|
||||
return (List<Email>) keyValueTemplate.findAll(Email.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Email save(Email email) {
|
||||
return keyValueTemplate.insert(email);
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package ru.otus.spring.repostory;
|
||||
|
||||
import org.springframework.data.keyvalue.repository.KeyValueRepository;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import ru.otus.spring.domain.Person;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface PersonRepository extends KeyValueRepository<Person, Integer> {
|
||||
|
||||
List<Person> findAll();
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?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>spring-data-mongo-class-work</artifactId>
|
||||
<version>1.0</version>
|
||||
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<modules>
|
||||
<module>spring-data-mongo-exercise</module>
|
||||
<module>spring-data-mongo-solution</module>
|
||||
</modules>
|
||||
</project>
|
||||
@@ -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>
|
||||
|
||||
<groupId>ru.otus</groupId>
|
||||
<artifactId>spring-data-mongo-exercise</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.3.4.RELEASE</version>
|
||||
<relativePath />
|
||||
</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-data-mongodb</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>de.flapdoodle.embed</groupId>
|
||||
<artifactId>de.flapdoodle.embed.mongo</artifactId>
|
||||
</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.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import ru.otus.spring.domain.Person;
|
||||
import ru.otus.spring.repostory.PersonRepository;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Main {
|
||||
|
||||
@SuppressWarnings("SpringJavaAutowiredFieldsWarningInspection")
|
||||
@Autowired
|
||||
private PersonRepository repository;
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
ApplicationContext context = SpringApplication.run(Main.class);
|
||||
|
||||
PersonRepository repository = context.getBean(PersonRepository.class);
|
||||
|
||||
repository.save(new Person("Dostoevsky"));
|
||||
|
||||
Thread.sleep(3000);
|
||||
|
||||
System.out.println("\n\n\n----------------------------------------------\n\n");
|
||||
System.out.println("Авторы в БД:");
|
||||
repository.findAll().forEach(p -> System.out.println(p.getName()));
|
||||
System.out.println("\n\n----------------------------------------------\n\n\n");
|
||||
}
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package ru.otus.spring.domain;
|
||||
|
||||
public class Person {
|
||||
|
||||
private String id;
|
||||
private String name;
|
||||
|
||||
public Person(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package ru.otus.spring.repostory;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import ru.otus.spring.domain.Person;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
public interface PersonRepository extends CrudRepository<Person, Integer> {
|
||||
|
||||
List<Person> findAll();
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
spring:
|
||||
data:
|
||||
mongodb:
|
||||
database: company
|
||||
@@ -0,0 +1,61 @@
|
||||
<?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>spring-data-mongo-solution</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.3.4.RELEASE</version>
|
||||
<relativePath />
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
<mongock.version>4.1.17</mongock.version>
|
||||
</properties>
|
||||
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-mongodb</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>de.flapdoodle.embed</groupId>
|
||||
<artifactId>de.flapdoodle.embed.mongo</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.github.cloudyrock.mongock</groupId>
|
||||
<artifactId>mongock-spring-v5</artifactId>
|
||||
<version>${mongock.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.github.cloudyrock.mongock</groupId>
|
||||
<artifactId>mongodb-springdata-v3-driver</artifactId>
|
||||
<version>${mongock.version}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
package ru.otus.spring;
|
||||
|
||||
import com.github.cloudyrock.spring.v5.EnableMongock;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
|
||||
import ru.otus.spring.domain.Person;
|
||||
import ru.otus.spring.repostory.PersonRepository;
|
||||
|
||||
@EnableMongock
|
||||
@EnableMongoRepositories
|
||||
@SpringBootApplication
|
||||
public class Main {
|
||||
|
||||
@SuppressWarnings("SpringJavaAutowiredFieldsWarningInspection")
|
||||
@Autowired
|
||||
private PersonRepository repository;
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
ApplicationContext context = SpringApplication.run(Main.class);
|
||||
|
||||
PersonRepository repository = context.getBean(PersonRepository.class);
|
||||
|
||||
repository.save(new Person("Dostoevsky"));
|
||||
|
||||
Thread.sleep(3000);
|
||||
|
||||
System.out.println("\n\n\n----------------------------------------------\n\n");
|
||||
System.out.println("Авторы в БД:");
|
||||
repository.findAll().forEach(p -> System.out.println(p.getName()));
|
||||
System.out.println("\n\n----------------------------------------------\n\n\n");
|
||||
}
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package ru.otus.spring.domain;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.mongodb.core.mapping.Document;
|
||||
|
||||
@Document(collection = "persons")
|
||||
public class Person {
|
||||
|
||||
@Id
|
||||
private String id;
|
||||
private String name;
|
||||
|
||||
public Person(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package ru.otus.spring.mongock.changelog;
|
||||
|
||||
import com.github.cloudyrock.mongock.ChangeLog;
|
||||
import com.github.cloudyrock.mongock.ChangeSet;
|
||||
import com.mongodb.client.MongoCollection;
|
||||
import com.mongodb.client.MongoDatabase;
|
||||
import org.bson.Document;
|
||||
import ru.otus.spring.domain.Person;
|
||||
import ru.otus.spring.repostory.PersonRepository;
|
||||
|
||||
@ChangeLog
|
||||
public class DatabaseChangelog {
|
||||
|
||||
@ChangeSet(order = "001", id = "dropDb", author = "stvort", runAlways = true)
|
||||
public void dropDb(MongoDatabase db) {
|
||||
db.drop();
|
||||
}
|
||||
|
||||
@ChangeSet(order = "002", id = "insertLermontov", author = "ydvorzhetskiy")
|
||||
public void insertLermontov(MongoDatabase db) {
|
||||
MongoCollection<Document> myCollection = db.getCollection("persons");
|
||||
var doc = new Document().append("name", "Lermontov");
|
||||
myCollection.insertOne(doc);
|
||||
}
|
||||
|
||||
@ChangeSet(order = "003", id = "insertPushkin", author = "stvort")
|
||||
public void insertPushkin(PersonRepository repository) {
|
||||
repository.save(new Person("Pushkin"));
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package ru.otus.spring.repostory;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import ru.otus.spring.domain.Person;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
public interface PersonRepository extends CrudRepository<Person, Integer> {
|
||||
|
||||
List<Person> findAll();
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
spring:
|
||||
data:
|
||||
mongodb:
|
||||
database: company
|
||||
|
||||
mongock:
|
||||
runner-type: "ApplicationRunner" # default
|
||||
#runner-type: "InitializingBean"
|
||||
change-logs-scan-package:
|
||||
- ru.otus.spring.mongock.changelog
|
||||
@@ -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>spring-mvc-class-work</artifactId>
|
||||
<version>1.0</version>
|
||||
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<modules>
|
||||
<module>spring-mvc-exercise</module>
|
||||
<module>spring-mvc-solution-1</module>
|
||||
<module>spring-mvc-solution-2</module>
|
||||
<module>spring-mvc-solution-3</module>
|
||||
<module>spring-mvc-demo</module>
|
||||
</modules>
|
||||
</project>
|
||||
@@ -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>spring-mvc-demo</artifactId>
|
||||
<version>1.0</version>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.7.5</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<java.version>11</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
</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,26 @@
|
||||
package ru.otus.spring;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import ru.otus.spring.domain.Person;
|
||||
import ru.otus.spring.repostory.PersonRepository;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Main.class);
|
||||
}
|
||||
|
||||
@SuppressWarnings("SpringJavaAutowiredFieldsWarningInspection")
|
||||
@Autowired
|
||||
private PersonRepository repository;
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
repository.save(new Person(1, "Pushkin"));
|
||||
}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package ru.otus.spring.config;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
import ru.otus.spring.rest.resolvers.SystemInfoMethodArgumentResolver;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Configuration
|
||||
public class WebConfig implements WebMvcConfigurer {
|
||||
|
||||
@Autowired
|
||||
private SystemInfoMethodArgumentResolver systemInfoMethodArgumentResolver;
|
||||
|
||||
@Override
|
||||
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
|
||||
resolvers.add(systemInfoMethodArgumentResolver);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package ru.otus.spring.domain;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class Person {
|
||||
|
||||
@Id
|
||||
private long id;
|
||||
private String name;
|
||||
|
||||
public Person() {
|
||||
}
|
||||
|
||||
public Person(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Person(long id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package ru.otus.spring.domain;
|
||||
|
||||
public class SystemInfo {
|
||||
private final String osName;
|
||||
private final String timeZone;
|
||||
private final String osArch;
|
||||
private final int processorsCount;
|
||||
|
||||
public SystemInfo(String osName, String timeZone, String osArch, int processorsCount) {
|
||||
this.osName = osName;
|
||||
this.timeZone = timeZone;
|
||||
this.osArch = osArch;
|
||||
this.processorsCount = processorsCount;
|
||||
}
|
||||
|
||||
public String getOsName() {
|
||||
return osName;
|
||||
}
|
||||
|
||||
public String getTimeZone() {
|
||||
return timeZone;
|
||||
}
|
||||
|
||||
public String getOsArch() {
|
||||
return osArch;
|
||||
}
|
||||
|
||||
public int getProcessorsCount() {
|
||||
return processorsCount;
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package ru.otus.spring.repostory;
|
||||
|
||||
import org.springframework.data.repository.PagingAndSortingRepository;
|
||||
import ru.otus.spring.domain.Person;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface PersonRepository extends PagingAndSortingRepository<Person, Long> {
|
||||
|
||||
List<Person> findAll();
|
||||
List<Person> findByName(String name);
|
||||
}
|
||||
+64
@@ -0,0 +1,64 @@
|
||||
package ru.otus.spring.rest;
|
||||
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import ru.otus.spring.domain.Person;
|
||||
import ru.otus.spring.repostory.PersonRepository;
|
||||
import ru.otus.spring.rest.dto.PersonDto;
|
||||
import ru.otus.spring.rest.exceptions.NotFoundException;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@RestController
|
||||
public class PersonController {
|
||||
|
||||
private final PersonRepository repository;
|
||||
|
||||
public PersonController(PersonRepository repository) {
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/persons", method = RequestMethod.GET)
|
||||
public List<PersonDto> getAllPersons() {
|
||||
return repository.findAll().stream()
|
||||
.map(PersonDto::toDto)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/persons", method = RequestMethod.GET, params = "name")
|
||||
public PersonDto getPersonByNameInRequest(@RequestParam("name") String name) {
|
||||
Person person = repository.findByName(name).stream().findFirst().orElseThrow(NotFoundException::new);
|
||||
return PersonDto.toDto(person);
|
||||
}
|
||||
|
||||
@GetMapping("/persons/{id}")
|
||||
public PersonDto getPersonByIdInPath(@PathVariable("id") long id) {
|
||||
Person person = repository.findById(id).orElseThrow(NotFoundException::new);
|
||||
return PersonDto.toDto(person);
|
||||
}
|
||||
|
||||
@PostMapping("/persons")
|
||||
public PersonDto createNewPerson(@RequestBody PersonDto dto) {
|
||||
Person person = PersonDto.toDomainObject(dto);
|
||||
Person savedPerson = repository.save(person);
|
||||
return PersonDto.toDto(savedPerson);
|
||||
}
|
||||
|
||||
@PatchMapping("/persons/{id}/name")
|
||||
public PersonDto updateNameById(@PathVariable("id") long id, @RequestParam("name") String name) {
|
||||
Person person = repository.findById(id).orElseThrow(NotFoundException::new);
|
||||
person.setName(name);
|
||||
return PersonDto.toDto(repository.save(person));
|
||||
}
|
||||
|
||||
@DeleteMapping("/persons/{id}")
|
||||
public void deleteById(@PathVariable("id") long id) {
|
||||
repository.deleteById(id);
|
||||
}
|
||||
|
||||
@ExceptionHandler(NotFoundException.class)
|
||||
public ResponseEntity<String> handleNotFound(NotFoundException ex) {
|
||||
return ResponseEntity.badRequest().body("Таких тут нет!");
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package ru.otus.spring.rest;
|
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import ru.otus.spring.domain.SystemInfo;
|
||||
|
||||
@RestController
|
||||
public class SystemInfoController {
|
||||
|
||||
@GetMapping("/server/system/info")
|
||||
public SystemInfo getServerSystemInfo(SystemInfo systemInfo) {
|
||||
return systemInfo;
|
||||
}
|
||||
}
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright 2016 Russian Post
|
||||
*
|
||||
* This source code is Russian Post Confidential Proprietary.
|
||||
* This software is protected by copyright. All rights and titles are reserved.
|
||||
* You shall not use, copy, distribute, modify, decompile, disassemble or reverse engineer the software.
|
||||
* Otherwise this violation would be treated by law and would be subject to legal prosecution.
|
||||
* Legal use of the software provides receipt of a license from the right name only.
|
||||
*/
|
||||
package ru.otus.spring.rest.dto;
|
||||
|
||||
import ru.otus.spring.domain.Person;
|
||||
|
||||
/**
|
||||
* DTO that represents Person
|
||||
*/
|
||||
@SuppressWarnings("all")
|
||||
public class PersonDto {
|
||||
|
||||
private long id;
|
||||
private String name;
|
||||
|
||||
public PersonDto() {
|
||||
}
|
||||
|
||||
public PersonDto(long id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public static Person toDomainObject(PersonDto dto) {
|
||||
return new Person(dto.getId(), dto.getName());
|
||||
}
|
||||
|
||||
public static PersonDto toDto(Person person) {
|
||||
return new PersonDto(person.getId(), person.getName());
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package ru.otus.spring.rest.exceptions;
|
||||
|
||||
public class NotFoundException extends RuntimeException{
|
||||
|
||||
public NotFoundException() {
|
||||
}
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package ru.otus.spring.rest.resolvers;
|
||||
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.bind.support.WebDataBinderFactory;
|
||||
import org.springframework.web.context.request.NativeWebRequest;
|
||||
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
|
||||
import org.springframework.web.method.support.ModelAndViewContainer;
|
||||
import ru.otus.spring.domain.SystemInfo;
|
||||
import ru.otus.spring.service.SystemInfoService;
|
||||
|
||||
@Component
|
||||
public class SystemInfoMethodArgumentResolver implements HandlerMethodArgumentResolver {
|
||||
|
||||
private final SystemInfoService systemInfoService;
|
||||
|
||||
public SystemInfoMethodArgumentResolver(SystemInfoService systemInfoService) {
|
||||
this.systemInfoService = systemInfoService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supportsParameter(MethodParameter parameter) {
|
||||
return parameter.getParameterType().equals(SystemInfo.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object resolveArgument(MethodParameter parameter,
|
||||
ModelAndViewContainer mavContainer,
|
||||
NativeWebRequest webRequest,
|
||||
WebDataBinderFactory binderFactory) throws Exception {
|
||||
return systemInfoService.getSystemInfo();
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package ru.otus.spring.service;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.otus.spring.domain.SystemInfo;
|
||||
|
||||
@Service
|
||||
public class SystemInfoService {
|
||||
|
||||
public SystemInfo getSystemInfo(){
|
||||
String osName = System.getProperty("os.name");
|
||||
String timeZone = System.getProperty("user.timezone");
|
||||
String osArch = System.getProperty("os.arch");
|
||||
int processorsCount = Runtime.getRuntime().availableProcessors();
|
||||
return new SystemInfo(osName, timeZone, osArch, processorsCount);
|
||||
|
||||
}
|
||||
}
|
||||
+127
@@ -0,0 +1,127 @@
|
||||
package ru.otus.spring.rest;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import ru.otus.spring.domain.Person;
|
||||
import ru.otus.spring.repostory.PersonRepository;
|
||||
import ru.otus.spring.rest.dto.PersonDto;
|
||||
import ru.otus.spring.service.SystemInfoService;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.springframework.http.MediaType.APPLICATION_JSON;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
@WebMvcTest(PersonController.class)
|
||||
class PersonControllerTest {
|
||||
|
||||
public static final String ERROR_STRING = "Таких тут нет!";
|
||||
|
||||
@Autowired
|
||||
private MockMvc mvc;
|
||||
|
||||
@Autowired
|
||||
private ObjectMapper mapper;
|
||||
|
||||
@MockBean
|
||||
private PersonRepository repository;
|
||||
|
||||
@MockBean
|
||||
private SystemInfoService systemInfoService;
|
||||
|
||||
@Test
|
||||
void shouldReturnCorrectPersonsList() throws Exception {
|
||||
List<Person> persons = List.of(new Person(1, "Person1"), new Person(2, "Person2"));
|
||||
given(repository.findAll()).willReturn(persons);
|
||||
|
||||
List<PersonDto> expectedResult = persons.stream()
|
||||
.map(PersonDto::toDto).collect(Collectors.toList());
|
||||
|
||||
mvc.perform(get("/persons"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().json(mapper.writeValueAsString(expectedResult)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReturnCorrectPersonByNameInRequest() throws Exception {
|
||||
Person person = new Person(1, "Person1");
|
||||
given(repository.findByName(person.getName())).willReturn(List.of(person));
|
||||
PersonDto expectedResult = PersonDto.toDto(person);
|
||||
|
||||
mvc.perform(get("/persons").param("name", person.getName()))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().json(mapper.writeValueAsString(expectedResult)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReturnCorrectPersonByIdInPath() throws Exception {
|
||||
Person person = new Person(1, "Person1");
|
||||
given(repository.findById(1L)).willReturn(Optional.of(person));
|
||||
PersonDto expectedResult = PersonDto.toDto(person);
|
||||
|
||||
mvc.perform(get("/persons/1"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().json(mapper.writeValueAsString(expectedResult)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReturnExpectedErrorWhenPersonNotFound() throws Exception {
|
||||
given(repository.findById(1L)).willReturn(Optional.empty());
|
||||
|
||||
mvc.perform(get("/persons").param("name", "Person1"))
|
||||
.andExpect(status().isBadRequest())
|
||||
.andExpect(content().string(ERROR_STRING));
|
||||
|
||||
mvc.perform(get("/persons/1"))
|
||||
.andExpect(status().isBadRequest())
|
||||
.andExpect(content().string(ERROR_STRING));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldCorrectSaveNewPerson() throws Exception {
|
||||
Person person = new Person(1, "Person1");
|
||||
given(repository.save(any())).willReturn(person);
|
||||
String expectedResult = mapper.writeValueAsString(PersonDto.toDto(person));
|
||||
|
||||
mvc.perform(post("/persons").contentType(APPLICATION_JSON)
|
||||
.content(expectedResult))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().json(expectedResult));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldCorrectUpdatePersonName() throws Exception {
|
||||
Person person = new Person(1, "Person1");
|
||||
given(repository.findById(1L)).willReturn(Optional.of(person));
|
||||
given(repository.save(any())).willAnswer(invocation -> invocation.getArgument(0));
|
||||
|
||||
Person expectedPerson = new Person(1, "Person2");
|
||||
String expectedResult = mapper.writeValueAsString(PersonDto.toDto(expectedPerson));
|
||||
|
||||
mvc.perform(patch("/persons/{id}/name", 1).param("name", expectedPerson.getName())
|
||||
.content(expectedResult))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().json(expectedResult));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldCorrectDeletePerson() throws Exception {
|
||||
mvc.perform(delete("/persons/1"))
|
||||
.andExpect(status().isOk());
|
||||
verify(repository, times(1)).deleteById(1L);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
package ru.otus.spring.rest;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import ru.otus.spring.domain.SystemInfo;
|
||||
import ru.otus.spring.repostory.PersonRepository;
|
||||
import ru.otus.spring.service.SystemInfoService;
|
||||
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
||||
|
||||
@WebMvcTest(SystemInfoController.class)
|
||||
@Import(SystemInfoService.class)
|
||||
class SystemInfoControllerTest {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mvc;
|
||||
|
||||
@Autowired
|
||||
private ObjectMapper mapper;
|
||||
|
||||
@MockBean
|
||||
private PersonRepository repository;
|
||||
|
||||
@Autowired
|
||||
private SystemInfoService systemInfoService;
|
||||
|
||||
@Test
|
||||
void shouldReturnCorrectServerSystemInfo() throws Exception {
|
||||
SystemInfo expectedSystemInfo = systemInfoService.getSystemInfo();
|
||||
mvc.perform(get("/server/system/info"))
|
||||
.andExpect(content().json(mapper.writeValueAsString(expectedSystemInfo)));
|
||||
}
|
||||
}
|
||||
@@ -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>
|
||||
|
||||
<groupId>ru.otus</groupId>
|
||||
<artifactId>spring-mvc-exercise</artifactId>
|
||||
<version>1.0</version>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.7.5</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<java.version>11</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
</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,26 @@
|
||||
package ru.otus.spring;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import ru.otus.spring.domain.Person;
|
||||
import ru.otus.spring.repostory.PersonRepository;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Main.class);
|
||||
}
|
||||
|
||||
@SuppressWarnings("SpringJavaAutowiredFieldsWarningInspection")
|
||||
@Autowired
|
||||
private PersonRepository repository;
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
repository.save(new Person(1, "Pushkin"));
|
||||
}
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
package ru.otus.spring.domain;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class Person {
|
||||
|
||||
@Id
|
||||
private long id;
|
||||
private String name;
|
||||
|
||||
public Person() {
|
||||
}
|
||||
|
||||
public Person(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Person(long id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package ru.otus.spring.repostory;
|
||||
|
||||
import org.springframework.data.repository.PagingAndSortingRepository;
|
||||
import ru.otus.spring.domain.Person;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface PersonRepository extends PagingAndSortingRepository<Person, Long> {
|
||||
|
||||
List<Person> findAll();
|
||||
List<Person> findByName(String name);
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package ru.otus.spring.rest;
|
||||
|
||||
import ru.otus.spring.repostory.PersonRepository;
|
||||
|
||||
|
||||
public class PersonController {
|
||||
|
||||
private final PersonRepository repository;
|
||||
|
||||
public PersonController(PersonRepository repository) {
|
||||
this.repository = repository;
|
||||
}
|
||||
}
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright 2016 Russian Post
|
||||
*
|
||||
* This source code is Russian Post Confidential Proprietary.
|
||||
* This software is protected by copyright. All rights and titles are reserved.
|
||||
* You shall not use, copy, distribute, modify, decompile, disassemble or reverse engineer the software.
|
||||
* Otherwise this violation would be treated by law and would be subject to legal prosecution.
|
||||
* Legal use of the software provides receipt of a license from the right name only.
|
||||
*/
|
||||
package ru.otus.spring.rest.dto;
|
||||
|
||||
import ru.otus.spring.domain.Person;
|
||||
|
||||
/**
|
||||
* DTO that represents Person
|
||||
*/
|
||||
@SuppressWarnings("all")
|
||||
public class PersonDto {
|
||||
|
||||
private long id;
|
||||
private String name;
|
||||
|
||||
public PersonDto() {
|
||||
}
|
||||
|
||||
public PersonDto(long id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public static Person toDomainObject(PersonDto dto) {
|
||||
return new Person(dto.getId(), dto.getName());
|
||||
}
|
||||
|
||||
public static PersonDto toDto(Person person) {
|
||||
return new PersonDto(person.getId(), person.getName());
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package ru.otus.spring.rest.exeptions;
|
||||
|
||||
public class NotFoundException extends RuntimeException{
|
||||
|
||||
public NotFoundException() {
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user