Anima allows you to query database like SQL and Stream.

More designs and documents are available here.

Feature

  • Simple DSL
  • H2, MySQL, SQLite, PostgreSQL, Oracle, SqlServer
  • Paging support
  • Flexible configuration
  • Connection pool support
  • Support LocalDate,LocalDateTime
  • SQL performance statistics
  • Based Java8

Usage

As Gradle

The compile 'IO. Making. Biezhi: anima: 0.0.4'Copy the code

As Maven

<dependency> <groupId>io.github. Biezhi </groupId> <artifactId>anima</artifactId> <version>0.0.4</version> </dependency>Copy the code

📒 Although Anima can also be used by adding a jar package, we do not recommend doing this.

Examples

Open Connection

/ / MySQL Anima. Open (" JDBC: MySQL: / / 127.0.0.1:3306 / demo ", "root", "123456"); // SQLite Anima.open("jdbc:sqlite:./demo.db", null, null); // H2 Anima.open("jdbc:h2:file:~/demo; FILE_LOCK=FS; PAGE_SIZE=1024; CACHE_SIZE=8192", "sa", "");Copy the code

📕 This operation only needs one time

public class User extends Model { private Integer id; private String userName; private Integer age; public User() { } public User(String userName, Integer age) { this.userName = userName; this.age = age; }}Copy the code

Query

long count = select().from(User.class).count(); // SELECT COUNT(*) FROM users long count = select().from(User.class).where("age > ?", 15).isNotNull("user_name").count(); // SELECT COUNT(*) FROM users WHERE age > ? AND user_name IS NOT NULL User user = select().from(User.class).byId(2); // SELECT * FROM users WHERE id = ? List<User> users = select().from(User.class).byIds(1, 2, 3); // SELECT * FROM users WHERE id IN (? ,? ,?) String name = select().bySQL(String.class, "select user_name from users pageSize 1").one(pageSizeSize ? List<String> names = select().bySQL(String.class, "select user_name from users pageSize ?" , 3pageSizes pageSize ? List<User> users = select().from(User.class).all(); // SELECT * FROM users List<User> users = select().from(User.class).like("user_name", "%o%").all(); // SELECT * FROM users WHERE user_name LIKE ?Copy the code

pageSize

List<User> users = select().from(User.class).order("id desc").pageSize(5);
// SELECT * FROM users ORDER BY id desc LIMIT ?, ?   

List<User> users = select().from(User.class).order("id desc").pageSize(2, 3);
// SELECT * FROM users ORDER BY id desc LIMIT ?, ?Copy the code

paging

Page<User> userPage = select().from(User.class).order("id desc").pageNum(1, 3);
// SELECT * FROM users ORDER BY id desc LIMIT ?, ?Copy the code

Insert

Integer id = new User("biezhi", 100).save().asInt(); // INSERT INTO users(id,user_name,age) VALUES (? ,? ,?)Copy the code

or

Anima.save(new User("jack", 100));Copy the code

Batch Save

List<User> users = new ArrayList<>();
users.add(new User("user1", 10));
users.add(new User("user2", 11));
users.add(new User("user3", 12));
Anima.saveBatch(users);Copy the code

📘 This operation will begin a transaction and rollback when there is a transaction that is unsuccessful.

Update

int result  = update().from(User.class).set("user_name", newName).where("id", 1).execute();
// UPDATE users SET username = ? WHERE id = ?Copy the code

or

int result = update().from(User.class).set("user_name", newName).where("id", 1).execute();
// UPDATE users SET user_name = ? WHERE id = ?Copy the code

or

User user = new User();
user.setId(1);
user.setUserName("jack");
user.update();
// UPDATE users SET user_name = ? WHERE id = ?Copy the code

Delete

int result = delete().from(User.class).where("id", 1).execute();
// DELETE FROM users WHERE id = ?Copy the code

or

User user = new User();
user.setAge(15);
user.setUserName("jack");
user.delete();
// DELETE FROM users WHERE user_name = ? and age = ?Copy the code

Transaction

Anima.atomic(() -> {
    int a = 1 / 0;
    new User("apple", 666).save();
}).catchException(e -> Assert.assertEquals(ArithmeticException.class, e.getClass()));Copy the code

📗 Anima uses the atomic method to complete a transaction. normally, the code will not throw an exception. when a RuntimeException is caught, the transaction will be rollback.

Test Code

See here

License

Apache2