https://github.com/mineking9534/databaseutils
Reflection driven database library for Java
https://github.com/mineking9534/databaseutils
database java postgres reflection sql
Last synced: about 2 months ago
JSON representation
Reflection driven database library for Java
- Host: GitHub
- URL: https://github.com/mineking9534/databaseutils
- Owner: MineKing9534
- License: mit
- Created: 2024-04-03T18:35:38.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2024-10-29T20:10:05.000Z (over 1 year ago)
- Last Synced: 2025-10-20T04:04:03.048Z (6 months ago)
- Topics: database, java, postgres, reflection, sql
- Language: Java
- Homepage:
- Size: 108 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: Readme.md
- License: LICENSE
Awesome Lists containing this project
README
![[Java CI]](https://github.com/MineKing9534/DatabaseUtils/actions/workflows/check.yml/badge.svg)
![[Latest Version]](https://maven.mineking.dev/api/badge/latest/releases/de/mineking/DatabaseUtils?prefix=v&name=Latest%20Version&color=0374b5)
> [!NOTE]
> If you use Kotlin it is highly recommended to use [KORMite](https://github.com/MineKing9534/KORMite) instead. It is a kotlin rewrite of this library with more features, more stability and a cleaner API interface.
# Installation
DatabaseUtils is hosted on a custom repository at [https://maven.mineking.dev](https://maven.mineking.dev/#/releases/de/mineking/DatabaseUtils). Replace VERSION with the lastest version (without the `v` prefix).
Alternatively, you can download the artifacts from jitpack (not recommended).
### Gradle
```groovy
repositories {
maven { url "https://maven.mineking.dev/releases" }
}
dependencies {
implementation "de.mineking:DatabaseUtils:VERSION"
}
```
### Maven
```xml
mineking
https://maven.mineking.dev/releases
de.mineking
DatabaseUtils
VERSION
```
## Usage
### Example 1
```java
public class User {
@Column(key = true)
public ID id; //You can use the JavaUtils ID class as column. It will automatically be generated. For custom types see TypeMapper and DatabaseManager#addMapper
@Column
public String name;
@Column
public String email;
public User() {}
public User(String name, String email) {
this.name = name;
this.email = email;
}
}
public class Main {
public static void main(String[] args) {
DatabaseManager manager = new DatabaseManager("localhost:5432/test", "user", "password");
Table table = manager.getTable(User.class, User::new, "users");
var user = new User("Rick Astley", "rick@example.com");
table.insert(user);
System.out.println(user.id.asString()); //The id has been generated automatically
}
}
```
### Example 2
```java
public class Book implements DataClass {
@Column(autoincrement = true, key = true)
public int id;
@Column
public String title;
@Column
public String author;
private final Table table;
public Book(Table table) {}
public Book(Table table, String title, String author) {
this.title = title;
this.author = author;
this.table = table;
}
@Override
public String toString() {
return id + ": " + title + " by " + author;
}
}
public class Main {
public final DatabaseManager manager;
public final Table table;
public static void main(String[] args) {
new Main();
}
public Main() {
manager = new DatabaseManager("localhost:5432/test", "user", "password");
table = manager.getTable(Book.class, this::createInstance, "books");
System.out.println(new Book(table, "My Life", "Rick Astley").update());
System.out.println(table.selectAll(Order.ascendingBy("id").limit(5)));
table.selectMany(Where.greateOrEqual("id", 3)).forEach(Book::delete);
}
private Book createInstance() {
return new Book(table);
}
}
```
### Example 3
```java
public interface Bookshelf extends Table { //You can create custom tables
default List getByAuthor(String author) {
return selectMany(Where.equals("author", author));
}
}
public class Main {
public static void main(String[] args) {
DatabaseManager manager = new DatabaseManager("localhost:5432/test", "user", "password");
Bookshelf table = manager.getTable(Bookshelf.class, User.class, User::new, "books");
System.out.println(table.getByAuthor("Rick Astley"));
}
}
```