Skip to content

itfsw/QueryBuilder

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

90 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Document

中文  |  English

QueryBuilder

这个是jQuery QueryBuilder插件的JAVA版本后台实现。

jQuery QueryBuilder


Maven引用:

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

基础使用

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"));
        }
    }
}

进阶使用

项目提供了自定义RuleFilter、RuleParser、GroupParser功能。其中RuleFilter进行对rule的验证和数据过滤等工作,而GroupParser、RuleParser则可以进行自定义规则的解析。 自定义RuleFilter和RuleParser使用Factory相应的addXXX、addXXXBefore、addXXXAt、addXXXAfter进行替换添加。


1.自定义RuleFilter

自定义类实现IRuleFilter接口
系统自带:

2.自定义RuleParser

根据数据库的不同,分别实现AbstractMongodbRuleParser或者AbstractSqlRuleParser

系统自带:

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")对应的Sql和Mongodb实现。

3.自定义GroupParser

自定义实现实现IGroupParser

系统自带

实现了AND、OR以及NOT操作