Skip to content

Latest commit

 

History

History
82 lines (67 loc) · 3.99 KB

README-EN.md

File metadata and controls

82 lines (67 loc) · 3.99 KB

Document

中文  |  English

QueryBuilder

An Java Backend for jQuery QueryBuilder

jQuery QueryBuilder


Maven引用:

<dependency>
  <groupId>com.itfsw</groupId>
  <artifactId>QueryBuilder</artifactId>
  <version>1.0.2</version>
</dependency>

Basic Usage

public class Test {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private MongoTemplate mongoTemplate;

    public void test() throws IOException {
        String json = "{\"condition\":\"OR\",\"rules\":[{\"id\":\"name\",\"field\":\"username\",\"type\":\"string\",\"input\":\"text\",\"operator\":\"equal\",\"value\":\"Mistic\"}],\"not\":false,\"valid\":true}";

        // ----------------------------------------- SQL -----------------------------------------
        // get SqlBuilder
        SqlQueryBuilderFactory sqlQueryBuilderFactory = new SqlQueryBuilderFactory();
        SqlBuilder sqlBuilder = sqlQueryBuilderFactory.builder();

        // build query
        SqlQueryResult sqlQueryResult = sqlBuilder.build(json);

        // execute
        jdbcTemplate.query(new StringBuffer("SELECT * FROM `user` WHERE ").append(sqlQueryResult.getQuery()).toString(), sqlQueryResult.getParams().toArray(), rs -> {
            System.out.println(rs.getString("username"));
        });

        // ----------------------------------------- Mongodb -----------------------------------------
        // get MongodbBuilder
        MongodbQueryBuilderFactory mongodbQueryBuilderFactory = new MongodbQueryBuilderFactory();
        MongodbBuilder mongodbBuilder = mongodbQueryBuilderFactory.builder();

        // build query
        MongodbQueryResult mongodbQueryResult = mongodbBuilder.build(json);

        // execute
        DBCursor cursor = mongoTemplate.getCollection("user").find(mongodbQueryResult.getQuery());
        while (cursor.hasNext()){
            System.out.println(cursor.next().get("username"));
        }
    }
}

Advanced

The project provides custom RuleFilter, RuleParser, and GroupParser features. In the RuleFilter, the validation of rule and data filtering are performed, while RuleParser and GroupParser can be used to resolve the custom rules. Custom RuleFilter and RuleParser can be replaced with factory corresponding addXXX, AddXXXBefore, AddXXXAt, and AddXXXAfter.


1.Custom RuleFilter

Custom RuleFilter Class must be implementIRuleFilter
Provide:

2.Custom RuleParser

Depending on the database,Custom RuleParser Class must be implementAbstractMongodbRuleParser or AbstractSqlRuleParser

Provide:

operator("equal","not_equal","in","not_in","less","less_or_equal","greater","greater_or_equal","between","not_between","begins_with","not_begins_with","contains","not_contains","ends_with","not_ends_with","is_empty","is_not_empty","is_null","is_not_null")。

3.Custom GroupParser

Custom RuleFilter Class must be implementIGroupParser

Provide:

default implements has provide "AND,OR,NOT"