/doyto-query

DoytoQuery - A Dynamic Query Language Implemented in Java for Database Access

Primary LanguageJavaApache License 2.0Apache-2.0

License Sonar Stats Code Lines Coverage Status

DoytoQuery - A Dynamic Query Language Implemented in Java for CRUD

Introduction

DoytoQuery implements a dynamic query language which generates query statements from objects. Entity/view objects are used to generate table names and columns, while query/having objects are used to dynamically generate query conditions. Each field defined in the query object is used to represent a query condition. When executing query, the query condition corresponding to the assigned field will be combined into the query clause, thereby completing the dynamic construction of SQL statements with entity/view objects.

Refer to the docs for more details.

Quick Usage

  1. Initialize the project on Spring Initializer with the following 4 dependencies:
  • Lombok
  • Spring Web
  • Validation
  • [A database driver]
  1. Add DoytoQuery dependencies in pom.xml:
<dependency>
    <groupId>win.doyto</groupId>
    <artifactId>doyto-query-jdbc</artifactId>
    <version>2.1.0</version>
</dependency>
<dependency>
    <groupId>win.doyto</groupId>
    <artifactId>doyto-query-web</artifactId>
    <version>2.1.0</version>
</dependency>
<dependency>
    <groupId>win.doyto</groupId>
    <artifactId>doyto-query-dialect</artifactId>
    <version>2.1.0</version>
</dependency>
  1. Define entity and query objects for a table:
@Getter
@Setter
@Entity(name = "user")
public class UserEntity extends AbstractPersistable<Long> {
    private String username;
    private Integer age;
    private Boolean valid;
}

@Getter
@Setter
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
public class UserQuery extends PageQuery {
    private String username;
    private Integer ageGe;
    private Integer ageLt;
    private Boolean valid;
}

Invoke the DataAccess#query(Q) method in UserService:

@Service
public class UserService extends AbstractCrudService<UserEntity, Long, UserQuery> {
    public List<UserEntity> findValidGmailUsers() {
        UserQuery userQuery = UserQuery.builder().ageGe(20).valid(true).pageSize(10).build();
        // Executed SQL: SELECT username, email, valid, id FROM t_user WHERE age >= ? AND valid = ? LIMIT 10 OFFSET 0
        // Parameters  : 20(java.lang.Integer), true(java.lang.Boolean)
        return dataAccess.query(userQuery);
    }
}

And a controller to support RESTful API:

@RestController
@RequestMapping("user")
public class UserController extends AbstractEIQController<UserEntity, Long, UserQuery> {
}

Refer to the demo for more details.

Architecture for 0.3.x and newer

architecture-0.3.x

Versions

Module Snapshot Release
doyto-query-api api-snapshots-img api-release-img
doyto-query-geo geo-snapshots-img geo-release-img
doyto-query-common common-snapshots-img common-release-img
doyto-query-sql sql-snapshots-img sql-release-img
doyto-query-jdbc jdbc-snapshots-img jdbc-release-img
doyto-query-web-common web-common-snapshots-img web-common-release-img
doyto-query-web web-snapshots-img web-release-img
doyto-query-dialect dialect-snapshots-img dialect-release-img

Supported Databases

  • MySQL
  • Oracle
  • SQL Server
  • PostgreSQL
  • SQLite
  • HSQLDB
  • MongoDB

License

This project is under the Apache Licence v2.