zoukankan      html  css  js  c++  java
  • [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

  • 相关阅读:
    python之数据规范化(Min-Max规范化)
    python对全班成绩进行数据清洗(pandas的使用)
    python统计全班的成绩(numpy的使用)
    python爬虫之动态渲染页面抓取-(Selenium)的使用
    python之小米应用商店搜索
    python之小米应用商店爬虫
    cmds系统数据库源端大表数据更新优化
    临时表空间扩容
    性能优化概要(2)数据库时间,监控和优化工具
    cmds挖掘redolog
  • 原文地址:https://www.cnblogs.com/Answer1215/p/13934889.html
Copyright © 2011-2022 走看看