Giter Club home page Giter Club logo

scalikejdbc's Introduction

ScalikeJDBC

Just write SQL and get things done!

ScalikeJDBC is a tidy SQL-based DB access library for Scala that naturally wraps JDBC and provides easy-to-use APIs.

ScalikeJDBC is practical and production-ready. Use this library for your real projects.

http://scalikejdbc.org/

Build Status Maven Central

Gitter Chat for Casual Q&A

Getting Started

Just add ScalikeJDBC, a JDBC driver, and an slf4j implementation to your sbt build settings:

libraryDependencies ++= Seq(
  "org.scalikejdbc" %% "scalikejdbc"        % "2.4.+",
  "com.h2database"  %  "h2"                 % "1.4.+",
  "ch.qos.logback"  %  "logback-classic"    % "1.1.+"
)

If you're a Play2 user, take a look at play-support project, too:

https://github.com/scalikejdbc/scalikejdbc-play-support

First example

After adding the above dependencies to your build.sbt, run sbt console and execute the following code:

import scalikejdbc._

// initialize JDBC driver & connection pool
Class.forName("org.h2.Driver")
ConnectionPool.singleton("jdbc:h2:mem:hello", "user", "pass")

// ad-hoc session provider on the REPL
implicit val session = AutoSession

// table creation, you can run DDL statements using #execute as with JDBC
sql"""
create table members (
  id serial not null primary key,
  name varchar(64),
  created_at timestamp not null
)
""".execute.apply()

// insert initial data
Seq("Alice", "Bob", "Chris") foreach { name =>
  sql"insert into members (name, created_at) values (${name}, current_timestamp)".update.apply()
}

// retrieve all data as a List of Map elements
val entities: List[Map[String, Any]] = sql"select * from members".map(_.toMap).list.apply()

// define an entity object and extractor
import org.joda.time._
case class Member(id: Long, name: Option[String], createdAt: DateTime)
object Member extends SQLSyntaxSupport[Member] {
  override val tableName = "members"
  def apply(rs: WrappedResultSet): Member = new Member(
    rs.long("id"), rs.stringOpt("name"), rs.jodaDateTime("created_at"))
}

// find all Members
val members: List[Member] = sql"select * from members".map(rs => Member(rs)).list.apply()

How did it go? If you'd like to know more details or see more practical examples, see the full documentation at:

http://scalikejdbc.org/

License

Published source code and binary files have the following copyright:

Copyright scalikejdbc.org
Apache License, Version 2.0
http://www.apache.org/licenses/LICENSE-2.0.html

scalikejdbc's People

Contributors

seratch avatar xuwei-k avatar gakuzzzz avatar tototoshi avatar kxbmap avatar tkawachi avatar zaneli avatar ponythewhite avatar btakashi avatar nazoking avatar arakaki avatar wozaki avatar rabitarochan avatar tlync avatar lloydmeta avatar kihn avatar ukitaka avatar todesking avatar pocketberserker avatar ocadaruma avatar namutaka avatar luminescent avatar kobtea avatar diceit avatar thobson avatar dinoboy197 avatar thagikura avatar takayukiatkwsk avatar ssowerby avatar emalock3 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.