Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version = "0.0.1-SNAPSHOT"

buildscript {

ext{
ext {
springBootVersion = "1.5.2.RELEASE"
kotlinVersion = "1.1.0"
}
Expand Down Expand Up @@ -59,5 +59,14 @@ dependencies {
compile('mysql:mysql-connector-java:5.1.13')

testCompile("junit:junit")

//thymeleaf
compile("org.springframework.boot:spring-boot-starter-thymeleaf")
//WebJars
compile("org.webjars:bootstrap:3.3.4")
compile("org.webjars:jquery:2.1.4")
// https://mvnrepository.com/artifact/org.webjars/datatables
compile group: 'org.webjars', name: 'datatables', version: '1.10.13'

}

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package jason.chen.mini_springboot.restful

import jason.chen.mini_springboot.restful.biz.CustomerRepository
import jason.chen.mini_springboot.restful.biz.CustomerService
import jason.chen.mini_springboot.restful.entity.Customer
import org.slf4j.LoggerFactory
import org.springframework.boot.CommandLineRunner
Expand All @@ -15,7 +15,7 @@ class Application {
private val log = LoggerFactory.getLogger(Application::class.java)

@Bean
fun init(repository: CustomerRepository) = CommandLineRunner {
fun init(repository: CustomerService) = CommandLineRunner {
// save a couple of customers
val now = Date();
repository.save(Customer("Jason", "Chen", now, now, 0, now))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package jason.chen.mini_springboot.restful.biz
import jason.chen.mini_springboot.restful.entity.Customer
import org.springframework.data.repository.CrudRepository

interface CustomerRepository : CrudRepository<Customer, Long> {
interface CustomerService : CrudRepository<Customer, Long> {

fun findByLastName(lastName: String): Iterable<Customer>
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,43 @@
package jason.chen.mini_springboot.restful.controller

import jason.chen.mini_springboot.restful.biz.CustomerRepository
import jason.chen.mini_springboot.restful.biz.CustomerService
import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.bind.annotation.ResponseBody

@RestController
class CustomerController(val repository: CustomerRepository) {
/**
* * 注意:
* 这里要使用@Controller注解
* 而不要使用@RestController
* 否则return "index";只是返回字符串"index", 不能跳转到index.html
*
* @RestController is a stereotype annotation that combines @ResponseBody and @Controller.
* @RestController注解相当于@ResponseBody + @Controller合在一起的作用。
*/

@GetMapping("/")
fun findAll() = repository.findAll()
@Controller
class CustomerController(val customerService: CustomerService) {

@GetMapping("/{lastName}")
fun findByLastName(@PathVariable lastName:String)
= repository.findByLastName(lastName)
}
@GetMapping(value = "/")
fun index(): String {
return "index"
}

@GetMapping("/customers.do")
fun listAll(model: Model): String {
val allCustomers = customerService.findAll()
model.addAttribute("customers", allCustomers)
return "customers"
}

@GetMapping("/listCustomers")
@ResponseBody
fun listCustomers(model: Model) = customerService.findAll()

@GetMapping("/{lastName}")
@ResponseBody
fun findByLastName(@PathVariable lastName: String)
= customerService.findByLastName(lastName)
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ class Customer(
@Id @GeneratedValue(strategy = GenerationType.AUTO)
val id: Long = -1) {
override fun toString(): String {
return "Customer(id=$id, firstName='$firstName', lastName='$lastName')"
return "Customer(firstName='$firstName', lastName='$lastName', gmtCreated=$gmtCreated, gmtModified=$gmtModified, isDeleted=$isDeleted, deletedDate=$deletedDate, id=$id)"
}
}
19 changes: 18 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,21 @@ spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

server.port=9891
server.port=9891


########################################################
###THYMELEAF (ThymeleafAutoConfiguration) 一般情况下,不需要进行配置,使用SpringBoot默认配置(如下所示)即可
########################################################
## 前缀prefix
#spring.thymeleaf.prefix=classpath:templates/
## 后缀suffix
#spring.thymeleaf.suffix=.html
## 类型mode
#spring.thymeleaf.mode=HTML5
## charset=<encoding> is added
#spring.thymeleaf.encoding=UTF-8
##content-type
#spring.thymeleaf.content-type=text/html
# set to false for hot refresh
#spring.thymeleaf.cache=false ##由于浏览器会有页面缓存,在开发过程中,建议开发者不启用cache
Loading