Danh sách mặc định lớp dữ liệu python

Hướng dẫn này chỉ cho bạn cách xây dựng một ứng dụng blog mẫu hiệu quả bằng cách kết hợp sức mạnh của Spring Boot và Kotlin

Nếu bạn đang bắt đầu với Kotlin, bạn có thể học ngôn ngữ này bằng cách đọc tài liệu tham khảo, làm theo hướng dẫn Kotlin Koans trực tuyến hoặc chỉ sử dụng tài liệu tham khảo Spring Framework hiện cung cấp các mẫu mã trong Kotlin

Hỗ trợ Spring Kotlin được ghi lại trong tài liệu tham khảo Spring Framework và Spring Boot. Nếu bạn cần trợ giúp, hãy tìm kiếm hoặc đặt câu hỏi với các thẻ

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
7 và
dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
8 trên StackOverflow hoặc tham gia thảo luận trên kênh
dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
9 của Kotlin Slack

Tạo một dự án mới

Đầu tiên chúng ta cần tạo một ứng dụng Spring Boot, có thể thực hiện theo một số cách

Sử dụng trang web Initializr

Truy cập https. //bắt đầu. mùa xuân. io và chọn ngôn ngữ Kotlin. Gradle là công cụ xây dựng được sử dụng phổ biến nhất trong Kotlin và nó cung cấp Kotlin DSL được sử dụng theo mặc định khi tạo dự án Kotlin, vì vậy đây là lựa chọn được đề xuất. Nhưng bạn cũng có thể sử dụng Maven nếu cảm thấy thoải mái hơn với nó. Lưu ý rằng bạn có thể sử dụng https. //bắt đầu. mùa xuân. io/#. language=kotlin&type=gradle-project để chọn Kotlin và Gradle theo mặc định

  1. Chọn "Dự án lớp" hoặc để "Dự án Maven" mặc định tùy thuộc vào công cụ xây dựng bạn muốn sử dụng

  2. Nhập tọa độ hiện vật sau.

    spring.jpa.properties.hibernate.globally_quoted_identifiers=true
    spring.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions = true
    0

  3. Thêm các phụ thuộc sau

    • web mùa xuân

    • Ria

    • Dữ liệu mùa xuân JPA

    • Cơ sở dữ liệu H2

    • Spring Boot DevTools

  4. Nhấp vào "Tạo dự án"

Các. zip chứa một dự án tiêu chuẩn trong thư mục gốc, vì vậy bạn có thể muốn tạo một thư mục trống trước khi giải nén nó

Sử dụng dòng lệnh

Ví dụ, bạn có thể sử dụng API HTTP Initializr từ dòng lệnh với curl trên hệ thống giống như UN*X

$ mkdir blog && cd blog
$ curl //start.spring.io/starter.zip -d language=kotlin -d dependencies=web,mustache,jpa,h2,devtools -d packageName=com.example.blog -d name=Blog -o blog.zip

Thêm

spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions = true
1 nếu bạn muốn sử dụng Gradle

Sử dụng IntelliJ IDEA

Spring Initializr cũng được tích hợp trong phiên bản IntelliJ IDEA Ultimate và cho phép bạn tạo và nhập một dự án mới mà không cần phải thoát khỏi IDE cho dòng lệnh hoặc giao diện người dùng web

Để truy cập trình hướng dẫn, hãy chuyển đến Tệp. Mới. Project và chọn Spring Initializr

Làm theo các bước của trình hướng dẫn để sử dụng các tham số sau

  • Hiện vật. "Blog"

  • Loại hình. Dự án Maven hoặc Dự án Gradle

  • Ngôn ngữ. Kotlin

  • Tên. "Blog"

  • phụ thuộc. "Trình khởi động web mùa xuân", "Mustache", "JPA dữ liệu mùa xuân", "Cơ sở dữ liệu H2" và "Công cụ dành cho nhà phát triển khởi động mùa xuân"

Hiểu bản dựng Gradle

Nếu bạn đang sử dụng Maven Build, bạn có thể bỏ qua phần dành riêng

bổ sung

Ngoài plugin Kotlin Gradle rõ ràng, cấu hình mặc định khai báo plugin kotlin-spring tự động mở các lớp và phương thức [không giống như trong Java, vòng loại mặc định là

spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions = true
2 trong Kotlin] được chú thích hoặc siêu chú thích với các chú thích Spring. Điều này rất hữu ích để có thể tạo đậu
spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions = true
3 hoặc
spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions = true
4 mà không cần phải thêm bộ hạn định
spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions = true
5 theo yêu cầu của proxy CGLIB chẳng hạn

Để có thể sử dụng các thuộc tính không thể vô hiệu hóa của Kotlin với JPA, plugin Kotlin JPA cũng được bật. Nó tạo ra các hàm tạo không có đối số cho bất kỳ lớp nào được chú thích bằng

spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions = true
6,
spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions = true
7 hoặc
spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions = true
8

spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions = true
9

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
4

Tùy chọn trình biên dịch

Một trong những tính năng chính của Kotlin là null-safety - xử lý rõ ràng các giá trị

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
50 tại thời điểm biên dịch thay vì chạm vào
dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
51 nổi tiếng trong thời gian chạy. Điều này làm cho các ứng dụng an toàn hơn thông qua các tuyên bố về tính vô hiệu và thể hiện ngữ nghĩa "có giá trị hoặc không có giá trị" mà không phải trả chi phí cho các trình bao bọc như
dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
52. Lưu ý rằng Kotlin cho phép sử dụng các cấu trúc chức năng có giá trị nullable;

Mặc dù Java không cho phép một người thể hiện tính an toàn không có giá trị trong hệ thống kiểu của nó, nhưng Spring Framework cung cấp tính an toàn không có giá trị cho toàn bộ API Spring Framework thông qua các chú thích thân thiện với công cụ được khai báo trong gói

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
53. Theo mặc định, các loại từ API Java được sử dụng trong Kotlin được công nhận là loại nền tảng để kiểm tra null được thoải mái. Hỗ trợ của Kotlin cho các chú thích JSR 305 + Các chú thích về khả năng vô hiệu của Spring cung cấp tính an toàn vô hiệu cho toàn bộ API Spring Framework cho các nhà phát triển Kotlin, với lợi thế xử lý các vấn đề liên quan đến
dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
50 tại thời điểm biên dịch

Tính năng này có thể được kích hoạt bằng cách thêm cờ trình biên dịch

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
55 với các tùy chọn
dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
56

spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions = true
9

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
3

phụ thuộc

3 thư viện cụ thể của Kotlin được yêu cầu cho ứng dụng web Spring Boot đó và được định cấu hình theo mặc định

  • dependencies {
      implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
      implementation["org.springframework.boot:spring-boot-starter-mustache"]
      implementation["org.springframework.boot:spring-boot-starter-web"]
      implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
      implementation["org.jetbrains.kotlin:kotlin-reflect"]
      implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
      runtimeOnly["com.h2database:h2"]
      runtimeOnly["org.springframework.boot:spring-boot-devtools"]
      testImplementation["org.springframework.boot:spring-boot-starter-test"]
    }
    58 là biến thể Java 8 của thư viện chuẩn Kotlin

  • dependencies {
      implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
      implementation["org.springframework.boot:spring-boot-starter-mustache"]
      implementation["org.springframework.boot:spring-boot-starter-web"]
      implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
      implementation["org.jetbrains.kotlin:kotlin-reflect"]
      implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
      runtimeOnly["com.h2database:h2"]
      runtimeOnly["org.springframework.boot:spring-boot-devtools"]
      testImplementation["org.springframework.boot:spring-boot-starter-test"]
    }
    59 là thư viện phản chiếu Kotlin

  • dependencies {
      implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
      implementation["org.springframework.boot:spring-boot-starter-mustache"]
      implementation["org.springframework.boot:spring-boot-starter-web"]
      implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
      implementation["org.jetbrains.kotlin:kotlin-reflect"]
      implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
      runtimeOnly["com.h2database:h2"]
      runtimeOnly["org.springframework.boot:spring-boot-devtools"]
      testImplementation["org.springframework.boot:spring-boot-starter-test"]
    }
    60 thêm hỗ trợ tuần tự hóa/giải tuần tự hóa các lớp Kotlin và lớp dữ liệu [các lớp hàm tạo đơn lẻ có thể được sử dụng tự động và những lớp có hàm tạo thứ cấp hoặc nhà máy tĩnh cũng được hỗ trợ]

spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions = true
9

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}

Các phiên bản gần đây của H2 yêu cầu cấu hình đặc biệt để thoát đúng các từ khóa dành riêng như

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
62

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
63

spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions = true

Plugin Spring Boot Gradle tự động sử dụng phiên bản Kotlin được khai báo thông qua plugin Kotlin Gradle

Bây giờ bạn có thể xem xét kỹ hơn ứng dụng đã tạo

Hiểu bản dựng Maven

bổ sung

Ngoài plugin Kotlin Maven rõ ràng, cấu hình mặc định khai báo plugin kotlin-spring tự động mở các lớp và phương thức [không giống như trong Java, vòng loại mặc định là

spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions = true
2 trong Kotlin] được chú thích hoặc siêu chú thích với các chú thích Spring. Điều này rất hữu ích để có thể tạo đậu
spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions = true
3 hoặc
spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions = true
4 mà không cần phải thêm bộ hạn định
spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions = true
5 theo yêu cầu của proxy CGLIB chẳng hạn

Để có thể sử dụng các thuộc tính không thể vô hiệu hóa của Kotlin với JPA, plugin Kotlin JPA cũng được bật. Nó tạo ra các hàm tạo không có đối số cho bất kỳ lớp nào được chú thích bằng

spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions = true
6,
spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions = true
7 hoặc
spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions = true
8

spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions = true
91

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
5

Một trong những tính năng chính của Kotlin là null-safety - xử lý rõ ràng các giá trị

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
50 tại thời điểm biên dịch thay vì chạm vào
dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
51 nổi tiếng trong thời gian chạy. Điều này làm cho các ứng dụng an toàn hơn thông qua các tuyên bố về tính vô hiệu và thể hiện ngữ nghĩa "có giá trị hoặc không có giá trị" mà không phải trả chi phí cho các trình bao bọc như
dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
52. Lưu ý rằng Kotlin cho phép sử dụng các cấu trúc chức năng có giá trị nullable;

Mặc dù Java không cho phép một người thể hiện tính an toàn không có giá trị trong hệ thống kiểu của nó, nhưng Spring Framework cung cấp tính an toàn không có giá trị cho toàn bộ API Spring Framework thông qua các chú thích thân thiện với công cụ được khai báo trong gói

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
53. Theo mặc định, các loại từ API Java được sử dụng trong Kotlin được công nhận là loại nền tảng để kiểm tra null được thoải mái. Hỗ trợ của Kotlin cho các chú thích JSR 305 + Các chú thích về khả năng vô hiệu của Spring cung cấp tính an toàn vô hiệu cho toàn bộ API Spring Framework cho các nhà phát triển Kotlin, với lợi thế xử lý các vấn đề liên quan đến
dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
50 tại thời điểm biên dịch

Tính năng này có thể được kích hoạt bằng cách thêm cờ trình biên dịch

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
55 với các tùy chọn
dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
56

Cũng lưu ý rằng trình biên dịch Kotlin được cấu hình để tạo Java 8 bytecode [Java 6 theo mặc định]

phụ thuộc

3 thư viện cụ thể của Kotlin được yêu cầu cho ứng dụng web Spring Boot đó và được định cấu hình theo mặc định

  • dependencies {
      implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
      implementation["org.springframework.boot:spring-boot-starter-mustache"]
      implementation["org.springframework.boot:spring-boot-starter-web"]
      implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
      implementation["org.jetbrains.kotlin:kotlin-reflect"]
      implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
      runtimeOnly["com.h2database:h2"]
      runtimeOnly["org.springframework.boot:spring-boot-devtools"]
      testImplementation["org.springframework.boot:spring-boot-starter-test"]
    }
    58 là biến thể Java 8 của thư viện chuẩn Kotlin

  • dependencies {
      implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
      implementation["org.springframework.boot:spring-boot-starter-mustache"]
      implementation["org.springframework.boot:spring-boot-starter-web"]
      implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
      implementation["org.jetbrains.kotlin:kotlin-reflect"]
      implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
      runtimeOnly["com.h2database:h2"]
      runtimeOnly["org.springframework.boot:spring-boot-devtools"]
      testImplementation["org.springframework.boot:spring-boot-starter-test"]
    }
    59 là thư viện phản chiếu Kotlin [bắt buộc kể từ Spring Framework 5]

  • dependencies {
      implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
      implementation["org.springframework.boot:spring-boot-starter-mustache"]
      implementation["org.springframework.boot:spring-boot-starter-web"]
      implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
      implementation["org.jetbrains.kotlin:kotlin-reflect"]
      implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
      runtimeOnly["com.h2database:h2"]
      runtimeOnly["org.springframework.boot:spring-boot-devtools"]
      testImplementation["org.springframework.boot:spring-boot-starter-test"]
    }
    60 thêm hỗ trợ tuần tự hóa/giải tuần tự hóa các lớp Kotlin và lớp dữ liệu [các lớp hàm tạo đơn lẻ có thể được sử dụng tự động và những lớp có hàm tạo thứ cấp hoặc nhà máy tĩnh cũng được hỗ trợ]

spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions = true
91

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
6

Hiểu ứng dụng được tạo

spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions = true
33

spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions = true
9

So với Java, bạn có thể nhận thấy thiếu dấu chấm phẩy, thiếu dấu ngoặc trên lớp trống [bạn có thể thêm một số nếu bạn cần khai báo các bean thông qua chú thích

spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions = true
34] và việc sử dụng hàm cấp cao nhất của
spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions = true
35.
spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions = true
36 là thành ngữ Kotlin thay thế cho
spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions = true
37 và có thể được sử dụng để tùy chỉnh ứng dụng với cú pháp sau

spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions = true
33

spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions = true
3

Viết bộ điều khiển Kotlin đầu tiên của bạn

Hãy tạo một bộ điều khiển đơn giản để hiển thị một trang web đơn giản

spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions = true
39

spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions = true
2

Lưu ý rằng chúng tôi đang sử dụng ở đây một tiện ích mở rộng Kotlin cho phép thêm các hàm hoặc toán tử của Kotlin vào các loại Spring hiện có. Ở đây chúng tôi nhập chức năng mở rộng

spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions = true
20 để có thể viết
spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions = true
21 thay vì
spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions = true
22. Spring Framework KDoc API liệt kê tất cả các tiện ích mở rộng của Kotlin được cung cấp để làm phong phú thêm Java API

Chúng ta cũng cần tạo các mẫu Mustache liên quan

spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions = true
23

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
40

spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions = true
24

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
41

spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions = true
25

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
42

Bắt đầu ứng dụng web bằng cách chạy chức năng

spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions = true
26 của
spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions = true
27 và truy cập vào
spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions = true
28, bạn sẽ thấy một trang web tỉnh táo với tiêu đề "Blog"

Thử nghiệm với JUnit 5

JUnit 5 hiện được sử dụng theo mặc định trong Spring Boot cung cấp nhiều tính năng rất tiện dụng với Kotlin, bao gồm tự động kết nối các tham số hàm tạo/phương thức cho phép sử dụng các thuộc tính

spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions = true
29 không thể vô hiệu hóa và khả năng sử dụng
dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
400/
dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
401 trên các phương thức không tĩnh thông thường

Viết bài kiểm tra JUnit 5 trong Kotlin

Vì lợi ích của ví dụ này, hãy tạo một thử nghiệm tích hợp để chứng minh các tính năng khác nhau

  • Chúng tôi sử dụng các câu thực giữa các backticks thay vì camel-case để cung cấp các tên hàm kiểm tra biểu cảm

  • JUnit 5 cho phép thêm các tham số phương thức và hàm tạo, phù hợp với các thuộc tính chỉ đọc và không thể vô hiệu hóa của Kotlin

  • Mã này tận dụng các tiện ích mở rộng của Kotlin

    dependencies {
      implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
      implementation["org.springframework.boot:spring-boot-starter-mustache"]
      implementation["org.springframework.boot:spring-boot-starter-web"]
      implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
      implementation["org.jetbrains.kotlin:kotlin-reflect"]
      implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
      runtimeOnly["com.h2database:h2"]
      runtimeOnly["org.springframework.boot:spring-boot-devtools"]
      testImplementation["org.springframework.boot:spring-boot-starter-test"]
    }
    402 và
    dependencies {
      implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
      implementation["org.springframework.boot:spring-boot-starter-mustache"]
      implementation["org.springframework.boot:spring-boot-starter-web"]
      implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
      implementation["org.jetbrains.kotlin:kotlin-reflect"]
      implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
      runtimeOnly["com.h2database:h2"]
      runtimeOnly["org.springframework.boot:spring-boot-devtools"]
      testImplementation["org.springframework.boot:spring-boot-starter-test"]
    }
    403 [bạn cần nhập chúng]

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
404

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
43

Vòng đời của phiên bản thử nghiệm

Đôi khi bạn cần thực thi một phương thức trước hoặc sau tất cả các bài kiểm tra của một lớp nhất định. Giống như Junit 4, JUnit 5 theo mặc định yêu cầu các phương thức này là tĩnh [có nghĩa là

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
405 trong Kotlin, khá dài dòng và không đơn giản] vì các lớp kiểm tra được khởi tạo một lần cho mỗi lần kiểm tra

Nhưng Junit 5 cho phép bạn thay đổi hành vi mặc định này và khởi tạo các lớp kiểm tra một lần cho mỗi lớp. Điều này có thể được thực hiện theo nhiều cách khác nhau, ở đây chúng tôi sẽ sử dụng tệp thuộc tính để thay đổi hành vi mặc định cho toàn bộ dự án

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
406

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
44

Với cấu hình này, giờ đây chúng ta có thể sử dụng các chú thích

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
400 và
dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
401 trên các phương thức thông thường như được hiển thị trong phiên bản cập nhật của
dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
409 ở trên

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
404

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
45

Tạo tiện ích mở rộng của riêng bạn

Thay vì sử dụng các lớp util với các phương thức trừu tượng như trong Java, Kotlin thường cung cấp các chức năng như vậy thông qua các tiện ích mở rộng của Kotlin. Ở đây chúng ta sẽ thêm một hàm

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
411 vào loại
dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
412 hiện có để tạo văn bản có định dạng ngày tháng tiếng Anh

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
413

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
46

Chúng tôi sẽ tận dụng các tiện ích mở rộng này trong phần tiếp theo

Kiên trì với JPA

Để làm cho quá trình tìm nạp chậm hoạt động như mong đợi, các thực thể phải là

spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions = true
5 như được mô tả trong KT-28525. Chúng tôi sẽ sử dụng plugin Kotlin
dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
415 cho mục đích đó

với lớp

spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions = true
9

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
47

Hoặc với Maven

spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions = true
91

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
48

Sau đó, chúng tôi tạo mô hình của mình bằng cách sử dụng cú pháp ngắn gọn của hàm tạo chính của Kotlin, cho phép khai báo đồng thời các thuộc tính và tham số của hàm tạo

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
418

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
49

Lưu ý rằng chúng tôi đang sử dụng tiện ích mở rộng

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
419 ở đây để cung cấp đối số mặc định cho tham số
dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
420 của hàm tạo
dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
421. Các tham số tùy chọn có giá trị mặc định được xác định ở vị trí cuối cùng để có thể bỏ qua chúng khi sử dụng các đối số vị trí [Kotlin cũng hỗ trợ các đối số được đặt tên]. Lưu ý rằng trong Kotlin, không có gì lạ khi nhóm các khai báo lớp ngắn gọn trong cùng một tệp

Ở đây chúng tôi không sử dụng các lớp

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
422 với các thuộc tính
spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions = true
29 vì JPA không được thiết kế để hoạt động với các lớp bất biến hoặc các phương thức được tạo tự động bởi các lớp
dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
422. Nếu bạn đang sử dụng hương vị Dữ liệu mùa xuân khác, hầu hết chúng được thiết kế để hỗ trợ các cấu trúc như vậy, vì vậy bạn nên sử dụng các lớp như
dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
425 khi sử dụng Dữ liệu mùa xuân MongoDB, Dữ liệu mùa xuân JDBC, v.v.

Mặc dù Spring Data JPA cho phép sử dụng ID tự nhiên [có thể là thuộc tính

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
426 trong lớp
dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
427] qua
dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
428, nhưng nó không phù hợp với Kotlin do KT-6653, đó là lý do tại sao bạn nên luôn sử dụng các thực thể có

Chúng tôi cũng khai báo kho lưu trữ Spring Data JPA của chúng tôi như sau

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
429

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
30

Và chúng tôi viết các bài kiểm tra JPA để kiểm tra xem các trường hợp sử dụng cơ bản có hoạt động như mong đợi hay không

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
430

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
31

Ở đây, chúng tôi sử dụng tiện ích mở rộng

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
431 Kotlin được cung cấp theo mặc định với Dữ liệu mùa xuân, đây là một biến thể có thể vô hiệu hóa của
dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
433 dựa trên
dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
52. Đọc Null tuyệt vời là bạn của bạn, không phải là một bài đăng trên blog sai lầm để biết thêm chi tiết

Triển khai công cụ blog

Chúng tôi cập nhật các mẫu Mustache "blog"

spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions = true
25

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
32

Và chúng tôi tạo một "bài viết" mới

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
435

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
33

Chúng tôi cập nhật

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
436 để hiển thị các trang blog và bài báo với ngày được định dạng. Các tham số hàm tạo
dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
437 và
dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
438 sẽ được tự động nối dây vì
dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
436 có một hàm tạo duy nhất [ẩn
dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
440]

spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions = true
39

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
34

Sau đó, chúng tôi thêm phần khởi tạo dữ liệu vào lớp

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
442 mới

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
443

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
35

Lưu ý việc sử dụng các tham số được đặt tên để làm cho mã dễ đọc hơn

Và chúng tôi cũng cập nhật các bài kiểm tra tích hợp cho phù hợp

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
404

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
36

Bắt đầu [hoặc khởi động lại] ứng dụng web và truy cập

spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions = true
28, bạn sẽ thấy danh sách các bài viết có liên kết có thể nhấp để xem một bài viết cụ thể

Hiển thị API HTTP

Bây giờ chúng tôi sẽ triển khai API HTTP thông qua bộ điều khiển có chú thích

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
446

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
447

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
37

Đối với các bài kiểm tra, thay vì kiểm tra tích hợp, chúng tôi sẽ tận dụng

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
448 và Mockk, tương tự như Mockito nhưng phù hợp hơn với Kotlin

Vì các chú thích

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
449 và
dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
450 dành riêng cho Mockito, nên chúng tôi sẽ tận dụng SpringMockK để cung cấp các chú thích tương tự như
dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
451 và
dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
452 cho Mockk

với lớp

spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions = true
9

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
38

Hoặc với Maven

spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions = true
91

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
39

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
455

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
0

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
456 cần được thoát trong chuỗi vì nó được sử dụng để nội suy chuỗi

Thuộc tính Cấu hình

Trong Kotlin, cách được đề xuất để quản lý các thuộc tính ứng dụng của bạn là tận dụng

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
457 với
dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
458 để có thể sử dụng các thuộc tính chỉ đọc

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
459

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
1

Sau đó, chúng tôi kích hoạt nó ở cấp độ

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
460

spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions = true
33

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
2

Để tạo siêu dữ liệu của riêng bạn để IDE của bạn nhận ra các thuộc tính tùy chỉnh này, kapt phải được định cấu hình với phụ thuộc

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
462 như sau

spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions = true
9

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
3

Lưu ý rằng một số tính năng [chẳng hạn như phát hiện giá trị mặc định hoặc các mục không dùng nữa] không hoạt động do những hạn chế trong mô hình mà kapt cung cấp. Ngoài ra, việc xử lý chú thích chưa được hỗ trợ với Maven do KT-18022, xem phần khởi tạo #438 để biết thêm chi tiết

Trong IntelliJ IDEA

  • Đảm bảo plugin Spring Boot được bật trong menu Tệp. Cài đặt. bổ sung. khởi động mùa xuân

  • Cho phép xử lý chú thích qua menu File. Cài đặt. Xây dựng, Thực thi, Triển khai. Trình biên dịch. Bộ xử lý chú thích. Bật xử lý chú thích

  • Vì Kapt chưa được tích hợp trong IDEA nên bạn cần chạy thủ công lệnh

    dependencies {
      implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
      implementation["org.springframework.boot:spring-boot-starter-mustache"]
      implementation["org.springframework.boot:spring-boot-starter-web"]
      implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
      implementation["org.jetbrains.kotlin:kotlin-reflect"]
      implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
      runtimeOnly["com.h2database:h2"]
      runtimeOnly["org.springframework.boot:spring-boot-devtools"]
      testImplementation["org.springframework.boot:spring-boot-starter-test"]
    }
    464 để tạo siêu dữ liệu

Giờ đây, các thuộc tính tùy chỉnh của bạn sẽ được nhận dạng khi chỉnh sửa

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
465 [tự động hoàn thành, xác thực, v.v. ]

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
63

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
4

Chỉnh sửa mẫu và bộ điều khiển cho phù hợp

spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions = true
25

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
5

spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions = true
39

dependencies {
  implementation["org.springframework.boot:spring-boot-starter-data-jpa"]
  implementation["org.springframework.boot:spring-boot-starter-mustache"]
  implementation["org.springframework.boot:spring-boot-starter-web"]
  implementation["com.fasterxml.jackson.module:jackson-module-kotlin"]
  implementation["org.jetbrains.kotlin:kotlin-reflect"]
  implementation["org.jetbrains.kotlin:kotlin-stdlib-jdk8"]
  runtimeOnly["com.h2database:h2"]
  runtimeOnly["org.springframework.boot:spring-boot-devtools"]
  testImplementation["org.springframework.boot:spring-boot-starter-test"]
}
6

Khởi động lại ứng dụng web, làm mới

spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions = true
28, bạn sẽ thấy biểu ngữ trên trang chủ blog

Sự kết luận

Bây giờ chúng ta đã hoàn thành việc xây dựng ứng dụng blog Kotlin mẫu này. Mã nguồn có sẵn trên Github. Bạn cũng có thể xem tài liệu tham khảo Spring Framework và Spring Boot nếu bạn cần thêm chi tiết về các tính năng cụ thể

Chủ Đề