Skip to content

Commit 0b8a3e4

Browse files
author
Peter Vestlin
committed
Lade till DB-anrop.
1 parent a79682d commit 0b8a3e4

File tree

9 files changed

+161
-18
lines changed

9 files changed

+161
-18
lines changed

build.gradle

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ buildscript {
1212
}
1313
}
1414

15+
apply plugin: 'java'
1516
apply plugin: 'eclipse'
1617
apply plugin: 'idea'
1718
apply plugin: 'kotlin' // Required for Kotlin integration
@@ -30,7 +31,20 @@ repositories {
3031
dependencies {
3132
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" // Required for Kotlin integration
3233
compile 'org.springframework.boot:spring-boot-starter-web'
34+
compile 'org.springframework.boot:spring-boot-starter-jdbc'
35+
compile "org.springframework:spring-tx"
36+
compile "org.springframework:spring-jdbc"
37+
compile "com.h2database:h2:1.4.191"
38+
compile "javax.inject:javax.inject:1"
39+
compile "org.springframework.boot:spring-boot-starter-actuator"
40+
compile "com.google.guava:guava:19.0"
41+
42+
compile "org.springframework.boot:spring-boot-devtools"
43+
44+
3345
testCompile 'junit:junit'
46+
47+
3448
}
3549

3650
springBoot {

src/main/kotlin/nu/westlin/kartrepo/Application.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ open class Application {
1010

1111
@Bean
1212
open fun init(repository: KartRepository) = CommandLineRunner {
13-
repository.store(User("pwestlin", "Peter", "Westlin"))
14-
repository.store(User("awestlin", "Adam", "Westlin"))
13+
repository.store(Driver("pwestlin", "Peter", "Westlin"))
14+
repository.store(Driver("awestlin", "Adam", "Westlin"))
15+
repository.store(Driver("fwestlin", "Felix", "Westlin"))
1516
}
1617

1718
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package nu.westlin.kartrepo
2+
3+
import org.springframework.context.annotation.Bean
4+
import org.springframework.context.annotation.Configuration
5+
import org.springframework.jdbc.core.JdbcOperations
6+
import org.springframework.jdbc.core.JdbcTemplate
7+
import org.springframework.jdbc.datasource.DataSourceTransactionManager
8+
import org.springframework.transaction.PlatformTransactionManager
9+
import javax.inject.Inject
10+
import javax.sql.DataSource
11+
12+
/**
13+
* Database configuration.
14+
*/
15+
@Configuration
16+
open class DatabaseConfiguration {
17+
18+
@Inject
19+
lateinit protected var dataSource: DataSource
20+
21+
@Bean
22+
open fun transactionManager(): PlatformTransactionManager {
23+
return DataSourceTransactionManager(this.dataSource)
24+
}
25+
26+
@Bean
27+
open fun jdbcOperations(): JdbcOperations {
28+
return JdbcTemplate(this.dataSource)
29+
}
30+
31+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package nu.westlin.kartrepo
2+
3+
data class Driver(val alias: String, val firstname: String, val lastname: String)

src/main/kotlin/nu/westlin/kartrepo/KartController.kt

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,45 @@
11
package nu.westlin.kartrepo
22

33
import org.springframework.beans.factory.annotation.Autowired
4+
import org.springframework.dao.IncorrectResultSizeDataAccessException
45
import org.springframework.http.HttpStatus
6+
import org.springframework.jdbc.core.JdbcOperations
57
import org.springframework.web.bind.annotation.*
6-
import javax.servlet.http.HttpServletResponse
78

89
@RestController
9-
class KartController @Autowired constructor(val kartRepository: KartRepository) {
10+
open class KartController @Autowired constructor(val kartRepository: KartRepository, val jdbcOperations: JdbcOperations) {
11+
12+
init {
13+
println("jdbcOperations = ${jdbcOperations}")
14+
println("kartRepository = ${kartRepository}")
15+
println("kartRepository.jdbcOperations = ${kartRepository.jdbcOperations}")
16+
// Jag får inte jdbcOperations att sättas automatiskt :(
17+
kartRepository.jdbcOperations = jdbcOperations
18+
println("kartRepository.jdbcOperations = ${kartRepository.jdbcOperations}")
19+
}
1020

1121

1222
@RequestMapping("/user")
13-
fun greeting(@RequestParam(value = "username", defaultValue = "pwestlin") username: String, response: HttpServletResponse): User {
14-
return kartRepository.load(username) ?: throw NotFoundException("User $username not found")
23+
fun greeting(@RequestParam(value = "username", defaultValue = "pwestlin") username: String): Driver {
24+
return kartRepository.load(username)
1525
}
1626

1727
@RequestMapping("/users")
18-
fun greeting(): List<User> {
19-
return listOf(User("pwestlin", "Peter", "Westlin"))
28+
fun greeting(): List<Driver> {
29+
return listOf(Driver("pwestlin", "Peter", "Westlin"))
30+
}
31+
32+
@ExceptionHandler(NotFoundException::class)
33+
@ResponseBody
34+
@ResponseStatus(value = HttpStatus.NOT_FOUND)
35+
fun notFoundException(e: NotFoundException): ErrorResource {
36+
return ErrorResource(HttpStatus.NOT_FOUND.value(), "Could not find resource")
2037
}
2138

22-
@ExceptionHandler(Exception::class)
39+
@ExceptionHandler(IncorrectResultSizeDataAccessException::class)
2340
@ResponseBody
2441
@ResponseStatus(value = HttpStatus.NOT_FOUND)
25-
fun handleException(e: NotFoundException, response: HttpServletResponse): ErrorResource {
42+
fun incorrectResultSizeDataAccessException(e: IncorrectResultSizeDataAccessException): ErrorResource {
2643
return ErrorResource(HttpStatus.NOT_FOUND.value(), "Could not find resource")
2744
}
2845
}
Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,40 @@
11
package nu.westlin.kartrepo
22

3+
import org.springframework.beans.factory.annotation.Autowired
4+
import org.springframework.jdbc.JdbcUpdateAffectedIncorrectNumberOfRowsException
5+
import org.springframework.jdbc.core.JdbcOperations
6+
import org.springframework.jdbc.core.RowMapper
37
import org.springframework.stereotype.Repository
8+
import org.springframework.transaction.PlatformTransactionManager
9+
import java.sql.ResultSet
10+
import java.sql.SQLException
411

512
@Repository
6-
class KartRepository {
13+
// jdbcOperations ska vara val och inte var men jag får inte jdbcOperations att sättas automatiskt :(
14+
open class KartRepository @Autowired constructor(var jdbcOperations: JdbcOperations, val transactionManager: PlatformTransactionManager) {
715

8-
var users: MutableMap<String, User> = mutableMapOf()
16+
fun store(user: Driver) {
17+
val rows: Int = jdbcOperations.update("insert into driver(alias, firstname, lastname) values(?,?,?)",
18+
user.alias, user.firstname, user.lastname)
19+
if (rows != 1)
20+
throw JdbcUpdateAffectedIncorrectNumberOfRowsException("Expected 1 row, got $rows", 0, rows)
21+
}
22+
23+
fun load(alias: String): Driver {
24+
return jdbcOperations.queryForObject("select alias,firstname,lastname from driver where alias=?", UserRowMapper(), alias)
25+
}
26+
27+
class UserRowMapper : RowMapper<Driver> {
28+
29+
@Throws(SQLException::class)
30+
override fun mapRow(rs: ResultSet, rowNum: Int): Driver {
31+
return Driver(
32+
rs.getString("alias"),
33+
rs.getString("firstname"),
34+
rs.getString("lastname")
35+
)
36+
}
937

10-
fun store(user: User) {
11-
users.put(user.username, user)
1238
}
1339

14-
fun load(username: String) = users.get(username)
1540
}

src/main/kotlin/nu/westlin/kartrepo/User.kt

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/main/resources/application.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
spring:
2+
application:
3+
name: kartrepo
4+
5+
jackson:
6+
serialization:
7+
indent_output: true
8+
9+
datasource:
10+
url: jdbc:h2:mem:test;MODE=PostgreSQL;DB_CLOSE_ON_EXIT=FALSE
11+
driver-class-name: org.h2.Driver
12+
initialSize: 1
13+
max-active: 5
14+
maxIdle: 5
15+
initialize: false
16+
17+
info.app:
18+
name: ${archivesBaseName}
19+
description: ${description}
20+
version: ${version}
21+
22+
23+
endpoints.health.sensitive: false
24+
25+
#logging.path: ./logs
26+
27+
28+
#embedded.server.tomcat.accesslog.enabled: true
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
databaseChangeLog:
2+
3+
4+
- changeSet:
5+
id: 1
6+
author: petves
7+
changes:
8+
- createTable:
9+
tableName: DRIVER
10+
columns:
11+
- column:
12+
name: ALIAS
13+
type: VARCHAR(20)
14+
constraints:
15+
primaryKey: true
16+
nullable: false
17+
- column:
18+
name: FIRSTNAME
19+
type: VARCHAR(36)
20+
constraints:
21+
nullable: false
22+
- column:
23+
name: LASTNAME
24+
type: VARCHAR(36)
25+
constraints:
26+
nullable: false
27+

0 commit comments

Comments
 (0)