Skip to content

Commit 6bbe5b7

Browse files
committed
add save to db
1 parent 330db8f commit 6bbe5b7

10 files changed

+95
-2
lines changed

pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@
4444
<groupId>org.springframework.boot</groupId>
4545
<artifactId>spring-boot-starter-mustache</artifactId>
4646
</dependency>
47+
<!-- data jpa and db -->
48+
<dependency>
49+
<groupId>org.springframework.boot</groupId>
50+
<artifactId>spring-boot-starter-data-jpa</artifactId>
51+
</dependency>
52+
<dependency>
53+
<groupId>com.h2database</groupId>
54+
<artifactId>h2</artifactId>
55+
<scope>runtime</scope>
56+
</dependency>
4757
</dependencies>
4858

4959
<build>

src/main/scala/com/edurt/ssi/SpringBootScalaIntegration.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@ package com.edurt.ssi
22

33
import org.springframework.boot.SpringApplication
44
import org.springframework.boot.autoconfigure.SpringBootApplication
5+
import org.springframework.boot.autoconfigure.domain.EntityScan
56
import org.springframework.context.annotation.ComponentScan
67

78
@SpringBootApplication
89
@ComponentScan(value = Array(
9-
"com.edurt.ssi.controller",
10-
"com.edurt.ssi.view"
10+
"com.edurt.ssi"
11+
))
12+
@EntityScan(value = Array(
13+
"com.edurt.ssi.model"
1114
))
1215
class SpringBootScalaIntegration
1316

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.edurt.ssi.controller
2+
3+
import com.edurt.ssi.model.UserModel
4+
import com.edurt.ssi.service.UserService
5+
import org.springframework.beans.factory.annotation.Autowired
6+
import org.springframework.web.bind.annotation.{PathVariable, PostMapping, RequestMapping, RestController}
7+
8+
@RestController
9+
@RequestMapping(value = Array("user"))
10+
class UserController @Autowired()(
11+
val userService: UserService
12+
) {
13+
14+
@PostMapping(value = Array("save/{name}"))
15+
def save(@PathVariable name: String): Long = {
16+
val userModel = {
17+
new UserModel()
18+
}
19+
userModel.name = name
20+
return this.userService.save(userModel).id
21+
}
22+
23+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.edurt.ssi.model
2+
3+
import javax.persistence.{Entity, GeneratedValue, Id}
4+
5+
@Entity
6+
class UserModel {
7+
8+
@Id
9+
@GeneratedValue
10+
var id: Long = 0
11+
12+
var name: String = null
13+
14+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package com.edurt.ssi.model;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.edurt.ssi.service
2+
3+
import com.edurt.ssi.model.UserModel
4+
5+
trait UserService {
6+
7+
/**
8+
* save model to db
9+
*/
10+
def save(model: UserModel): UserModel;
11+
12+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.edurt.ssi.service
2+
3+
import com.edurt.ssi.model.UserModel
4+
import com.edurt.ssi.support.UserSupport
5+
import org.springframework.beans.factory.annotation.Autowired
6+
import org.springframework.stereotype.Service
7+
8+
@Service(value = "userService")
9+
class UserServiceImpl @Autowired() (
10+
val userSupport: UserSupport
11+
) extends UserService {
12+
13+
/**
14+
* save model to db
15+
*/
16+
override def save(model: UserModel): UserModel = {
17+
return this.userSupport.save(model)
18+
}
19+
20+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package com.edurt.ssi.service;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.edurt.ssi.support
2+
3+
import com.edurt.ssi.model.UserModel
4+
import org.springframework.data.repository.PagingAndSortingRepository
5+
6+
trait UserSupport extends PagingAndSortingRepository[UserModel, Long] {
7+
8+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package com.edurt.ssi.support;

0 commit comments

Comments
 (0)