1 - what is scalikeJDBC
This library naturally wraps JDBC APIs and provides you easy-to-use and very flexible APIs.see more : http://scalikejdbc.org/
2- how to setup
*first your project have to setup connection with database
if not you can read this : play scala 2.4 connect and generate mysql database by evolutions
* now enable scalikejdbc plug-in
add scalikejdbc in your build dependencies : build.sbt
libraryDependencies ++= Seq(* add this for initializer
"org.scalikejdbc" %% "scalikejdbc" % "2.2.8",
"com.h2database" % "h2" % "1.4.189",
"ch.qos.logback" % "logback-classic" % "1.1.3",
"org.scalikejdbc" %% "scalikejdbc-config" % "2.2.8"
)
libraryDependencies ++= Seq(* add this if use scalikeJDBC for test
"org.scalikejdbc" %% "scalikejdbc-play-initializer" % "2.4.2"
)
libraryDependencies ++= Seq(* Now enable scalikejdbc.PlayModule in conf/application.conf
"org.scalikejdbc" %% "scalikejdbc-test" % "2.2.8" % "test"
)
play.modules.enabled += "scalikejdbc.PlayModule"* In your application you want to run query with scalikejdbc, just add the following import.
import scalikejdbc._* In your action you want to run query with scalikejdbc, just add implicit a session .
def your_action()(implicit session: DBSession = AutoSession) = Action {}3- how to run a query with scalikejdbc
*first you need sql query,of course ,if you did setup with above tutorial you have "users" table :
select * from users* ok,now i will show you,how to run with scalikejdbc
val runSelectQuery : Option[String] = SQL("select * from users limit 1")
.map { rs => rs.string("email") }
.single().apply()
- SQL("select * from users limit 1") : return a query
- map { rs => rs.string("email") } : because this query is select so you need to define output
- single() : format for output ,single() return Option,in this case it will return Option[String]
- apply() : just apply,you need it to finish your query
example,now i want to select user have email is "hoai@pt.com" ,the query above will change like this
val emailAddress = "hoai@pt.com"
val runSelectQuery : Option[String] = SQL("select * from users where email = ${email} limit 1")
.bindByName('email -> emailAddress)* another way to make a query is use : sql"your_query_here"
.map { rs => rs.string("email") }
.single().apply()
i will rewrite above query :
val emailAddress = "hoai@pt.com"
val runSelectQuery : Option[String] = sql"select * from users where email = ${emailAddress} limit 1"
.map { rs => rs.string("email") }so do you see what different by this way,now i don't need use bindByName
.single().apply()
4- some notes
this is some common option for you to create and run a query by scalikejdbc
- import scalikejdbc._ : you must import this for run query with scalikejdbc
- bindByName() : can use with both SQL() and sql"" standard
- map() : use when you select or want to define output
- single() : use for select and have only one row return ,return Option
- list() : use for select and return a List ,of course
- execute() : can use for every kind of query,but just return true or false
- update() : can NOT user for select query
- updateAndReturnGeneratedKey() : use for insert and return primary key if query run success
good luck and leave your comment
Comments
Post a Comment