2016-01-27 27 views
2

我似乎在web上的例子很多有光滑3配置如下油滑3我怎样才能在应用程序文件的默认配置

slick.dbs.default.driver="slick_driver" 
slick.dbs.default.db.driver="db_driver" 
slick.dbs.default.db.url="URL_file 
slick.dbs.default.db.user="user" 
slick.dbs.default.db.password="password" 

我的问题是你怎么能连接到Scala的内控制器?我正在使用播放框架版本2.4.6。我已经试过这

def db: Database = Database.forDataSource(DB.getDataSource()) 

,但我得到一个错误

Error:(5, 17) Play 2 Compiler: 
C:\Users\nemer\mycontroller\app\controllers\Application.scala:5: 
    object db is not a member of package play.api 
import play.api.db.DB 
       ^

我的控制器看起来像这样

package controllers 


import org.mindrot.jbcrypt.BCrypt 
import play.api.db.DB 

import play.api.libs.json.{Json, JsValue} 
import play.api.mvc._ 
import slick.driver.PostgresDriver.api._ 
import scala.concurrent.duration._ 
import scala.concurrent.Await 
import play.api.libs.concurrent.Execution.Implicits._ 


class Application extends Controller { 


// I am trying to get the Slick config settings in here 
def db: Database = Database.forDataSource(DB.getDataSource()) 

    def mypage = Action { 



    val json: JsValue = Json.parse(""" 
{ 
    "name" : "Watership Down", 
    "location" : { 
    "lat" : 51.235685, 
    "long" : -1.309197 
    }, 
    "residents" : [ { 
    "name" : "Fiver", 
    "age" : 4, 
    "role" : null 
    }, { 
    "name" : "Bigwig", 
    "age" : 6, 
    "role" : "Owsla" 

    } ] 
} 
            """) 
    Ok(json).as("text/json") 
    } 

} 

回答

0
在conf

,我定义数据库如下:

db.test.driver="com.mysql.jdbc.Driver" 
db.test.url="jdbc:mysql://localhost/test" 
db.test.user="test" 
db.test.password="test" 

在.scala文件中,我用t他的数据库是这样的:

import com.typesafe.config.ConfigFactory 

val config = ConfigFactory.load() 
val db = Database.forConfig("db.test", config) 

然后从数据库中选择如下:

db.withSession { implicit session => 
    myTable.filter(_.id === "123").firstOption 
} 
相关问题