ScalikeJDBC is a SQL-based DB access library for Scala developers.
This library naturally wraps JDBC APIs and provides you easy-to-use APIs.
Users do nothing other than writing SQL and mapping from java.sql.ResultSet
objects to Scala values.
If you want to create mapper modules easily, also take a look at scalikejdbc-mapper-generator.
https://github.com/seratch/scalikejdbc/tree/master/scalikejdbc-mapper-generator
We never release without passing all the unit tests with the following RDBMS.
- PostgreSQL
- MySQL
- H2 Database Engine
- HSQLDB
- SQLite
Here is the scaladoc:
http://seratch.github.com/scalikejdbc/api/index.html#scalikejdbc.package
libraryDependencies += "com.github.seratch" %% "scalikejdbc" % "[1.4,)"
// slf4j binding you like
libraryDependencies += "org.slf4j" % "slf4j-simple" % "[1.7,)"
<dependency>
<groupId>com.github.seratch</groupId>
<artifactId>scalikejdbc_2.9.2</artifactId>
<version>[1.4,)</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>[1.7,)</version>
</dependency>
Try ScalikeJDBC right now!
git clone git://github.com/seratch/scalikejdbc.git
cd scalikejdbc/sandbox
sbt console
"users" table is already created. You can execute queries as follows:
case class User(id: Long, name: String)
val users = DB readOnly { implicit s =>
SQL("select * from users")
.map(rs => User(rs.long("id"), rs.string("name")))
.list.apply()
}
DB localTx { implicit s =>
SQL("insert into users (id, name) values ({id}, {name})")
.bindByName('id -> 3, 'name -> "Charles")
.update.apply()
}
If you need more information(connection management, transaction, CRUD), please check the following wiki page or scaladoc in detail.
https://github.com/seratch/scalikejdbc/wiki/GettingStarted
http://seratch.github.com/scalikejdbc/api/index.html#scalikejdbc.package
The most basic way is just using prepared statement as follows.
SQL("""insert into users values (?, ?, ?, ?)""")
.bind(132430, "[email protected]", "Bob", "xfewSZe2sd3w")
.update.apply()
Instead of embedding ?
(place holder), you can specify named place holder that is similar to Anorm.
SQL("""insert into users values ({id}, {email}, {name}, {encryptedPassword})""")
.bindByName(
'id -> 132430,
'emal -> "[email protected]",
'name -> "Bob",
'encryptedPassword -> "xfewSZe2sd3w")
.update.apply()
Instead of embedding ?
(place holder), you can specify executable SQL as template. Using this API, it's possible to validate SQL before building into application.
Usage is simple. Use Scala Symbol literal in comment with dummy value in SQL template, and pass named values by using not bind(Any*)
but bindByName((Symbol, Any)*)
. When some of the passed names by #bindByName
are not used, or #bind
is used although the template seems to be executable SQL template, runtime exception will be thrown.
SQL("""
insert into users values (
/*'id*/123,
/*'email*/'[email protected]',
/*'name*/'Alice',
/*'encryptedPassword*/'123456789012')
""")
.bindByName(
'id -> 132430,
'emal -> "[email protected]",
'name -> "Bob",
'encryptedPassword -> "xfewSZe2sd3w")
.update.apply()
This feature is still experimental, but you can try it now.
https://github.com/seratch/scalikejdbc/tree/master/scalikejdbc-interpolation
def create(id: Long, email: String, name: String, encryptedPassword: Sting) {
sql"insert into users values (${id}, ${email}, ${name}, ${encryptedPassword})"
.update.apply()
}
Using LogginSQLAndTime feature, you can check the actual SQL(not exactly) and time.
https://github.com/seratch/scalikejdbc/wiki/LoggingSQLAndTime
If you want to create mapper modules easily, also take a look at this sbt plugin.
sbt "scalikejdbc-gen [table-name (class-name)]"
https://github.com/seratch/scalikejdbc/tree/master/scalikejdbc-mapper-generator
You can use ScalikeJDBC with Play framework 2.x seamlessly.
https://github.com/seratch/scalikejdbc/tree/master/scalikejdbc-play-plugin
We promise you that it becomes more useful when using together with scalikejdbc-mapper-generator.
Apache License, Version 2.0