# easy-query **Repository Path**: hongweifei/easy-query ## Basic Information - **Project Name**: easy-query - **Description**: 用于快速创建查询接口,只需编写API接口路径与对应查询SQL。 - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-07-22 - **Last Updated**: 2025-09-16 ## Categories & Tags **Categories**: database-dev **Tags**: None ## README # EasyQuery for easier http query 用于快速创建查询接口,只需编写API接口路径与对应查询SQL。 使用`Provider`来支持多样的框架,现在已经有: - `provider-db-jdbc` - `provider-db-mybatis` - `provider-db-mybatis-spring` - `provider-http-router-servlet` - `provider-http-router-spring` - `provider-http-router-solon` - `provider-http-server-servlet` - `provider-http-server-spring` 在依赖中更换不同的`Provider`,程序将自动加载。 并且现在已经开发了`easy-query-spring-boot-starter`,只需在`SprintBoot`项目中引入依赖: ```xml net.cyue.web.easyquery easy-query-spring-boot-starter 0.8.0 ``` 并引入需要使用的`provider-db`,启用`EasyQueryApplication`即可使用。示例代码: ```java @SpringBootApplication @EnableEasyQueryApplication("example.toml") public class Main { public static void main(String[] args) { SpringApplication.run(Main.class, args); } } ``` 或不使用注解: ```java @Bean public static EasyQueryApplication easyQueryApplication(ServletContext context) throws ConfigException, IOException { EasyQueryApplication app = EasyQueryApplicationFactory.create(context); EasyQueryConfiguration configuration = EasyQueryTOMLConfiguration.fromStream(ResourceUtil.getResourceAsStream("example.toml")); // app.runByProperties(ResourceUtil.getResourceAsStream("example.properties")); app.run(configuration); app.getContext().registerQuery( PathInfo .builder() .apiPath("/api/v1/userinfo") .addQueryParameter("id") .addQueryParameter("username") .build(), "select * from user where id = #{id} or username = #{username}" ); return app; } ``` 同时也支持`Solon`框架。`Solon`项目使用示例: 首先自行选择合适的`provider-db`,然后在项目中引入`easy-query-http-router-solon`,创建`EasyQueryApplication`。 ```java public static void main(String[] args) throws ConfigException, IOException { SolonApp app = Solon.start(Main.class, args); EasyQueryApplication easyQueryApplication = EasyQueryApplicationFactory.create(solonApp); easyQueryApplication.runByProperties(ResourceUtil.getResourceAsStream("example.properties")); easyQueryApplication.getContext().registerQuery( PathInfo .builder() .apiPath("/api/v1/userinfo") .addQueryParameter("id") .addQueryParameter("username") .build(), "select * from user where id = #{id} or username = #{username}" ); } ``` `EasyQueryApplication`配置文件示例: `toml`: ```toml [application.context] path = "/" [application.context.handler] exception = "" result = "" response = "" [queryUserList] type = "API" method = "GET" path = "/api/v1/user/list" sql = """ select * from user """ parameters = [] [pageQueryUserList] type = "API" method = "GET" path = "/api/v1/user/page" queryType = "page" pageParameter = "page" pageSizeParameter = "pageSize" sql = """ select * from user """ parameters = [ "page" ] [[pageQueryUserList.parameter]] name = "pageSize" type = "int" required = true [[pageQueryUserList.parameter]] name = "name" type = "string" description = "optional" required = false defaultValue = "默认值" ``` `json`: ```json { "上下文路径": { "group": "application.context", "name": "path", "value": "/" }, "上下文路径key版": { "key": "application.context.path", "value": "/" }, "application": { "context": { "path": "/", "handler": { "exception": "", "result": "", "response": "" } } }, "查询用户列表": { "type": "API", "method": "GET", "path": "/api/v1/user/list", "sql": "select * from user", "queryParameters": [], "queryParameter": [], "parameters": [], "parameter": [] }, "分页查询用户列表": { "type": "API", "method": "GET", "path": "/api/v1/user/page", "parameters": [ "page", { "name": "pageSize", "type": "int", "default": 10 }, { "name": "name", "type": "string", "description": "optional", "required": false, "defaultValue": "默认值" } ], "queryType": "page", "pageParameter": "page", "pageSizeParameter": "pageSize", "page": "page", "pageSize": "pageSize", "sql": "select * from user" } } ``` `properties`: ```properties application.context.path=/ application.context.handler.exception=自定义的异常处理器(IWebExceptionHandler)实现类类名 application.context.handler.result=自定义的查询结果处理器(IWebResultHandler)实现类类名 application.context.handler.response=自定义的响应处理器(IWebResponseHandler)实现类类名 api.v1.user.list=select * from user api.v1.user.{username}=select * from user where username = #{username} ``` 配置文件使用`getResourceAsStream`进行加载,请将配置文件放在resources文件夹。 `provider-db-jdbc`与`provider-db-mybatis`配置文件示例: ```properties url=jdbc:mysql://localhost:3306/jdbc_test?useUnicode=true&characterEncoding=utf8 user=root password=123456 ``` 在`Spring`项目中使用`provider-db-mybatis-spring`: ```xml ``` 或使用`mybatis.properties`。 在`Spring-Boot`项目中使用`provider-db-mybatis-spring`: ```yaml spring: datasource: url: jdbc:mysql://localhost:3306/jdbc_test?useUnicode=true&characterEncoding=utf8 username: root password: 123456 mybatis: mapper-locations: classpath:mapper/**/*.xml ``` 或使用`mybatis.properties`。