首页 > 解决方案 > 链接spring boot和postgreSQL(kotlin)时出错

问题描述

我想使用 spring boot 将一个表从 intellij 传递到 postgreSQL。使用的语言是 Kotlin,它是用 gradle 编写的。

信息到达邮递员,但不是 postgreSQL。另外,没有发生错误,所以我不再知道原因。

下面是我的代码和依赖项。

应用程序属性

spring.datasource.url=jdbc:postgresql://localhost:5432/spring
spring.datasource.username=postgres
spring.datasource.password=1234

用户信息.kt

import javax.persistence.*
@Entity
data class UserInfo(
    @Id val id: String,
    var pw: String,
    var name: String,
    var birth: String
)

用户控制器.kt

import org.springframework.boot.autoconfigure.EnableAutoConfiguration
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
import org.springframework.web.bind.annotation.*
import javax.annotation.PostConstruct

@RestController
@EnableAutoConfiguration(exclude = [DataSourceAutoConfiguration::class])
class UserController {

    private val userMap: MutableMap<String, UserInfo> = mutableMapOf()

    @PostConstruct
    fun init() {
   

        userMap["id1"] = UserInfo("id1", "password1", "Alice", "1001")
        userMap["id2"] = UserInfo("id2", "password2", "Rabit", "1101")
        userMap["id3"] = UserInfo("id3", "password3", "Card", "1201")
    }


    @GetMapping(path = ["/user/{id}"])
    fun getUserInfo(@PathVariable("id") id: String) = userMap[id]

    @GetMapping(path = ["user/all"])
    fun getUserInfoAll() = ArrayList<UserInfo>(userMap.values)


    @PutMapping(path = ["/user/{id}"])
    fun putUserInfo(@PathVariable("id") id: String, @RequestParam("pw") pw: String,
                    @RequestParam("name") name: String, @RequestParam("birth") birth: String) {

        val userInfo = UserInfo(id,pw, name, birth)
        userMap[id] = userInfo
    }


    @PostMapping(path = ["/user/{id}"])
    fun postUserInfo(@PathVariable("id") id: String, @RequestParam("pw") pw: String,
                     @RequestParam("name") name: String, @RequestParam("birth") birth: String){
        val userInfo = userMap[id]
        userInfo?.pw = pw
        userInfo?.name = name
        userInfo?.birth = birth
    }

    @DeleteMapping(path = ["/user/{id}"])
    fun deleteUserInfo(@PathVariable("id") id: String) = userMap.remove(id)

}

用户存储库.kt

import org.springframework.data.repository.reactive.ReactiveCrudRepository
import org.springframework.stereotype.Repository


@Repository
interface UserRepository : ReactiveCrudRepository<UserInfo, String

gradle 在附件中

我认为UserRepository.kt中可能有问题,但我找不到它,因为没有错误。

感谢您阅读这篇文章。祝你今天愉快。

在此处输入图像描述

标签: postgresqlspring-bootkotlin

解决方案


看来你实际上并没有调用save()你的方法UserRepository

尝试先注入它:

@Autowired
private lateinit var userRepository: UserRepository

而里面putUserInfo()

    val userInfo = UserInfo(id,pw, name, birth)
    userRepository.save(userInfo)

请注意,您将收到 Mono 的结果save()


推荐阅读