{"id":13481549,"url":"https://github.com/scalikejdbc/scalikejdbc","last_synced_at":"2025-05-13T23:10:56.768Z","repository":{"id":536231,"uuid":"2801094","full_name":"scalikejdbc/scalikejdbc","owner":"scalikejdbc","description":"A tidy SQL-based DB access library for Scala developers. This library naturally wraps JDBC APIs and provides you easy-to-use APIs.","archived":false,"fork":false,"pushed_at":"2025-05-09T08:07:57.000Z","size":7192,"stargazers_count":1269,"open_issues_count":42,"forks_count":230,"subscribers_count":57,"default_branch":"master","last_synced_at":"2025-05-09T09:23:54.755Z","etag":null,"topics":["database","h2","jdbc","mysql","postgresql","scala"],"latest_commit_sha":null,"homepage":"https://scalikejdbc.org/","language":"Scala","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/scalikejdbc.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2011-11-18T06:56:36.000Z","updated_at":"2025-05-09T08:08:00.000Z","dependencies_parsed_at":"2024-01-24T07:29:17.049Z","dependency_job_id":"12932432-d592-44db-b401-8faa1b79a960","html_url":"https://github.com/scalikejdbc/scalikejdbc","commit_stats":{"total_commits":2600,"total_committers":107,"mean_commits":"24.299065420560748","dds":0.5007692307692307,"last_synced_commit":"e24c0c0ba8f89963a174db8d94ce0844d93f8b6e"},"previous_names":[],"tags_count":161,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scalikejdbc%2Fscalikejdbc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scalikejdbc%2Fscalikejdbc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scalikejdbc%2Fscalikejdbc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scalikejdbc%2Fscalikejdbc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/scalikejdbc","download_url":"https://codeload.github.com/scalikejdbc/scalikejdbc/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254042334,"owners_count":22004901,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["database","h2","jdbc","mysql","postgresql","scala"],"created_at":"2024-07-31T17:00:52.737Z","updated_at":"2025-05-13T23:10:51.755Z","avatar_url":"https://github.com/scalikejdbc.png","language":"Scala","funding_links":[],"categories":["Database","Table of Contents","数据库开发","Scala"],"sub_categories":["Database"],"readme":"# ScalikeJDBC\n\n[![Maven Central](https://img.shields.io/maven-central/v/org.scalikejdbc/scalikejdbc_2.13.svg?label=Maven%20Central)](https://search.maven.org/search?q=g:org.scalikejdbc%20AND%20a:scalikejdbc_2.13)\n\n## Just Write SQL And Get Things Done 💪\n\n[**ScalikeJDBC**](https://scalikejdbc.org/) seamlessly wraps JDBC APIs, offering intuitive and highly flexible functionalities. With QueryDSL, your code becomes inherently type-safe and reusable. This library is not just practical; it’s production-ready. Utilize this library confidently in your real-world projects.\n\n## Getting Started\n\n### Simple Database Library\n\nIf you're looking to execute SQL queries efficiently, the best approach is to use ScalikeJDBC along with the appropriate JDBC driver for your database. Here’s how you can get started quickly!\n\n#### Dependencies\n\nTo get started with ScalikeJDBC, add the following dependency to your `build.sbt`:\n\n```scala\nlibraryDependencies ++= Seq(\n  \"org.scalikejdbc\" %% \"scalikejdbc\"     % \"4.3.+\",\n  \"com.h2database\"  %  \"h2\"              % \"2.2.+\",\n  \"ch.qos.logback\"  %  \"logback-classic\" % \"1.5.+\"\n)\n```\n\nIf you're a Play Framework user, take a look at play-support project, too: https://github.com/scalikejdbc/scalikejdbc-play-support\n\n#### Quick Example\n\nHere’s a quick example to get you up and running:\n\n```scala\nimport scalikejdbc._\n\n// initialize JDBC driver \u0026 connection pool\nClass.forName(\"org.h2.Driver\")\nConnectionPool.singleton(\"jdbc:h2:mem:hello\", \"user\", \"pass\")\n\n// ad-hoc session provider on the REPL\nimplicit val session: DBSession = AutoSession\n\n// table creation, you can run DDL by using #execute as same as JDBC\nsql\"\"\"\ncreate table members (\n  id serial not null primary key,\n  name varchar(64),\n  created_at timestamp not null\n)\n\"\"\".execute.apply()\n\n// insert initial data\nSeq(\"Alice\", \"Bob\", \"Chris\") foreach { name =\u003e\n  sql\"insert into members (name, created_at) values (${name}, current_timestamp)\".update.apply()\n}\n\n// for now, retrieves all data as Map value\nval entities: List[Map[String, Any]] = sql\"select * from members\".map(_.toMap).list.apply()\n\n// defines entity object and extractor\nimport java.time._\ncase class Member(id: Long, name: Option[String], createdAt: ZonedDateTime)\nobject Member extends SQLSyntaxSupport[Member] {\n  override val tableName = \"members\"\n  def apply(rs: WrappedResultSet) = new Member(\n    rs.long(\"id\"), rs.stringOpt(\"name\"), rs.zonedDateTime(\"created_at\"))\n}\n\n// find all members\nval members: List[Member] = sql\"select * from members\".map(rs =\u003e Member(rs)).list.apply()\n\n// use paste mode (:paste) on the Scala REPL\nval m = Member.syntax(\"m\")\nval name = \"Alice\"\nval alice: Option[Member] = withSQL {\n  select.from(Member as m).where.eq(m.name, name)\n}.map(rs =\u003e Member(rs)).single.apply()\n```\n\n### Rich O/R Mapper\n\nFor those who require more robust functionalities, consider using **scalikejdbc-orm**. This extension is an O/R mapper built on top of the ScalikeJDBC core library, drawing significant inspiration from Ruby on Rails' ActiveRecord library.\n\n\n#### Efficient Data Fetching with Join Queries / Eager Loading\n\nOne of the standout features of scalikejdbc-orm is its ability to efficiently handle data associations, effectively eliminating the common N+1 query problem. This is achieved through the smart use of join queries in resolving associations like `#belongsTo`, `#hasOne`, and `#hasMany/#hasManyThrough`. These are processed behind the scenes, allowing you to focus on your application without worrying about performance degradation due to N+1 issues.\n\nWhile join queries are suitable for many scenarios, some complex data relationships might require a different approach. For such use cases, you can do eager loading (i.e. resolve the main entity and then perform in-clause query to resolve deep nested associations) with the `#includes` method.\n\n#### Dependencies\n\nLike the instruction for the simple DB library, add the library along with a JDBC driver and logging tool:\n\n```scala\nlibraryDependencies ++= Seq(\n  \"org.scalikejdbc\" %% \"scalikejdbc-orm\" % \"4.3.+\",\n  \"com.h2database\"  %  \"h2\"              % \"2.2.+\",\n  \"ch.qos.logback\"  %  \"logback-classic\" % \"1.5.+\"\n)\n```\n\n#### Quick Example\n\nSave the following code as `example.scala`:\n\n```scala\nimport java.time.ZonedDateTime\n\nimport scalikejdbc.*\nimport scalikejdbc.orm.*\nimport scalikejdbc.orm.timstamps.TimestampsFeature\n\ncase class Email(\n  id: Long,\n  memberId: Long,\n  address: String,\n)\nobject Email extends CRUDMapper[Email] {\n  override lazy val tableName = \"member_email\"\n  lazy val defaultAlias = createAlias(\"me\")\n  def extract(rs: WrappedResultSet, e: ResultName[Email]): Email = autoConstruct(rs, e)\n}\n\ncase class Member(\n  id: Long,\n  name: Option[String],\n  createdAt: ZonedDateTime,\n  updatedAt: Option[ZonedDateTime],\n  email: Option[Email] = None,\n)\nobject Member extends CRUDMapper[Member] with TimestampsFeature[Member] {\n  lazy val defaultAlias = createAlias(\"m\")\n  def extract(rs: WrappedResultSet, n: ResultName[Member]): Member = autoConstruct(rs, n, \"email\")\n\n  val email = hasOne[Email](Email, (m, e) =\u003e m.copy(email = e))\n}\n\nobject Example extends App {\n  // ### Database connection ###\n  Class.forName(\"org.h2.Driver\")\n  ConnectionPool.singleton(\"jdbc:h2:mem:hello;MODE=PostgreSQL\", \"user\", \"pass\")\n  implicit val session: DBSession = AutoSession\n\n  // ### Create tables ###\n  sql\"\"\"create table member (\n    id serial not null primary key,\n    name varchar(64),\n    created_at timestamp not null,\n    updated_at timestamp\n  )\"\"\".execute.apply()\n  sql\"\"\"create table member_email (\n    id serial not null primary key,\n    member_id int not null,\n    address varchar(256) not null\n  )\"\"\".execute.apply()\n\n  val m = Member.column\n\n  // ### Insert rows ###\n  val ids = Seq(\"Alice\", \"Bob\", \"Chris\") map { name =\u003e\n    // insert into member (name, created_at, updated_at) values ('Alice', '2024-05-11 14:52:27.13', '2024-05-11 14:52:27.13');\n    Member.createWithNamedValues(m.name -\u003e name)\n  }\n\n  // ### Find all rows ###\n  // select m.id as i_on_m, m.name as n_on_m, m.created_at as ca_on_m, m.updated_at as ua_on_m from member m order by m.id;\n  val allMembers1: Seq[Member] = Member.findAll()\n  // select m.id as i_on_m, m.name as n_on_m, m.created_at as ca_on_m, m.updated_at as ua_on_m from member m where m.id in (1, 2, 3);\n  val allMembers2: Seq[Member] = Member.findAllByIds(ids*)\n\n  // ### Run queries with where conditions ###\n  // Quick way but less type-safety\n  // select m.id as i_on_m, m.name as n_on_m, m.created_at as ca_on_m, m.updated_at as ua_on_m from member m where m.name = 'Alice' order by m.id;\n  val member1: Seq[Member] = Member.where(\"name\" -\u003e \"Alice\").apply()\n  // Types-safe query builder\n  // select m.id as i_on_m, m.name as n_on_m, m.created_at as ca_on_m, m.updated_at as ua_on_m from member m where name = 'Alice' order by m.id;\n  val member2: Seq[Member] = Member.where(sqls.eq(m.name, \"Alice\")).apply()\n\n  val memberId = member2.head.id\n\n  // ### Run join queries ###\n  val e = Email.column\n  // insert into member_email (member_id, address) values (1, 'a@example.com');\n  Email.createWithNamedValues(e.memberId -\u003e memberId, e.address -\u003e \"a@example.com\")\n\n  // Note that member3.email exists while it does not in member1,2\n  // select m.id as i_on_m, m.name as n_on_m, m.created_at as ca_on_m, m.updated_at as ua_on_m , me.id as i_on_me, me.member_id as mi_on_me, me.address as a_on_me from member m left join member_email me on m.id = me.member_id where name = 'Alice' order by m.id;\n  val member3 = Member.joins(Member.email).where(sqls.eq(m.name, \"Alice\")).apply()\n\n  // ### Update/delete rows ###\n  // update member set updated_at = '2024-05-11 14:52:27.188', name = 'Ace' where id = 1;\n  Member.updateById(memberId).withAttributes(\"name\" -\u003e \"Ace\")\n  // delete from member where id = 1;\n  Member.deleteById(memberId)\n}\n```\n\nRun the code by the `sbt run` command.\n\nHow did it go? If you'd like to know more details or see more practical examples, see the full documentation at:\n\nhttps://scalikejdbc.org/\n\n## License\n\nPublished source code and binary files have the following copyright:\n\n```\nCopyright scalikejdbc.org\nApache License, Version 2.0\nhttps://www.apache.org/licenses/LICENSE-2.0.html\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fscalikejdbc%2Fscalikejdbc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fscalikejdbc%2Fscalikejdbc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fscalikejdbc%2Fscalikejdbc/lists"}