{"id":43180494,"url":"https://github.com/dibog/spring-jdbc-template-demo","last_synced_at":"2026-02-01T03:42:17.503Z","repository":{"id":37262818,"uuid":"202545072","full_name":"dibog/spring-jdbc-template-demo","owner":"dibog","description":"Examples and extension functions for Spring jdbc templates ","archived":false,"fork":false,"pushed_at":"2022-06-20T22:42:27.000Z","size":28,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2023-03-12T07:24:32.452Z","etag":null,"topics":["jdbc","kotlin","spring"],"latest_commit_sha":null,"homepage":null,"language":"Kotlin","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/dibog.png","metadata":{"files":{"readme":"readme.adoc","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-08-15T13:21:53.000Z","updated_at":"2019-08-16T17:36:49.000Z","dependencies_parsed_at":"2022-09-03T18:41:18.718Z","dependency_job_id":null,"html_url":"https://github.com/dibog/spring-jdbc-template-demo","commit_stats":null,"previous_names":[],"tags_count":null,"template":null,"template_full_name":null,"purl":"pkg:github/dibog/spring-jdbc-template-demo","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dibog%2Fspring-jdbc-template-demo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dibog%2Fspring-jdbc-template-demo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dibog%2Fspring-jdbc-template-demo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dibog%2Fspring-jdbc-template-demo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dibog","download_url":"https://codeload.github.com/dibog/spring-jdbc-template-demo/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dibog%2Fspring-jdbc-template-demo/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28966956,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-01T02:14:24.993Z","status":"ssl_error","status_checked_at":"2026-02-01T02:13:55.706Z","response_time":56,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["jdbc","kotlin","spring"],"created_at":"2026-02-01T03:42:16.918Z","updated_at":"2026-02-01T03:42:17.488Z","avatar_url":"https://github.com/dibog.png","language":"Kotlin","funding_links":[],"categories":[],"sub_categories":[],"readme":"= Readme\nDieter Bogdoll, \u003cdibog@github.com\u003e\n{project.version}, 2019/08/15\n:icons: font\n:toc:\n:toc2:\n:toc-title:\n\nimage:https://img.shields.io/github/license/dibog/spring-jdbc-template-demo[link=\"LICENSE\"]\nimage:https://api.travis-ci.org/dibog/spring-jdbc-template-demo.svg?branch=master[link=\"https://travis-ci.org/dibog/spring-jdbc-template-demo\"]\nimage:https://jitpack.io/v/dibog/spring-jdbc-template-demo.svg[link=\"https://jitpack.io/#dibog/spring-jdbc-template-demo\"]\nimage:https://img.shields.io/badge/100%25-kotlin-blue.svg[link=\"https://kotlinlang.org/\"]\n\n[discrete]\n== Introduction\n\nThere are many methods within the `JdbcTemplate` class of spring.\n\nHere are some examples when to use which method.\n\nYou can build this document locally with\n[source,bash]\n----\nmvn clean package -Pdocumentation\n----\n\nYou can find the generated file in `target/generated-docs/readme.html`.\n\n\n== Queries\n\n=== Query for exactly one entity\n[source,kotlin]\n----\ninclude::{docdir}/src/test/java/io/dibog/spring/jdbc/QueryTest.kt[tags=query-single-entity,indent=0]\n----\n\u003c1\u003e `JdbcTemplate::queryForObject` expects to return *one* entity. It will throw an\n`EmptyResultDataAccessException` if the query returns no entity or `IncorrectResultSizeDataAccessException`\nif it throws more than one entity.\n\u003c2\u003e The SQL statement for the prepared statement.\n\u003c3\u003e Parameter array for the prepared statement.\n\u003c4\u003e The `RowMapper\u003cT\u003e` to be used to transform the result set into an entity.\n\n=== Query for several entities\n[source,kotlin]\n----\ninclude::{docdir}/src/test/java/io/dibog/spring/jdbc/QueryTest.kt[tags=query-multiple-entity,indent=0]\n----\n\u003c1\u003e `JdbcTemplate::query` expects to return zero to many entities.\n\u003c2\u003e The SQL statement for the prepared statement.\n\u003c3\u003e The `RowMapper\u003cT\u003e` to be used to transform the result set into an entity.\n\nThere is also another version of this method which takes an parameter array to\nparameterize the prepared statement.\n\n=== Query for one attribute of entities\n[source,kotlin]\n----\ninclude::{docdir}/src/test/java/io/dibog/spring/jdbc/QueryTest.kt[tags=query-single-attribute,indent=0]\n----\n\u003c1\u003e `JdbcTemplate::queryForList` expects to return zero to more rows with one attribute.\n\u003c2\u003e The SQL statement for the prepared statement selecting just one attribute.\n\u003c3\u003e The kotlin type of the attribute to be part of the result list.\n\n=== Query for several attributes of entities\n[source,kotlin]\n----\ninclude::{docdir}/src/test/java/io/dibog/spring/jdbc/QueryTest.kt[tags=query-multiple-attribute,indent=0]\n----\n\u003c1\u003e `JdbcTemplate::queryForList` expects to return zero to more rows with attributes.\n\u003c2\u003e The SQL statement for the prepared statement selecting just multiple attributes.\n\n== Inserts with generated keys\n\nIn the following examples will be showed which inserts new rows into a table\nwhere the `ID` attribute is automatically generated and returned. Returning\nof generated keys is not supported for all JDBC drivers, so use it with care.\n\nIn the examples the image:http://hsqldb.org/images/hypersql_logo.png[HSQLDB,100,30,link=http://hsqldb.org] is used.\n\n=== Inserting one entity the jdbc way\n[source,kotlin]\n----\ninclude::{docdir}/src/test/java/io/dibog/spring/jdbc/InsertingEntitiesWithGeneratedIDsTest.kt[tags=inserting-single-entity-jdbc,indent=0]\n----\n\u003c1\u003e To be able to create the prepare statement ourself we need to use this method of `JdbcTemplate` to get a jdbc connection.\n\u003c2\u003e This constructor calls creates a prepared statement which can return generated attributes of the insert.\nYou can either specify `Statement.RETURN_GENERATED_KEYS` to return any generated key of the insert,\nor you can specify a StringArray containing the column names or an IntArray containing the column indices\nof the returned generated key columns.\n\u003c3\u003e The prepared statement parameters have to be set the usual jdbc way.\n\u003c4\u003e The returned `Int` should contain the modified rows within the table. In the case of a single insert it should be `1`.\n\u003c5\u003e The extension method `singleGeneratedKey` returns the generated key of the column `ID`. The `singleGeneratedKey`\nmethod can be found in as extension method in the current project.\n\n\n=== Inserting one entity the spring way\n\n[source,kotlin]\n----\ninclude::{docdir}/src/test/java/io/dibog/spring/jdbc/InsertingEntitiesWithGeneratedIDsTest.kt[tags=inserting-single-entity-spring,indent=0]\n----\n\u003c1\u003e This object will contain after the query the generated key.\n\u003c2\u003e Again this method is required to access the jdbc connection.\n\u003c3\u003e Same as before we need to create the prepared statement with the correct constructor call.\n\u003c4\u003e Again the parameters for the prepared statement have to be set the jdbc way.\n\u003c5\u003e The keyholder into which the generated key will be inserted.\n\u003c6\u003e After the call the to the JdbcTemplate query the key can be accessed via the keyholder.\n\n\nBoth methods seems to be of the same length or complexity.\n\n=== Batch insert of multiple entities the jdbc way\n\nThe batch insert returning generated keys is not directly supported by the `JdbcTemplate`. In this project\nyou can find an extension function which supports the `GeneratedKeyHolder` of Spring.\n----\ninclude::{docdir}/src/test/java/io/dibog/spring/jdbc/InsertingEntitiesWithGeneratedIDsTest.kt[tags=inserting-multiple-entity-plain,indent=0]\n----\n\u003c1\u003e To be able to create the prepare statement ourself we need to use this method of `JdbcTemplate` to get a jdbc connection.\n\u003c2\u003e This constructor calls creates a prepared statement which can return generated attributes of the insert.\nYou can either specify `Statement.RETURN_GENERATED_KEYS` to return any generated key of the insert,\nor you can specify a StringArray containing the column names or an IntArray containing the column indices\nof the returned generated key columns.\n\u003c3\u003e The prepared statement parameters have to be set the usual jdbc way.\n\u003c4\u003e The returned `Int` should contain the modified rows within the table. In the case of a single insert it should be `1`.\n\u003c5\u003e The extension method `singleGeneratedKey` returns the generated key of the column `ID`. The `singleGeneratedKey`\nmethod can be found in as extension method in the current project.\n\n=== Batch insert of multiple entities the spring way\n----\ninclude::{docdir}/src/test/java/io/dibog/spring/jdbc/InsertingEntitiesWithGeneratedIDsTest.kt[tags=inserting-multiple-entity-spring,indent=0]\n----\n\u003c1\u003e To be able to create the prepare statement ourself we need to use this method of `JdbcTemplate` to get a jdbc connection.\n\u003c2\u003e This constructor calls creates a prepared statement which can return generated attributes of the insert.\nYou can either specify `Statement.RETURN_GENERATED_KEYS` to return any generated key of the insert,\nor you can specify a StringArray containing the column names or an IntArray containing the column indices\nof the returned generated key columns.\n\u003c3\u003e The prepared statement parameters have to be set the usual jdbc way.\n\u003c4\u003e The returned `IntArray` should contain the modified rows within the table.\n\u003c5\u003e The extension method `generatedKeys` returns the generated key of the column `ID` as a List. The `generatedKeys`\nmethod can be found in as extension method in the current project.\n\n== Misc\n\n=== Where conditions for checking membership in collections\n\nHere an example for querying for an entity:\n\n----\ninclude::{docdir}/src/test/java/io/dibog/spring/jdbc/MiscTest.kt[tags=query-for-entitiy-in]\n----\n\nAnd here the same for querying for an attribute:\n\n----\ninclude::{docdir}/src/test/java/io/dibog/spring/jdbc/MiscTest.kt[tags=query-for-attribute-in]\n----\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdibog%2Fspring-jdbc-template-demo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdibog%2Fspring-jdbc-template-demo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdibog%2Fspring-jdbc-template-demo/lists"}