Scala:需要类类型,但在特征中找到{泛型类型}

时间:2015-04-30 16:50:52

标签: scala

我正在使用Slick 3.0并尝试创建一个提供基本操作的特征。这是我的特点:

object DAO {
  var db: Database = null
}

trait CommonAPI[T <: Table[_]] {
  private val db = DAO.db
  private val objects = TableQuery[T]

  def count: Future[Int] = db.run(objects.length.result)
  def insert(obj: T#TableElementType): Future[Int] = db.run(objects += obj)
  def all: Future[Seq[T]] = db.run(objects.result)
}

DAO.db已在Play onStart方法中初始化。但是,我遇到了行class type required but T found中的编译错误private val objects = TableQuery[T]

我该怎么办?谢谢!

3 个答案:

答案 0 :(得分:2)

这是一个解决方案:

首先,定义它以避免类类型问题..

JobParametersBuilder builder = new JobParametersBuilder();
builder.addString("fileName", fileName);
builder.addLong("time", System.currentTimeMillis());
String jobParam = request.getParameter("job");
jobLauncher.run(myJob, builder.toJobParameters());

然后以这种方式使用它,Post是Table的子类:

class Service[T <: Table[_]](path: String, cons: Tag => T){

  lazy val db = Database.forConfig(path)

  def query = TableQuery[T](cons)
}

此解决方案测试正常。

答案 1 :(得分:0)

您好我做过类似的事情。我用了一点不同的方法。我有一个通用的DAO和很少(每个资源/表)类只是为它继承。

这是通用的DAO:

class BaseDbEntity[T <: BaseEntity, R <: BaseT[T]](val tableName: String, tableQuery: TableQuery[R]) extends DatabaseAccess{

  val createReturningId = tableQuery returning tableQuery.map{item => item.id}

  def create(entity: T): Int = {
    connectionPool withSession {
      implicit session =>
        val resId = createReturningId += entity
        resId
    }
  }

  def getAll = {
    connectionPool withSession {
      implicit session =>
        tableQuery.list
    }
  }
}

全班代码: (https://github.com/vixxx123/scalasprayslickexample/blob/master/src/main/scala/com/vixxx123/scalasprayslickexample/database/BaseDbEntity.scala

和具有相应表类的特定DAO:

class PersonDao extends BaseDbEntity[Person, PersonT]("person", TableQuery[PersonT])

class PersonT(tag: Tag) extends BaseT[Person](tag, "person") {
  def name: Column[String] = column[String]("name")
  def lastname: Column[String] = column[String]("lastname")

  override def * = (id.?, name, lastname) <> (
    (Person.apply _).tupled, Person.unapply)
}

您可以在此处找到该课程:https://github.com/vixxx123/scalasprayslickexample/blob/master/src/main/scala/com/vixxx123/scalasprayslickexample/exampleapi/person/PersonDao.scala

也许它会帮助你。

答案 2 :(得分:0)

如果您的表类定义为:

,则可以传递标记并从中创建表查询
case class Sample(....)

class SampleTable(tag: Tag)
  extends Table[Sample](tag, "sample_table") {
    .....
}

然后您可以按如下方式实现您的通用特征:

import scala.slick.driver.MySQLDriver.simple.Tag // here mysql is used , you can import the driver specific to your db

object DAO {
  var db: Database = null
}

trait CommonAPI[T, A<: Table[T]] {
  private val db = DAO.db
  private val tableTag : Tag => A = _
  def setTag(tag : Tag => A) = { tableTag = tag }
  private val objects = TableQuery(tableTag)

  def count: Future[Int] = db.run(objects.length.result)
  def insert(obj: T#TableElementType): Future[Int] = db.run(objects += obj)
  def all: Future[Seq[T]] = db.run(objects.result)
}