ScalikeJDBC with play scala 2.4 tutorial

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/
play scala scalikejdbc tutorial

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(
  "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"
)
* add this for initializer
libraryDependencies ++= Seq(
  "org.scalikejdbc" %% "scalikejdbc-play-initializer" % "2.4.2"
)
* add this if use scalikeJDBC for test
libraryDependencies ++= Seq(
  "org.scalikejdbc" %% "scalikejdbc-test"   % "2.2.8"   % "test"
)
* Now enable scalikejdbc.PlayModule in conf/application.conf
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()
  1.  SQL("select * from users limit 1") : return a query 
  2. map { rs => rs.string("email") } : because this query is select so you need to define output
  3. single() : format for output ,single() return Option,in this case it will return Option[String]
  4. apply() : just apply,you need it to finish your query
* if you have conditions for your query : bindByName('name -> value)
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)
  .map { rs => rs.string("email") }
  .single().apply()
* another way to make a query is use : sql"your_query_here"
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") }
  .single().apply()
so do you see what different by this way,now i don't need use bindByName 

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
just all for today,next turtor i will introduce you one more way to create and run a query by scalikejdbc : SQLSyntaxSupport
good luck and leave your comment

Comments