【发布时间】: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中可能有问题,但我找不到它,因为没有错误。
感谢您阅读本文。祝你有美好的一天。
【问题讨论】:
-
为什么要禁用 DataSource 自动配置?即
@EnableAutoConfiguration(exclude = [DataSourceAutoConfiguration::class])。乍一看,这看起来像你的问题。此外,您没有在控制器中的任何地方使用UserRepository。创建UserInfo对象不会将实体保存到您的数据库中。 -
还是在学习阶段,看几个例子,估计是练习的不是很清楚。那么我应该如何清除@EnableAutoConfiguration并修改UserRepository呢?
标签: postgresql spring-boot kotlin