本文介绍了Scala + Slick如何将json映射到数据类型为blob的表列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要使用slick将json值映射到表,并将整个json作为blob存储在同一表中.
I need to map json values to the table using slick and store whole json as a blob in the same table as well.
def createJob(jobs: JobEntity): Future[Option[Long]] =
db.run(job returning job.map((_.id)) += jobs)
我的Json包含类似
id(long),name(string), type(string)
我尝试映射的表有列
id(long),name(string), type(string),json_data(blob)
我的JobEntity类为
I have JobEntity class as
case class JobEntity(id: Option[Long] = None, name: String, type: String) {
require(!jobname.isEmpty, "jobname.empty")
}
如何将json映射到json_data列?
How do I map the json to the json_data column?
推荐答案
Slick支持以下LOB类型:java.sql.Blob, java.sql.Clob, Array[Byte]
(请参见文档)
Slick supports the following LOB types: java.sql.Blob, java.sql.Clob, Array[Byte]
(see the documentation)
因此,您想使用这些类型之一.您的表定义可能如下所示:
Therefore you want to use one of these types. Your table definition could look like this:
case class Job(id: Option[Long] = None, name: String, `type`: String, json_data: java.sql.Blob)
class Jobs(tag: Tag) extends Table[Job](tag, "jobs") {
def id = column[Option[Long]]("id")
def name = column[String]("name")
def `type` = column[String]("type")
def json_data = column[java.sql.Blob]("json_data")
def * = (id, name, `type`, json_data) <> (Job.tupled, Job.unapply)
}
这篇关于Scala + Slick如何将json映射到数据类型为blob的表列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!