【发布时间】:2019-11-30 19:22:09
【问题描述】:
我有一个现有的数据库,可以将团队分成单独的模式,其中具有相同的表结构(丑陋,我知道)。我需要接受团队名称作为获取请求中的参数,并动态查询该数据的适当架构。
我已尝试使用查询参数设置架构以防止 SQL 注入,但我了解到您不允许使用这些设置架构/表。
我创建了这个代码设置示例:
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
}
【问题讨论】:
标签: java spring spring-boot jpa kotlin