sequence

This paper mainly studies how to use RSQL to achieve dynamic data query from the front end to the back end.

RSQL

The RESTful Service Query Language (RSQL) is a superset of Feed Item Query Language (FIQL) and a Query Language for RESTful services. This is done using rSQL-jPA, which relies on rSQL-Parser to parse RSQL syntax and then escapes parsed RSQL to the JPA Specification.

maven

< the dependency > < groupId > com. Making. Tennaito < / groupId > < artifactId > RSQL - jpa < / artifactId > < version > 2.0.2 < / version > </dependency>Copy the code

It relies on RSQL-Parser

The instance

domain

@Entity public class TodoTask { @javax.persistence.Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private Long totalNum = 0L; private String title; @Version private Long version; / /... }Copy the code

controller

@RestController
@RequestMapping("/rsql")
public class RsqlController {

    @Autowired
    TodoTaskService todoTaskService;

    /**
     * @param condition
     * @param page
     * @param size
     * @return
     */
    @GetMapping("")
    public Page<TodoTask> query(@RequestParam String condition,
                                @RequestParam(required = false,defaultValue = "0") int page,
                                @RequestParam(required = false,defaultValue = "20") int size){
        returntodoTaskService.query(condition,new PageRequest(page,size)); }}Copy the code

service

@Component
public class TodoTaskService {

    @Autowired
    private EntityManager entityManager;

    public Page<TodoTask> query(String condition, Pageable pageable){
        // 1.Create the JPA Visitor
        RSQLVisitor<CriteriaQuery<TodoTask>, EntityManager> visitor = new JpaCriteriaQueryVisitor<TodoTask>();
        // 2.Parse a RSQL into a Node
        Node rootNode = new RSQLParser().parse(condition);
        // 3.Create CriteriaQuery
        CriteriaQuery<TodoTask> criteriaQuery = rootNode.accept(visitor, entityManager);
        List<TodoTask> total = entityManager.createQuery(criteriaQuery).getResultList();
        List<TodoTask> resultList = entityManager.createQuery(criteriaQuery)
                .setFirstResult(pageable.getOffset()).setMaxResults(pageable.getPageSize()).getResultList();

        returnnew PageImpl<>(resultList,pageable, total.size()); }}Copy the code

The EntityManager is used directly for the query in three steps: creating the RSQLVisitor, parsing the condition to the Node, and creating a CriteriaQuery from the Node, from which the query can be performed.

run

curl -i http://localhost:8080/rsql? condition=title==hello curl -i http://localhost:8080/rsql? condition=totalNum%3E50 curl -i http://localhost:8080/rsql? condition=totalNum%3E50; title==helloCopy the code

Where %3E is a > url escape, if there are multiple and conditions, use; separated

summary

RSQL is a powerful abstract language that can be used as a general purpose query language for REST services. Spring-data-rest provides similar and more powerful capabilities. Select * from db (select * from db); select * from db (select * from DB); select * from DB (select * from DB);

doc

  • FIQL: The Feed Item Query Language
  • REST Query Language with RSQL
  • rsql-jpa
  • rsql-parser
  • Using the Criteria API to Create Queries