【问题标题】:Error in linking spring boot and postgreSQL(kotlin)链接 spring boot 和 postgreSQL(kotlin) 时出错
【发布时间】:2021-04-27 04:51:57
【问题描述】:

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

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

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

application.properties

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)

}

UserRepository.kt

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


@Repository
interface UserRepository : ReactiveCrudRepository<UserInfo, String

gradle 在附件中

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

感谢您阅读本文。祝你有美好的一天。

enter image description here

【问题讨论】:

  • 为什么要禁用 DataSource 自动配置?即@EnableAutoConfiguration(exclude = [DataSourceAutoConfiguration::class])。乍一看,这看起来像你的问题。此外,您没有在控制器中的任何地方使用UserRepository。创建UserInfo 对象不会将实体保存到您的数据库中。
  • 还是在学习阶段,看几个例子,估计是练习的不是很清楚。那么我应该如何清除@EnableAutoConfiguration并修改UserRepository呢?

标签: postgresql spring-boot kotlin


【解决方案1】:

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

尝试先注入它:

@Autowired
private lateinit var userRepository: UserRepository

putUserInfo()里面

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

注意,save() 会导致您收到 Mono

【讨论】:

  • @Autowired private lateinit var userRepository: UserRepository { val userInfo = UserInfo(id,pw, name,birth) userRepository.save(userInfo) }
  • 我还在学习中,所以很抱歉详细地问你……我可以这样写吗?
  • 只需将userRepository 声明为Controller 的成员,然后将第二个代码块放入您的fun putUserInfo(...)
  • 如前所述,保存类型已变为单声道。有什么解决办法吗?
  • Mono 是高级主题。在最简单的情况下,您可以调用它的 .block() 来同步等待 Mono 解决
猜你喜欢
  • 2018-02-07
  • 2019-09-02
  • 1970-01-01
  • 2019-02-27
  • 2021-01-21
  • 2021-09-01
  • 2020-11-26
  • 2017-11-20
  • 2020-12-21
相关资源
最近更新 更多