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.
- Initialize the project on Spring Initializer with the following 4 dependencies:
- Lombok
- Spring Web
- Validation
- [A database driver]
- 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>
- 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.
Module | Snapshot | Release |
---|---|---|
doyto-query-api | ||
doyto-query-geo | ||
doyto-query-common | ||
doyto-query-sql | ||
doyto-query-jdbc | ||
doyto-query-web-common | ||
doyto-query-web | ||
doyto-query-dialect |
- MySQL
- Oracle
- SQL Server
- PostgreSQL
- SQLite
- HSQLDB
- MongoDB
This project is under the Apache Licence v2.