https://github.com/landawn/abacus-jdbc
Coding with SQL/DB is just like coding with Collections
https://github.com/landawn/abacus-jdbc
dao java jdbc jpa orm sql sqlbuilder
Last synced: about 2 months ago
JSON representation
Coding with SQL/DB is just like coding with Collections
- Host: GitHub
- URL: https://github.com/landawn/abacus-jdbc
- Owner: landawn
- License: apache-2.0
- Created: 2019-10-01T05:05:08.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2026-02-28T08:51:26.000Z (about 2 months ago)
- Last Synced: 2026-02-28T13:49:19.733Z (about 2 months ago)
- Topics: dao, java, jdbc, jpa, orm, sql, sqlbuilder
- Language: Java
- Homepage:
- Size: 79.7 MB
- Stars: 18
- Watchers: 2
- Forks: 3
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGES.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# abacus-jdbc
[](https://maven-badges.herokuapp.com/maven-central/com.landawn/abacus-jdbc/)
[](https://www.javadoc.io/doc/com.landawn/abacus-jdbc/4.6.2/index.html)
Experience the simplicity of working with SQL/DB as naturally as working with Collections.
## Features:
This library focuses on three core areas:
* Write or generate `SQL scripts` (optional):: [SQLBuilder](https://htmlpreview.github.io/?https://github.com/landawn/abacus-jdbc/blob/master/docs/SQLBuilder_view.html),
[DynamicSQLBuilder](https://htmlpreview.github.io/?https://github.com/landawn/abacus-jdbc/blob/master/docs/DynamicSQLBuilder_view.html).
```java
// Manually write the sql in plain string.
String query = "SELECT id, first_name, last_name, email FROM user WHERE first_Name = ?";
// Or by SQLBuilder
String query = PSC.select("id", "firstName, "lastName", "email").from(User.class).where(Filters.eq("firstName")).sql();
// Or if select all columns from user:
String query = PSC.selectFrom(User.class).where(Filters.eq("firstName")).sql();
// Sql scripts can also be placed in sql mapper xml file and then associated with a DAO object.
UserDao userDao = JdbcUtil.createDao(UserDao.class, dataSource, sqlMapper);
```
`userSqlMapper.xml`
```xml
SELECT id, first_name, last_name, email FROM user WHERE first_Name = ?
```
* Prepare `statements/queries`: [PreparedQuery](https://htmlpreview.github.io/?https://github.com/landawn/abacus-jdbc/blob/master/docs/PreparedQuery_view.html),
[NamedQuery](https://htmlpreview.github.io/?https://github.com/landawn/abacus-jdbc/blob/master/docs/NamedQuery_view.html),
[CallableQuery](https://htmlpreview.github.io/?https://github.com/landawn/abacus-jdbc/blob/master/docs/CallableQuery_view.html) with a `sql` or `Dao` mapped with `sqls`.
```java
// sql can be used to create PreparedQuery/NamedQuery/CallableQuery
PreparedQuery preparedQuery = JdbcUtil.prepareQuery(dataSource, query...);
//.prepareQuery(connection, query...)
//.prepareNamedQuery(dataSource, namedQuery...)
//.prepareCallableQuery(dataSource, query...)
//....
.setString(1, fistName) // Firstly set query parameters, if needed.
//.setLong(paramName, paramValue) // set named parameters for NamedQuery/CallableQuery.
//.setParameters(entity) // set named parameters by entity with getter/setter methods
//.setParameters(Map) // set named parameters by Map
//.setParameters(param1, param2...) // set several parameters in one line.
//.setParameters(Collection> parameters) // set parameters with a Collection.
//.setParameters(ParametersSetter parametersSetter) // set parameters by functional interface.
//....
// Sql can also be associated to a self-defined DAO method. (There are tens of most used predefined methods in DAO interfaces which be used without write single line of code).
public interface UserDao extends CrudDao, JoinEntityHelper {
// This is just a sample. Normally there are pre-defined methods available for this query: userDao.list(Condition cond).
// Methods defined in Dao interface don't require implementation. Of course, Customized implemnetation is also supported by default method.
@Query("SELECT id, first_name, last_name, email FROM user WHERE first_Name = ?")
List selectUserByFirstName(String firstName) throws SQLException;
// Or id of the sql script defined in xml mapper file.
@Query(id = "selectUserByFirstName")
List selectUserByFirstName(String firstName) throws SQLException;
// Or id of the sql script defined in below nested static class.
// Instead of writing sql scripts manually, you can also use SQLBuilder/DynamicSQLBuilder to write sql scripts.
@Query(id = "selectUserByFirstName")
List selectUserByFirstName(String firstName) throws SQLException;
// Multiple updates within transaction.
@Transactional
@Query({ "update user set first_name = ? where id = ?",
"update user set last_name = ? where id = :id" })
default void updateFirstNameLastNameByIds(long idForUpdateFirstName, long idForUpdateLastName, String... sqls) throws SQLException { // Last parameter must be String[]. It will be automatically filled with sqls in @Sql.
prepareQuery(sqls[0]).setLong(1, idForUpdateFirstName).update();
prepareNamedQuery(sqls[1]).setLong(1, idForUpdateLastName).update();
}
// Refer classes in package com.landawn.abacus.jdbc.annotation for more supported annations
@Query("SELECT * FROM {tableName} where id = :id ORDER BY {{orderBy}}")
User selectByIdWithDefine(@Define("tableName") String tableName, @Define("{{orderBy}}") String orderBy, @Bind("id") long id);
static final class SqlTable {
@SqlScript
static final String selectUserByFirstName = PSC.select("id", "firstName, "lastName", "email").from(User.class).where(Filters.eq("first")).sql();
}
}
UserDao userDao = JdbcUtil.createDao(UserDao.class, dataSource, ...);
```
* Execute and retrieve results (when needed):
[Dao](https://htmlpreview.github.io/?https://github.com/landawn/abacus-jdbc/blob/master/docs/Dao_view.html)/[CrudDao](https://htmlpreview.github.io/?https://github.com/landawn/abacus-jdbc/blob/master/docs/CrudDao_view.html)/[JoinEntityHelper](https://htmlpreview.github.io/?https://github.com/landawn/abacus-jdbc/blob/master/docs/JoinEntityHelper_view.html),
[Jdbc](https://htmlpreview.github.io/?https://github.com/landawn/abacus-jdbc/blob/master/docs/Jdbc_view.html),
[Dataset](https://htmlpreview.github.io/?https://github.com/landawn/abacus-jdbc/blob/master/docs/Dataset_view.html),
[Filters](https://htmlpreview.github.io/?https://github.com/landawn/abacus-jdbc/blob/master/docs/Filters_view.html),
[JdbcUtil](https://htmlpreview.github.io/?https://github.com/landawn/abacus-jdbc/blob/master/docs/JdbcUtil_view.html),
[JdbcUtils](https://htmlpreview.github.io/?https://github.com/landawn/abacus-jdbc/blob/master/docs/JdbcUtils_view.html).
```java
// Execute the sql by a PreparedQuery/NamedQuery/CallableQuery
preparedQuery.findFirst()
//.findFirst(User.class)/findFirst(rowMapper)/...
//.findOnlyOne()/findOnlyOne(User.class)/findOnlyOne(rowMapper)/...
//.list()/list(User.class)/list(rowMapper)/...
//.stream()/stream(User.class)/stream(rowMapper)/...
//.query()/qurey(resultExtractor)/queryForInt/queryForString/queryForSingleResult/...
//.exists()/ifExists(rowConsumer)
//.update/batchUpdate/execute/...
// Sql can also be executed by directly calling DAO methods.
userDao.selectUserByFirstName(firstName)
//.updateFirstNameLastNameByIds(100, 101)
//.findFirst(Condition)
//.findOnlyOne(Condition)
//.list(Condition)
//.stream(Condition)
//.update(user)/deleteById(userId)/batchInsert(Collection)/...
```
* Code Generation: [CodeGenerationUtil](https://www.javadoc.io/doc/com.landawn/abacus-common/latest/com/landawn/abacus/util/CodeGenerationUtil.html), [JdbcCodeGenerationUtil](https://www.javadoc.io/doc/com.landawn/abacus-jdbc/latest/com/landawn/abacus/jdbc/JdbcCodeGenerationUtil.html).
## Why abacus-jdbc?
The biggest difference between this library and other data(database) access frameworks is the simplicity/consistency/integrity in the APIs design.
Refer to: [Abacus-JDBC vs Spring Data (JPA & JDBC), MyBatis, and Hibernate – A Comparative Analysis](https://github.com/landawn/abacus-jdbc/blob/master/docs/Abacus-JDBC%20vs%20Spring%20Data%20(JPA%20%26%20JDBC)%2C%20MyBatis%2C%20and%20Hibernate%20%E2%80%93%20A%20Comparative%20Analysis.pdf)
## Design and Implementation Considerations:
* In general, embedding SQL scripts where they are used is easier to review and maintain than saving them in separate files.
* While high-level abstractions such as JPA or Spring’s `CrudRepository` enhance development productivity, preserving the ability for users to write and execute SQL offers essential flexibility. SQL is a simple, expressive, and powerful language, and should be leveraged instead of being avoided without justification.
* High-level abstraction APIs should simplify implementation, improve productivity, and avoid increasing complexity or reducing performance.
## Download/Installation & [Changes](https://github.com/landawn/abacus-jdbc/blob/master/CHANGES.md):
* [Maven](https://central.sonatype.com/artifact/com.landawn/abacus-jdbc)
```xml
com.landawn
abacus-jdbc
4.6.2
```
* Gradle:
```gradle
// JDK 17 or above:
compile 'com.landawn:abacus-jdbc:4.6.2'
```
## User Guide:
* [Introduction to JDBC](https://www.javacodegeeks.com/2015/02/jdbc-tutorial.html)
* [More samples/questions(?)](https://github.com/landawn/abacus-jdbc/tree/master/samples/com/landawn/abacus/samples)
## Also See: [abacus-common](https://github.com/landawn/abacus-common), [abacus-entity-manager](https://github.com/landawn/abacus-entity-manager).
## Recommended Java programming libraries/frameworks:
[lombok](https://github.com/rzwitserloot/lombok),
[Apache Druid](https://github.com/apache/druid),
[Sharding-JDBC](https://github.com/apache/incubator-shardingsphere)
... [awesome-java](https://github.com/akullpp/awesome-java#database)
## Recommended Java programming tools:
[Spotbugs](https://github.com/spotbugs/spotbugs), [JaCoCo](https://www.eclemma.org/jacoco/)...