https://github.com/michaelliao/warpdb
DSL-driven RDBMS interface for Java.
https://github.com/michaelliao/warpdb
java jdbc mysql orm spring6 warp
Last synced: over 1 year ago
JSON representation
DSL-driven RDBMS interface for Java.
- Host: GitHub
- URL: https://github.com/michaelliao/warpdb
- Owner: michaelliao
- License: gpl-3.0
- Created: 2016-03-26T00:32:51.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2024-12-02T21:44:37.000Z (over 1 year ago)
- Last Synced: 2025-03-29T16:06:29.655Z (over 1 year ago)
- Topics: java, jdbc, mysql, orm, spring6, warp
- Language: Java
- Homepage:
- Size: 564 KB
- Stars: 96
- Watchers: 5
- Forks: 19
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# warpdb
DSL-driven RDBMS interface for Java:

# Status:
 
### Design principles
* JPA-annotation based configuration.
* DSL-style API reads like English.
* Support raw SQL for very complex query.
* No "attach/dettach".
* All entities are simple POJOs without proxy-ing.
### Database Support
* MySQL 5.x/8.x
### Configuration
Maven dependency:
```
com.itranswarp
warpdb
${warpdb.version}
```
Use 5.x for Spring 5.x and 6.x for Spring 6.x.
Warpdb is built on top of Spring-JDBC. JdbcTemplate or DataSource is required when build warpdb instance:
```
com.test.product.model
com.test.order.model
```
Or using data source:
```
com.test.product.model
com.test.order.model
```
# Basic Usage
### Fully JPA Annotation Support
Entities are configured with JPA annotation:
```
@Entity
@Table(name="user")
public class User {
@Id
String id;
@Column(nullable=false)
String name;
}
```
### Query
Query by primary key:
```
// get user, or throw EntityNotFoundException if not found:
User user = warpdb.get(User.class, "123");
// get user, or return null if not found:
User another = warpdb.fetch(User.class, "456");
```
You have to provide multiple values if multiple id columns are defined:
```
Product p = warpdb.get(Product.class, "p123", "c456);
```
Warpdb supports criteria query and raw SQL query, both are type-safe:
```
List users = warpdb.from(User.class)
.where("name=?", "bob")
.orderBy("updatedAt").desc()
.list();
```
Get first result or null if not found:
```
User user = warpdb.from(User.class)
.where("name=?", "bob")
.orderBy("updatedAt").desc()
.first();
```
Query for update:
```
User user = warpdb.selectForUpdate()
.where("id=?", 123)
.first();
```
Using raw SQL:
```
List users = warpdb.query("select * from User order by name limit 100");
```
### Paged Query
Warpdb supports paged query by specify page index and page size:
```
// page 3, 10 items per page:
PagedResults pr = warpdb.from(User.class)
.orderBy("updatedAt")
.list(3, 10);
System.out.println(pr.page.pageIndex); // 3
System.out.println(pr.page.itemsPerPage); // 10
System.out.println(pr.page.totalPages); // 92
System.out.println(pr.page.totalItems); // 912
List list = pr.results; // current page items
```
A paged query will generate 2 SQLs when execute `list(pageIndex, pageSize)`:
```
SELECT COUNT(*) FROM User;
SELECT * FROM User ORDER BY updatedAt limit 20, 10
```
### Insert
Using `insert()` to insert one or more entities:
```
User user = new User();
user.setId(...);
user.setName(...);
Product product = new Product();
product.setId(...);
product.setName(...);
warpdb.insert(user, product);
```
### Batch insert
Using `insert(List)` to do batch save entities.
### Update
Using `update()` to update one or more entities:
```
User user = ...
user.setName(...);
Product product = ...
product.setName(...);
warpdb.update(user, product);
```
### Batch update
Using `update(List)` to do batch update entities.
### Remove
Using `remove()` to remove one or more entities:
```
User user = ...
Product product = ...
warpdb.remove(user, product);
```
### Batch remove
Using `remove(List)` to do batch remove entities.
# Misc
### Enum Support
Enum is stored as `VARCHAR(50)` in database:
```
@Entity
public class User {
RoleEnum role;
}
```
### Attribute Converter
Values used in Java and db can be converted by attribute converter:
```
@Entity
public class User {
// stored as "DATE" in db:
@Convert(converter = LocalDateConverter.class)
@Column(columnDefinition = "date")
public LocalDate birth;
}
```
### Listeners
Listeners must be added as entity method with annotation `PostLoad`, `PrePersist`, `PostPersist`, `PreUpdate`, `PostUpdate`, `PreRemove`, `PostRemove`:
```
@Entity
public class User {
@Id
String id;
long createdAt;
@PrePersist()
public void prePersist() {
if (this.id == null) {
this.id = nextId();
}
this.createdAt = System.currentTimeMillis();
}
}
```
### Schema Export
Using `getDDL()` to export schema:
```
String ddl = warpdb.getDDL();
```
or get schema for one entity:
```
String ddl = warpdb.getDDL(User.class);
```