[Kotlin spring boot] Work with database
pom.xml: add compile plugin
<build> <sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory> <testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-maven-plugin</artifactId> <configuration> <args> <arg>-Xjsr305=strict</arg> </args> <compilerPlugins> <plugin>spring</plugin> <plugin>jpa</plugin> </compilerPlugins> </configuration> <dependencies> <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-maven-allopen</artifactId> <version>${kotlin.version}</version> </dependency> <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-maven-noarg</artifactId> <version>${kotlin.version}</version> </dependency> </dependencies> </plugin> </plugins> </build>
update application.properties:
spring.datasource.url=jdbc:h2:file:~/theater
spring.datasource.driverClassName = org.h2.Driver
spring.datasource.username = sa
spring.datasource.password =
spring.jpa.properties.hibernate.hbm2dll.auto=update
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.properties.hibernate.globally_quoted_identifiers=true
We need to create a jpa interface, so that jpa will help us to generate database table according to our Entity data schema:
data/JPAInterfaces.kt
package com.virtualpairprogrammers.theater.data import com.virtualpairprogrammers.theater.domain.Seat import org.springframework.data.jpa.repository.JpaRepository interface SeatRepository : JpaRepository<Seat, Long>
domain/Seat.kt:
package com.virtualpairprogrammers.theater.domain
import java.math.BigDecimal
import javax.persistence.Entity
import javax.persistence.GeneratedValue
import javax.persistence.GenerationType
import javax.persistence.Id
@Entity
data class Seat(@Id @GeneratedValue(strategy = GenerationType.AUTO)
val id: Long,
val row: Char,
val num: Int,
val price: BigDecimal,
val description: String) {
override fun toString(): String = "Seat $row-$num $$price ($description)"
}
Inside main controller, create a endpoint "bootstrap" when we need to add data into the table
fun createInitialData(): ModelAndView {
val seats = theaterService.seats
seatRepository.saveAll(seats)
return homePage()
}
https://github.com/zhentian-wan/kotlin-spring-boot-demo/commit/a1d8bd0925a6189f0aabb351a57cb96a6e81abc8
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2018-11-06 [Tools] Convert SVG to a PDF in Node with PDFKit and SVG.js
2018-11-06 [Javascript] Cancel A Promise Using AbortController
2018-11-06 [Testing] JavaScript Mocking Fundamentals
2017-11-06 [React Native] Animate the Scale of a React Native Button using Animated.spring
2017-11-06 [React] Use the URL as the source of truth in React
2015-11-06 [Angular 2] Validation
2015-11-06 [Angualr 2] Using FormBuilder