整合springboot+kotlin+gradle+jpa的demo学习笔记

Posted *^O^*—*^O^*

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了整合springboot+kotlin+gradle+jpa的demo学习笔记相关的知识,希望对你有一定的参考价值。

jpa其实就是Java Persistence API的缩写。jpa的中文名字叫做Java持久层API,jpa是JDK 5.0注解或者是XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中。

JPA的宗旨是为POJO提供持久化标准规范,JPA的总体思想和现有的JDO、TopLink、Hibernate等ORM框架总体上是一致的。

JPA具体包括了ORM映射元数据、API、查询语言三个方面的技术。

JPA 也可以自建数据库表,根据实体类

build.gradle.kts

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins 
    id("org.springframework.boot") version "2.3.7.RELEASE"
    id("io.spring.dependency-management") version "1.0.10.RELEASE"
    kotlin("jvm") version "1.3.72"
    kotlin("plugin.spring") version "1.3.72"


group = "com"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_1_8


configurations 
    compileOnly 
        extendsFrom(configurations.annotationProcessor.get())
    


repositories 
    mavenLocal()   //配置先从本地仓库寻找jar包,优先寻找上一个配置,找到不执行下面的配置
    mavenCentral() //配置从中央仓库寻找
    google()       //第三方仓库


dependencies 
    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")
    compileOnly("org.projectlombok:lombok")
    developmentOnly("org.springframework.boot:spring-boot-devtools")
    annotationProcessor("org.projectlombok:lombok")
    testImplementation("org.springframework.boot:spring-boot-starter-test") 
        exclude(group = "org.junit.vintage", module = "junit-vintage-engine")
    
    // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa
    implementation("org.springframework.boot:spring-boot-starter-data-jpa:2.7.1")
// https://mvnrepository.com/artifact/mysql/mysql-connector-java
    implementation("mysql:mysql-connector-java:5.1.38")
    // https://mvnrepository.com/artifact/cn.hutool/hutool-core
    implementation("cn.hutool:hutool-core:5.8.4")



tasks.withType<Test> 
    useJUnitPlatform()


tasks.withType<KotlinCompile> 
    kotlinOptions 
        freeCompilerArgs = listOf("-Xjsr305=strict")
        jvmTarget = "1.8"
    


application.yml

server:
  port: 8080
spring:
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true
    open-in-view: true

  application:
    name: spring_jpa
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/jpa?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8
    username: root
    password: 123456

这里的ddl-auto: update这里使用的update,就是在每次启动时,进行更新,首次会进行创建

实体类

package com.spring_jpa.entity

import lombok.Data
import org.hibernate.annotations.GenericGenerator
import org.springframework.format.annotation.DateTimeFormat
import java.io.Serializable
import java.util.*
import javax.persistence.*

@Entity
@Data
@Table(name = "user")
class User : Serializable 

    @Id
    @Column(length = 20)
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "gg") //自定义生成主键
    @GenericGenerator(name = "gg", strategy = "com.spring_jpa.util.PrimaryGenerator")
    val id : String = String()

    @Column(nullable = false, unique = true, length = 20)
    var userName : String? = null

    @Column(nullable = false, length = 20)
    var password : String? = null

    @Column(nullable = false)
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    var regdate : Date? = null


这里主键Id使用的是雪花算法生成的,可以在hibernate整合spring的demo里面找到

controller

package com.spring_jpa.controller

import com.spring_jpa.entity.User
import com.spring_jpa.service.UserService
import com.spring_jpa.util.R
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.*
import java.util.*

@RestController
@RequestMapping("/user")
class UserController 

    @Autowired
    lateinit var userService: UserService

    @GetMapping
    fun getOne(id:String) : R<*>
        val user = userService.findById(id)
        return R.ok(user,"获取一条数据成功")
    

    @PostMapping
    fun insertOne(@RequestBody user: User) : R<*>
        user.regdate = Date()
        val u =  userService.save(user)
        return R.ok(u,"插入成功")
    

    @DeleteMapping
    fun deleteOne(id: String) : R<*>
        val line = userService.deleteById(id)
        return R.ok(line,"删除成功")
    

    @PutMapping
    fun updateOne(@RequestBody user: User):R<*>
        user.regdate = Date()
        val u = userService.save(user)
        return R.ok(u,"更新成功")
    

    @GetMapping("/all")
    fun getAll():R<*>
        return R.ok(userService.findAll(),"所有数据")
    

service

package com.spring_jpa.service

import com.spring_jpa.entity.User
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.stereotype.Service


@Service
interface UserService : JpaRepository<User,String> 

这里因为==JpaRepository<User,String>==比较强大,很多基本的CRUD操作都有,所以这里我直接使用的就是这个类里面的接口,直接进行简单的CRUD

以上是关于整合springboot+kotlin+gradle+jpa的demo学习笔记的主要内容,如果未能解决你的问题,请参考以下文章

Gradle kotlin Springboot多模块导致无法引用kotlin的类文件(BootJar)

Gradle kotlin Springboot多模块导致无法引用kotlin的类文件(BootJar)

基于springboot+kotlin+gradle构建的框架的坑

Spring Boot + Kotlin + Gradle - 错误:在类中找不到主要方法

springboot+kotlin+gradle+hibernate学习笔记

springboot+kotlin+gradle+hibernate学习笔记