Skip to content

Commit e4e4b96

Browse files
author
Nikita Konev
committed
working example with Page
1 parent a833eff commit e4e4b96

File tree

6 files changed

+40
-4
lines changed

6 files changed

+40
-4
lines changed

src/main/kotlin/com/example/web/jdbc/web/jdbc/Application.kt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import org.springframework.boot.ApplicationArguments
55
import org.springframework.boot.ApplicationRunner
66
import org.springframework.boot.autoconfigure.SpringBootApplication
77
import org.springframework.boot.runApplication
8+
import org.springframework.data.domain.PageRequest
9+
import org.springframework.data.domain.Pageable
810
import org.springframework.stereotype.Component
911

1012
@SpringBootApplication
@@ -15,8 +17,11 @@ fun main(args: Array<String>) {
1517
}
1618

1719
@Component
18-
class AppRunner(private val subjectRepository: SubjectRepository,
19-
private val branchRepository: BranchRepository) : ApplicationRunner {
20+
class AppRunner(
21+
private val subjectRepository: SubjectRepository,
22+
private val branchRepository: BranchRepository,
23+
private val personRepository: PersonRepository
24+
) : ApplicationRunner {
2025

2126
private val logger = LoggerFactory.getLogger(this::class.java)
2227

@@ -59,5 +64,9 @@ class AppRunner(private val subjectRepository: SubjectRepository,
5964
logger.info("Checking if subjects still presents")
6065
val allSubjects = subjectRepository.findAll()
6166
allSubjects.forEach { logger.info("Found subject {}", it) }
67+
68+
// https://docs.spring.io/spring-data/jdbc/docs/current/reference/html/#jdbc.query-methods
69+
val foundPersons = personRepository.findByLastName("Doe", PageRequest.of(1, 10))
70+
foundPersons.forEach { logger.info("Found Person {}", it) }
6271
}
6372
}

src/main/kotlin/com/example/web/jdbc/web/jdbc/Entities.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,5 @@ data class Branch(
2727
}
2828

2929
data class BranchData(val buildingType: String?, var rating: Int, var comment: String?)
30+
31+
data class Person (val id: Long, var firstName: String, var lastName: String)
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.example.web.jdbc.web.jdbc
22

3+
import org.springframework.data.domain.Page
4+
import org.springframework.data.domain.Pageable
35
import org.springframework.data.repository.CrudRepository
46
import org.springframework.stereotype.Repository
57

@@ -8,4 +10,9 @@ import org.springframework.stereotype.Repository
810
interface SubjectRepository : CrudRepository<Subject, Long>
911

1012
@Repository
11-
interface BranchRepository : CrudRepository<Branch, Long>
13+
interface BranchRepository : CrudRepository<Branch, Long>
14+
15+
@Repository
16+
interface PersonRepository : CrudRepository<Person, Long> {
17+
fun findByLastName(lastname: String, pageable: Pageable) : Page<Person>
18+
}

src/main/resources/db/changelog.yml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,16 @@ databaseChangeLog:
1010
author: nkonev
1111
changes:
1212
- sqlFile:
13-
path: /db/changelog/1648459518__branch_data_jsonb.sql
13+
path: /db/changelog/1648459518__branch_data_jsonb.sql
14+
- changeSet:
15+
id: 3
16+
author: nkonev
17+
changes:
18+
- sqlFile:
19+
path: /db/changelog/1648633719__person.sql
20+
- changeSet:
21+
id: 4
22+
author: nkonev
23+
changes:
24+
- sqlFile:
25+
path: /db/changelog/1648633720__person_doe.sql
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
CREATE TABLE person(id BIGSERIAL PRIMARY KEY, first_name text not null , last_name text not null);
2+
3+
INSERT INTO person (first_name, last_name)
4+
SELECT 'John' || i, 'Doe' || i FROM generate_series(0, 200) AS i;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
INSERT INTO person (first_name, last_name)
2+
SELECT 'John' || i, 'Doe' FROM generate_series(0, 50) AS i;

0 commit comments

Comments
 (0)