postgresql - 链接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中可能有问题,但我找不到它,因为没有错误。
感谢您阅读这篇文章。祝你今天愉快。
解决方案
看来你实际上并没有调用save()
你的方法UserRepository
尝试先注入它:
@Autowired
private lateinit var userRepository: UserRepository
而里面putUserInfo()
val userInfo = UserInfo(id,pw, name, birth)
userRepository.save(userInfo)
请注意,您将收到 Mono 的结果save()
推荐阅读
- r - 如何根据现有数据在 R 中创建百分比表?
- amazon-web-services - Devops 和 AWS Lambda 函数工具
- video - 在两个单独的接收器上同步视频流
- html - 同一div中的响应式多个三角形CSS
- html - 如何更新/发布/部署网页中不断变化的“常量”部分?
- flutter - ListView 和 BottomNavigationBar 之间的空白
- jquery - 语言切换 - 使用 HTML 而不是 Key
- python - 如何在 Twitter API 上访问特定时间范围内的推文?
- java - 为什么我的数据库连接在尝试执行 JOOQ 生成的 SQL 时关闭?
- omnet++ - 如何在静脉中创建自定义消息