From 84e528590662852888449786a1835279c3a84418 Mon Sep 17 00:00:00 2001 From: myluckagain Date: Tue, 25 Aug 2020 22:56:56 +0500 Subject: [PATCH] crudrepository and query methods --- spring-data-jdbc-2/pom.xml | 63 ++++++++++++++++++ .../SpringDataJdbcApplication.java | 14 ++++ .../springdatajdbc/dao/AnimalRepository.java | 20 ++++++ .../sysout/springdatajdbc/model/Animal.java | 21 ++++++ .../src/main/resources/application.yml | 9 +++ .../src/main/resources/data.sql | 2 + .../src/main/resources/schema.sql | 5 ++ .../springdatajdbc/AnimalRepositoryTest.java | 65 +++++++++++++++++++ 8 files changed, 199 insertions(+) create mode 100644 spring-data-jdbc-2/pom.xml create mode 100644 spring-data-jdbc-2/src/main/java/ru/sysout/springdatajdbc/SpringDataJdbcApplication.java create mode 100644 spring-data-jdbc-2/src/main/java/ru/sysout/springdatajdbc/dao/AnimalRepository.java create mode 100644 spring-data-jdbc-2/src/main/java/ru/sysout/springdatajdbc/model/Animal.java create mode 100644 spring-data-jdbc-2/src/main/resources/application.yml create mode 100644 spring-data-jdbc-2/src/main/resources/data.sql create mode 100644 spring-data-jdbc-2/src/main/resources/schema.sql create mode 100644 spring-data-jdbc-2/src/test/java/ru/sysout/springdatajdbc/AnimalRepositoryTest.java diff --git a/spring-data-jdbc-2/pom.xml b/spring-data-jdbc-2/pom.xml new file mode 100644 index 0000000..7caea6e --- /dev/null +++ b/spring-data-jdbc-2/pom.xml @@ -0,0 +1,63 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.3.3.RELEASE + + + ru.sysout + spring-data-jdbc + 0.0.1-SNAPSHOT + spring-data-jdbc + Demo project for Spring Boot + + + 11 + + + + + org.springframework.boot + spring-boot-starter-data-jdbc + + + org.springframework.boot + spring-boot-starter-web + + + + com.h2database + h2 + runtime + + + org.projectlombok + lombok + true + + + org.springframework.boot + spring-boot-starter-test + test + + + org.junit.vintage + junit-vintage-engine + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/spring-data-jdbc-2/src/main/java/ru/sysout/springdatajdbc/SpringDataJdbcApplication.java b/spring-data-jdbc-2/src/main/java/ru/sysout/springdatajdbc/SpringDataJdbcApplication.java new file mode 100644 index 0000000..f94ef37 --- /dev/null +++ b/spring-data-jdbc-2/src/main/java/ru/sysout/springdatajdbc/SpringDataJdbcApplication.java @@ -0,0 +1,14 @@ +package ru.sysout.springdatajdbc; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SpringDataJdbcApplication { + + + public static void main(String[] args) { + SpringApplication.run(SpringDataJdbcApplication.class, args); + } + +} diff --git a/spring-data-jdbc-2/src/main/java/ru/sysout/springdatajdbc/dao/AnimalRepository.java b/spring-data-jdbc-2/src/main/java/ru/sysout/springdatajdbc/dao/AnimalRepository.java new file mode 100644 index 0000000..74752b5 --- /dev/null +++ b/spring-data-jdbc-2/src/main/java/ru/sysout/springdatajdbc/dao/AnimalRepository.java @@ -0,0 +1,20 @@ +package ru.sysout.springdatajdbc.dao; + +import org.springframework.data.jdbc.repository.query.Query; +import org.springframework.data.repository.PagingAndSortingRepository; +import ru.sysout.springdatajdbc.model.Animal; + +import java.util.List; + +public interface AnimalRepository extends PagingAndSortingRepository { + + @Query("select count(*) from animal") + int animalCount(); + + Animal findByName(String name); + + Animal findFirstByName(String name); + + List findByNameNotContaining(String str); + +} diff --git a/spring-data-jdbc-2/src/main/java/ru/sysout/springdatajdbc/model/Animal.java b/spring-data-jdbc-2/src/main/java/ru/sysout/springdatajdbc/model/Animal.java new file mode 100644 index 0000000..d6116a8 --- /dev/null +++ b/spring-data-jdbc-2/src/main/java/ru/sysout/springdatajdbc/model/Animal.java @@ -0,0 +1,21 @@ +package ru.sysout.springdatajdbc.model; + +import lombok.Data; +import lombok.NoArgsConstructor; +import org.springframework.data.annotation.Id; + +@Data +@NoArgsConstructor +public class Animal { + @Id + private long id; + private String name; + + public Animal(long id, String name) { + this.id = id; + this.name = name; + } + public Animal(String name) { + this.name = name; + } +} diff --git a/spring-data-jdbc-2/src/main/resources/application.yml b/spring-data-jdbc-2/src/main/resources/application.yml new file mode 100644 index 0000000..23e95f7 --- /dev/null +++ b/spring-data-jdbc-2/src/main/resources/application.yml @@ -0,0 +1,9 @@ +spring: + h2: + console: + enabled: true + path: /h2-console + datasource: + url: jdbc:h2:mem:testdb + username: sa + password: \ No newline at end of file diff --git a/spring-data-jdbc-2/src/main/resources/data.sql b/spring-data-jdbc-2/src/main/resources/data.sql new file mode 100644 index 0000000..b1ed876 --- /dev/null +++ b/spring-data-jdbc-2/src/main/resources/data.sql @@ -0,0 +1,2 @@ +insert into animal (name) values ('cat'); +insert into animal (name) values ('dog'); \ No newline at end of file diff --git a/spring-data-jdbc-2/src/main/resources/schema.sql b/spring-data-jdbc-2/src/main/resources/schema.sql new file mode 100644 index 0000000..48136a0 --- /dev/null +++ b/spring-data-jdbc-2/src/main/resources/schema.sql @@ -0,0 +1,5 @@ +DROP TABLE IF EXISTS ANIMAL; +CREATE TABLE IF NOT EXISTS ANIMAL( + ID BIGINT AUTO_INCREMENT PRIMARY KEY, + NAME VARCHAR(255) + ); diff --git a/spring-data-jdbc-2/src/test/java/ru/sysout/springdatajdbc/AnimalRepositoryTest.java b/spring-data-jdbc-2/src/test/java/ru/sysout/springdatajdbc/AnimalRepositoryTest.java new file mode 100644 index 0000000..f636e74 --- /dev/null +++ b/spring-data-jdbc-2/src/test/java/ru/sysout/springdatajdbc/AnimalRepositoryTest.java @@ -0,0 +1,65 @@ +package ru.sysout.springdatajdbc; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.data.jdbc.DataJdbcTest; +import ru.sysout.springdatajdbc.dao.AnimalRepository; +import ru.sysout.springdatajdbc.model.Animal; + +import java.util.List; +import java.util.Optional; + +@DataJdbcTest +public class AnimalRepositoryTest { + @Autowired + private AnimalRepository dao; + + @Test + void givenId_whenFindThenReturnsAnimal() { + Optional optionalAnimal = dao.findById(1l); + Assertions.assertTrue(!optionalAnimal.isEmpty()); + optionalAnimal.get().setName("cat"); + } + + @Test + void givenAnimal_whenSaveThenReturnsAnimal() { + Animal newAnimal=new Animal("mouse"); + Animal animal = dao.save(newAnimal); + Assertions.assertSame(animal, newAnimal); + Assertions.assertNotNull(animal.getId()); + } + + @Test + void givenId_whenUpdateThenReturnsAnimal() { + Optional optionalAnimal = dao.findById(1l); + optionalAnimal.get().setName("cat1"); + Animal updatedAnimal=dao.save(optionalAnimal.get()); + Assertions.assertEquals("cat1", updatedAnimal.getName()); + } + + @Test + void whenCount_thenCountIsTwo() { + long count = dao.count(); + Assertions.assertEquals(2 , count); + } + + @Test + void givenName_whenFindBy_thenReturnsAnimal() { + Animal animal = dao.findByName("cat"); + Assertions.assertEquals("cat", animal.getName()); + } + + @Test + void givenName_whenFindFirstBy_thenReturnsAnimal() { + Animal animal = dao.findFirstByName("cat"); + Assertions.assertEquals("cat", animal.getName()); + } + + @Test + void givenName_whenFindByNameNotContaining_thenCorrect() { + List animals = dao.findByNameNotContaining("cat"); + Assertions.assertEquals(1, animals.size()); + } + +}