Make query easier for Java web service.
English | 简体中文
Easyquery can be used to quickly and easily build query requests, such as filtering, searching, and sorting, by defining query objects and automatically building query SQL, eliminating the need to manually splice SQL or assemble query code.
For example, let's say we are using MybatisPlus, our query request interface queries the table job, the entity object is shown below, and now we want to add the fields name, type for filtering, and created_time for sorting. Generally we can write it like this:
// Entity job.java
@Data
@TableName("job")
public class Job {
@TableId(type = IdType.AUTO)
private String id;
private OffsetDateTime createdTime;
private OffsetDateTime updatedTime;
private String name;
private String type;
private String remark;
}
// build query in JobServiceImpl.java, JobMapper and IJobService are omitted.
@Service
public class JobServiceImpl extends ServiceImpl<JobMapper, Job> implements IJobService {
public List<Job> listJob(String name, String type, boolean createdTimeAsc) {
QueryWrapper<Job> wrapper = new QueryWrapper<>();
if (Objects.nonNull(name)) {
wrapper.eq("name", name);
}
if (Objects.nonNull(type)) {
wrapper.eq("type", type);
}
wrapper.orderBy(true, createdTimeAsc, "created_time");
return list(wrapper);
}
}
While Easyquery can simplify the above steps, a series of query construction statement is simplified to a DTO object configuration, through the configuration of the object can be transformed to QueryWrapper, service can be called directly.
// DTO Object
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class JobDTO {
@QueryFilter
private String name;
@QueryFilter
private String type;
@QuerySorter
private Boolean createdTime;
}
// called in controller class(or in service class)
@PostMapping("/listJob")
public ResponseEntity<?> listJob(@RequestBody JobDTO dto) {
MyBatisPlusQueryWrapper<Job> wrapper = new MyBatisPlusQueryWrapper<>(extractorHolder);
QueryWrapper<Job> queryWrapper = wrapper.build(dto);
List<Job> data = jobService.list(queryWrapper);
return ResponseEntity.ok(data);
}
Easyquery can be configured according to the DTO object and the request body is automatically transformed to the QueryWrapper , eliminating the need to manually build the tedious code.
Easyquery also supports a variety of filtering operators (greater than, less than, contains, non-empty , etc.), multi-field search , combined sorting and other functions.
Easyquery core library based on JDK 17 +, no other dependencies, based on the user's Mybatis enhancement framework (currently supports MybatisPlus and MybatisFlex).
Please import the Mybatis-Plus or Mybatis-Flex dependency by yourself first and import the corresponding dependency.
<dependency>
<groupId>io.github.wwtg99</groupId>
<artifactId>easyquery-mybatis-plus</artifactId>
<version>1.1.1</version>
</dependency>
implementation 'io.github.wwtg99:easyquery-mybatis-plus:1.1.1'
<dependency>
<groupId>io.github.wwtg99</groupId>
<artifactId>easyquery-mybatis-flex</artifactId>
<version>1.1.1</version>
</dependency>
implementation 'io.github.wwtg99:easyquery-mybatis-flex:1.1.1'
Documents generated by DeepWiki.
Depends on JUnit 5, please use Maven run the unit tests.
mvn test