sql - Kotlin 暴露了 DSL 查询映射
问题描述
- 如何将选择查询映射到类
是否有任何@transactional 注释来执行查询?
object UserRepository { fun getAll() : List<User> { return User.selectAll().map { User } // how to add it in a transaction ? // Is it the right way to map query to a Class ? } fun get(id: Int) : User? { return User.select { User.id eq id id}.map { User.it } // Mapping Not working }
解决方案
我知道已经晚了,但对于其他有这个问题的人来说:如果您使用的是 DSL,您可以在数据类中创建一个函数以用于映射目的。例如 :
data class User(
val id: Int,
val username: String,
val password: String
) {
companion object {
fun fromRow(resultRow: ResultRow) = User(
id = resultRow[UserTable.id].value,
username= resultRow[UserTable.username],
password = resultRow[UserTable.password]
)
}
}
在您的交易块中:
transaction {
user = UserTable.select { UserTable.id eq userId }.map { User.fromRow(it) }
}
推荐阅读
- google-sheets - Google 表格:如何计算累积的分层定价
- html - 如何确保内部 div 仅使用 CSS/HTML 不会超出其容器 div flexbox?
- typescript - 通用返回 T 的打字稿
- python - 如何使用 Tesseract OCR 从视频帧中提取数字?
- postgresql-9.5 - 错误:“SELECT”第 14 行或附近的语法错误:v_result := SELECT * FROM iis_e_ack_pkg.is_e_ack_confirm
- typescript - 在 create-react-app 中添加自定义反应脚本的标准方法
- python - 使用 Python 和 REST api 从 Sharepoint 中提取
- linux - 如何找到应该发送 AT 命令的端口(/dev/smdX)?
- javascript - JS:如何过滤一个对象数组,向第一个对象添加一个带有其余过滤元素值的键?
- google-cloud-networking - 使用 VPC 对等访问 Memorystore (Redis)