2016-01-27 46 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 
} 
相關問題