gpt4 book ai didi

java - 是否可以在 Spring Repository @Query 注释中动态设置模式?

转载 作者:行者123 更新时间:2023-12-02 02:44:53 25 4
gpt4 key购买 nike

我有一个现有的数据库,它将团队分成不同的模式,其中具有相同的表结构(我知道这很丑)。我需要接受团队名称作为获取请求中的参数,并动态查询该数据的适当架构。

我尝试使用查询参数设置架构以防止 SQL 注入(inject),但我了解到您不允许使用这些参数设置架构/表。

我创建了这个示例来说明如何设置我的代码:

Entity:

@Entity
@Immutable
data class UserEntity (
@Id
@Column(name = "userId")
var userId: String,

@Column(name = "phone")
var phone: String
)

-------------------

Controller:

@GetMapping("/getPhoneFromCorrectSchema"
fun getPhoneFromCorrectSchema(
@RequestParam(value = "userId") userId: String,
@RequestParam(value = "teamName") teamName: String
) : String {
return userRepo.getPhoneFromCorrectSchema(use( rId, teamName)
}

---------------------
Repository:

iterface UserRepo : CrudRepository<UserEntity, String> {
@Query(nativeQuery = true,
value = "SELECT * FROM :teamName.USER_TABLE WHERE userId = :userId"
)
fun getPhoneFromCorrectSchema(@Param("userId") userId: String, @Param("teamName") teamName: String) : UserEntity
}

最佳答案

它看起来很难看,我没有任何明确的解决方案,但我可以告诉你,如果你使用:teamName.USER_TABLE,它不会尝试解析“teamName”,它会尝试解析“teamName.USER_TABLE”。您可以尝试将 teamName 加上 tableName 作为单个参数传递,而不是仅传递 teamName 并附加“.TableName”。它可能会起作用。

关于java - 是否可以在 Spring Repository @Query 注释中动态设置模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57152416/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com