Anima
Anima
allows you to query database like SQL
and Stream
.
a simple DSL syntax, supports multiple databases, integrates well with Java8,
supports multiple relational mappings, and is a database manipulation tool.
Feature
- Simple DSL
- H2、MySQL、SQLite、PostgreSQL、Oracle、SqlServer
- Paging support
- Flexible configuration
- Connection pool support
- Support
LocalDate
、LocalDateTime
- Support lambda expression
- Relationship (
hasOne
、hasMany
、belongsTo
) - SQL performance statistics
- Based Java8
Usage
Latest snapshot version
If you want to prioritize new features or some BUG fixes you can use it, you need to specify the snapshot repository in
pom.xml
1 | <repository> |
Here’s the RELEASE
version.
As Gradle
1 | compile 'io.github.biezhi:anima:0.2.3' |
As Maven
1 | <dependency> |
📒 Although
Anima
can also be used by adding a jar package, we do not recommend doing this.
Examples
Open Connection
1 | // MySQL |
📕 This operation only needs one time
1 | public class User extends Model { |
Table Structure
1 | CREATE TABLE `users` ( |
Query
1 | long count = select().from(User.class).count(); |
Limit
1 | List<User> users = select().from(User.class).order("id desc").limit(5); |
Paging
1 | Page<User> userPage = select().from(User.class).order("id desc").page(1, 3); |
Map
1 | select().from(User.class).map(User::getUserName).limit(3).collect(Collectors.toList()); |
Filter
1 | select().from(User.class).filter(u -> u.getAge() > 10).collect(Collectors.toList()); |
Lambda
1 | User user = select().from(User.class).where(User::getUserName).eq("jack").one(); |
1 | List<User> user = select().from(User.class) |
1 | select().from(User.class).order(User::getId, OrderBy.DESC).order(User::getAge, OrderBy.ASC).all(); |
Join
1 | "order_info") (name = |
1 | // HasOne |
Insert
1 | Integer id = new User("biezhi", 100).save().asInt(); |
or
1 | Anima.save(new User("jack", 100)); |
Batch Save
1 | List<User> users = new ArrayList<>(); |
📘 This operation will begin a transaction and rollback when there is a transaction that is unsuccessful.
Update
1 | int result = update().from(User.class).set("user_name", newName).where("id", 1).execute(); |
or
1 | int result = update().from(User.class).set("user_name", newName).where("id", 1).execute(); |
or
1 | User user = new User(); |
1 | update().from(User.class).set(User::getUserName, "base64").updateById(2); |
1 | update().from(User.class).set(User::getUserName, "base64").where(User::getId).eq(2).execute(); |
Delete
1 | int result = delete().from(User.class).where("id", 1).execute(); |
or
1 | User user = new User(); |
1 | delete().from(User.class).where(User::getId).deleteById(3); |
Transaction
1 | Anima.atomic(() -> { |
📗
Anima
uses theatomic
method to complete a transaction. normally, the code will not throw an exception.
when aRuntimeException
is caught, the transaction will berollback
.
Test Code
See here
License
Apache2