-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
32f015e
commit 84e5285
Showing
8 changed files
with
199 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
14 changes: 14 additions & 0 deletions
14
spring-data-jdbc-2/src/main/java/ru/sysout/springdatajdbc/SpringDataJdbcApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
spring-data-jdbc-2/src/main/java/ru/sysout/springdatajdbc/dao/AnimalRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
spring-data-jdbc-2/src/main/java/ru/sysout/springdatajdbc/model/Animal.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
); |
65 changes: 65 additions & 0 deletions
65
spring-data-jdbc-2/src/test/java/ru/sysout/springdatajdbc/AnimalRepositoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |