Skip to content

Repository files navigation

ORM-Java

ORM-Java is an ORM used for SQLite, MySQL, PostgreSQL databases. Using JDBC as the driver at the bottom level. It provides a simple and efficient API without the need to write a large number of SQL statements. You only need to know the basics of SQL to get started.

Features

  • Support automatic creation of tables, addition of columns, and addition of indexes.
  • Provide APIs for adding, deleting, modifying, and querying.
  • Provides aggregate function APIs.
  • APIs are simple, elegant and efficient to use.

Download

Gradle:

repositories {
maven { url 'https://www.jitpack.io' }
}
dependencies {
implementation 'com.github.artbits:orm-java:2.1.0'
}

Maven:

<repository>
<id>jitpack.io</id>
<url>https://www.jitpack.io</url>
</repository>
<dependency>
<groupId>com.github.artbits</groupId>
<artifactId>orm-java</artifactId>
<version>2.1.0</version>
</dependency>

Usage

Let Java classes be mapped into database tables. The id field is a default required field.

publicclassUser {
publicLongid;
@Column(index = true)
publicLonguid;
publicStringname;
publicIntegerage;
publicBooleanvip;
publicUser(Consumer<User> consumer) {
super(consumer);
}
}
publicclassBook {
publicLongid;
publicStringname;
publicStringauthor;
publicDoubleprice;
publicBook(Consumer<Book> consumer) {
super(consumer);
}
}

Connect to the database and load tables (automatically add tables, columns and index).

Configconfig = Config.of(c -> c.url = "jdbc:sqlite:example.db");
DBdb = DB.connect(config);
db.tables(User.class, Book.class);

Insert data.

// No need to set ID, ID will increase automatically when inserting data.Useruser = newUser(u -> {u.name = "Lake"; u.age = 25; u.vip = true;});
db.insert(user);
DB.print(user);

Update data.

// Update data by id.db.update(newUser(u -> {
u.id = 11L;
u.vip = false;
}));
// Update data by condition.db.update(newUser(u -> u.vip = true), "age < ?", 50);

Delete data.

// Delete all data in this table.db.deleteAll(User.class);
// Delete data by IDs.db.delete(User.class, 1L, 2L, 3L);
// Delete data by ID list.db.delete(User.class, Arrays.asList(1L, 2L, 3L));
// Delete data by condition.db.delete(User.class, "name = ? && vip = ?", "Lake", false);

Query data.

// Find one by ID.Useruser1 = db.findOne(User.class, 1L);
// Find one by condition.Useruser2 = db.findOne(User.class, "name = ?", "Lake");
// Find first.Useruser3 = db.first(User.class);
// Find first by condition.Useruser4 = db.first(User.class, "vip = ?", true);
// Find last.Useruser1 = db.last(User.class);
// Find last by condition.Useruser2 = db.last(User.class, "vip = ?", false);
// Find all.List<User> users1 = db.findAll(User.class);
// Find many by IDs.List<User> users2 = db.find(User.class, 1L, 2L, 3L);
// Find many by ID list.List<User> users3 = db.find(User.class, Arrays.asList(1L, 2L, 3L));
// Find many by custom option rules. Options APIs are optional, choose according to actual needs.List<User> users4 = db.find(User.class, options -> options
.select("name", "age")
.where("age <= ? && vip = ?", 50, true)
.order("age", Options.DESC)
.limit(5)
.offset(1));

Aggregate function.

longcount1 = db.count(User.class);
longcount2 = db.count(User.class, "vip = ?", true);
doubleaverage1 = db.average(User.class, "age");
doubleaverage2 = db.average(User.class, "age", "vip = ?", false);
intsum1 = db.sum(User.class, "age").intValue();
intsum2 = db.sum(User.class, "age", "vip = ?", false).intValue();
intmax1 = db.max(User.class, "age").intValue();
intmax2 = db.max(User.class, "age", "vip = ?", false).intValue();
intmin1 = db.min(User.class, "age").intValue();
intmin2 = db.min(User.class, "age", "vip = ?", true).intValue();

Links

License

Copyright 2023 Zhang Guanhu
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

About

A Java ORM for Database.

Topics

Resources

Stars

8 stars

Watchers

1 watching

Forks

Releases

Contributors

Languages