Skip to content

Commit

Permalink
crudrepository and query methods
Browse files Browse the repository at this point in the history
  • Loading branch information
myluckagain committed Aug 25, 2020
1 parent 32f015e commit 84e5285
Show file tree
Hide file tree
Showing 8 changed files with 199 additions and 0 deletions.
63 changes: 63 additions & 0 deletions spring-data-jdbc-2/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>ru.sysout</groupId>
<artifactId>spring-data-jdbc</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-data-jdbc</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>11</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

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

}
Original file line number Diff line number Diff line change
@@ -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<Animal, Long> {

@Query("select count(*) from animal")
int animalCount();

Animal findByName(String name);

Animal findFirstByName(String name);

List<Animal> findByNameNotContaining(String str);

}
Original file line number Diff line number Diff line change
@@ -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;
}
}
9 changes: 9 additions & 0 deletions spring-data-jdbc-2/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
spring:
h2:
console:
enabled: true
path: /h2-console
datasource:
url: jdbc:h2:mem:testdb
username: sa
password:
2 changes: 2 additions & 0 deletions spring-data-jdbc-2/src/main/resources/data.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
insert into animal (name) values ('cat');
insert into animal (name) values ('dog');
5 changes: 5 additions & 0 deletions spring-data-jdbc-2/src/main/resources/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
DROP TABLE IF EXISTS ANIMAL;
CREATE TABLE IF NOT EXISTS ANIMAL(
ID BIGINT AUTO_INCREMENT PRIMARY KEY,
NAME VARCHAR(255)
);
Original file line number Diff line number Diff line change
@@ -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<Animal> 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<Animal> 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<Animal> animals = dao.findByNameNotContaining("cat");
Assertions.assertEquals(1, animals.size());
}

}

0 comments on commit 84e5285

Please sign in to comment.