Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.5k views
in Technique[技术] by (71.8m points)

Spring boot and postgreSQL integration with Kotlin

language Kotlin.

Currently, it is being integrated with Spring Boot and postgreSQL. However, it works well until I check it with postman, but I cannot connect. Can you please tell me where is wrong?

UserProfile.kt

data class UserProfile(
val id: String,
var pw: String,
var name: String,
var birth: String
)

UserController.kt

@RestController
class UserController {

    private var userMap: MutableMap<String, UserProfile> = mutableMapOf()

    @PostConstruct
    fun init() {
        userMap["id1"] = UserProfile("id1", "password1", "name1", "birth1")
        userMap["id2"] = UserProfile("id2", "password2", "name2", "birth2")
        userMap["id3"] = UserProfile("id3", "password3", "name2", "birth3")
    }

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

    @GetMapping(path = ["user/all"])
    fun getUserInfoAll() = ArrayList<UserProfile>(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 = UserProfile(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)
}

UserProfileMapper.kt

@Mapper
interface UserProfileMapper {

    @get:Select("SELECT * FROM UserProfile")
    val userProfileList:List<UserProfile>

    @Select("SELECT * FROM UserProfile WHERE id=#{id}")
    fun getUserProfile(@Param("id") id:String):UserProfile

    @Insert("INSERT INTO UserProfile VALUES(#{id}, #{name}, #{phone}, #{address})")
    fun insertUserProfile(@Param("id") id:String, @Param("name") name:String, @Param("phone") phone:String, @Param("address") address:String):Int

    @Update("UPDATE UserProfile SET name=#{name}, phone=#{phone}, address=#{address} WHERE id=#{id}")
    fun updateUserProfile(@Param("id") id:String, @Param("name") name:String, @Param("phone") phone:String, @Param("address") address:String):Int

    @Delete("DELETE FROM UserProfile WHERE id=#{id}")
    fun deleteUserProfile(@Param("id") id:String):Int
}

All of my files. It is determined that there is no problem in pom.xml and is not uploaded.

@Select("SELECT * FROM UserProfile WHERE id=#{id}")

I want to put the id of the following sentence fun in the id here, but unlike Java, the conversion does not work well. Please help.

Thank you for reading the long article. Have a good day.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...