当前位置: 首页 > 工具软件 > Scala Slick > 使用案例 >

scala slick mysql_mysql – 如何在Scala Slick中使用枚举?

籍兴文
2023-12-01

我通过以下方式解决了Enums的问题(以您的值为例):

import play.api.db.slick.DB

import play.api.db.slick.Config.driver.simple._

sealed trait MyEnum

case object MyEnumA extends MyEnum

case object MyEnumB extends MyEnum

case object MyEnumC extends MyEnum

object MyEnumMapper {

val string_enum_mapping:Map[String,MyEnum] = Map(

"a" -> MyEnumA,

"b" -> MyEnumB,

"c" -> MyEnumC

)

val enum_string_mapping:Map[MyEnum,String] = string_enum_mapping.map(_.swap)

implicit val myEnumStringMapper = MappedTypeMapper.base[MyEnum,String](

e => enum_string_mapping(e),

s => string_enum_mapping(s)

)

}

import MyEnumMapper._

case class MyData(

......

enumValue: MyEnum,

.....

)

................

object MyDataTable extends Table[MyData]("") {

......

def enumValue = column[MyEnum]("")

.....

.... /* whatever lifted or direct slick calls you want */

}

如果适用于Play 2.1和Play 2.2,Slick 1.0.0和MariaDB 5.5(与MySQL相同)

 类似资料: