QLExpress
github地址:https://github.com/alibaba/QLExpress
推荐博文:https://blog.csdn.net/YuYunTan/article/details/101436910
本文通过规则引擎实现用户输入规则,根据用户规则过滤出用户想要的数据。
数据库初始化,创建testdb数据库:
DROP TABLE IF EXISTS book_info_t;
CREATE TABLE book_info_t (
book_id INT PRIMARY KEY AUTO_INCREMENT,
book_name VARCHAR (64) NOT NULL,
book_author VARCHAR (64) NOT NULL,
book_price DECIMAL (10, 2) NOT NULL,
book_publisher VARCHAR (64) NOT NULL
);
INSERT INTO book_info_t VALUES (1,'MySQL权威指南','张三',99.99,'科学出版社');
INSERT INTO book_info_t VALUES (2,'学习MySQL从入门到精通','李四',100.01,'科学出版社');
INSERT INTO book_info_t VALUES (3,'遥远的救世主','李四',110.01,'清北出版社');
INSERT INTO book_info_t VALUES (4,'大话菜鸟模式','王五',80.36,'两湖出版社');
INSERT INTO book_info_t VALUES (5,'Redis模式','李四,王五',150.66,'电大出版社');
INSERT INTO book_info_t VALUES (6,'编程Cookbook','Lisa',99.66,'疯狂出版社');
结果如下:
引入QLExpress依赖:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<!-- QLExpress依赖 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>QLExpress</artifactId>
<version>3.2.5</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
application.properties文件配置:
server.port=8080
server.servlet.context-path=/book
#MyBatis配置
mybatis.configuration.map-underscore-to-camel-case=true
mybatis.mapper-locations=classpath:mapper/*.xml
#配置MySQL数据源
spring.datasource.url=jdbc:mysql://localhost:3306/testdb?allowMultiQueries=true&serverTimezone=GMT&characterEncoding=utf8
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=123456
实体类BookVO:
package com.lwy.it.book.vo;
import java.io.Serializable;
import lombok.Data;
@Data
public class BookVO implements Serializable {
private static final long serialVersionUID = 5709412010383043539L;
private Integer bookId;
private String bookName;
private String bookAuthor;
private Double bookPrice;
private String bookPublisher;
}
数据库访问接口:
package com.lwy.it.book.dao;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import com.lwy.it.book.vo.BookVO;
@Mapper
public interface BookDao {
public List<BookVO> findAllList();
}
Dao层实现方法:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.lwy.it.book.dao.BookDao">
<select id="findAllList" resultType="com.lwy.it.book.vo.BookVO">
SELECT
book_info_t.book_id,
book_info_t.book_name,
book_info_t.book_author,
book_info_t.book_price,
book_info_t.book_publisher
FROM
book_info_t
</select>
</mapper>
BookController代码如下:逻辑应该放到Service中,通过用户输入规则,实现数据过滤:
package com.lwy.it.book.Controller;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.lwy.it.book.dao.BookDao;
import com.lwy.it.book.vo.BookVO;
import com.ql.util.express.DefaultContext;
import com.ql.util.express.ExpressRunner;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@RestController
public class BookController {
@Autowired
private BookDao bookDao;
@PostMapping("/filter")
public List<BookVO> filterBook(@RequestParam("express") String express) {
/**
* express输出结果:object.bookPrice < 100.0d && object.bookName like "%MySQL%"
* 过滤出:书名称包含MySQL且价格小于100元的数据
*/
log.warn(express);
List<BookVO> list = Collections.emptyList();
list = bookDao.findAllList();
ExpressRunner runner = new ExpressRunner();
DefaultContext<String, Object> context = new DefaultContext<String, Object>();
List<BookVO> resultList = new ArrayList<>();
for (BookVO bookVO : list) {
// 与输入对象object命名保持一致,可直接通过object.bookPrice访问属性
context.put("object", bookVO);
Object result = Boolean.FALSE;
try {
result = runner.execute(express, context, null, true, false);
} catch (Exception exception) {
log.error(exception.getMessage(), exception);
}
if (result.equals(Boolean.TRUE)) {
resultList.add(bookVO);
}
context.clear();
}
return resultList;
}
}
使用Postman调用,返回结果: