@@ -2,53 +2,8 @@ springboot-starter-data-fast
22
33基于JPA的快速API能力服务
44
5- ## FastController 快速API能力服务
6- ``` java
7- package com.codingapi.springboot.example.query ;
8-
9- import com.codingapi.springboot.example.infrastructure.jpa.entity.DemoEntity ;
10- import com.codingapi.springboot.example.infrastructure.jpa.pojo.PageSearch ;
11- import com.codingapi.springboot.fast.annotation.FastController ;
12- import com.codingapi.springboot.fast.annotation.FastMapping ;
13- import com.codingapi.springboot.framework.dto.response.MultiResponse ;
14- import org.springframework.security.access.prepost.PreAuthorize ;
15- import org.springframework.web.bind.annotation.RequestMethod ;
16-
17- @FastController
18- public interface FastDemoApi {
19-
20-
21- @PreAuthorize (value = " hasRole('ROLE_ADMIN')" )
22- @FastMapping (
23- method = RequestMethod . GET ,
24- mapping = " /api/demo/findByName1" ,
25- value = " select d from DemoEntity d where name = :name" ,
26- countQuery = " select count(d) from DemoEntity d where name = :name" )
27- MultiResponse<DemoEntity > findByName1 (PageSearch query );
28-
29-
30-
31- @PreAuthorize (value = " hasRole('ROLE_USER')" )
32- @FastMapping (
33- method = RequestMethod . GET ,
34- mapping = " /api/demo/findByName2" ,
35- value = " select d from DemoEntity d where name = :name" ,
36- countQuery = " select count(d) from DemoEntity d where name = :name" )
37- MultiResponse<DemoEntity > findByName2 (PageSearch query );
38-
39- }
40-
41- ```
42- @FastController 用于标记当前接口为Fast接口
43- @FastMapping 用于标记当前接口的映射关系
44- mapping为接口映射路径,method为接口请求方法
45- value为查询语句,countQuery为查询总数语句,query为查询参数,支持分页查询,排序查询,查询参数等等
46- MultiResponse为返回结果
47- @PreAuthorize (value = "hasRole('ROLE_USER')") 用于标记当前接口的权限,如果不需要权限可以不用添加
48-
495## FastRepository 的使用教程
506
51-
527继承FastRepository接口,实现自定义的接口,即可使用FastRepository的能力
538``` java
549
@@ -189,41 +144,98 @@ public interface DemoRepository extends FastRepository<Demo,Integer> {
189144 }
190145
191146```
192- ## SortRepository的使用教程
193147
194- ``` java
148+ ## ScriptMapping 教程
195149
196- public interface DemoRepository extends FastRepository< Demo , Integer > , SortRepository< Demo , Integer > {
150+ 通过动态添加mvc mapping实现查询功能.
197151
198- }
199152
200153```
154+ ScriptMapping scriptMapping = new ScriptMapping({mapinggUrl}, {mapinggMethod}, {mappingGrovvry});
155+ scriptMappingRegister.addMapping(scriptMapping);
201156
202- SortRepository的能力展示
157+ ```
158+ mapinggUrl 是mvc接口的地址
159+ mapinggMethod 是mvc接口的请求方式
160+ mappingGrovvry 是执行的查询脚本
203161
204- ``` java
162+ 脚本实例代码:
163+ * 动态分页查询
164+ ```
165+ // 获取name的请求参数
166+ var name = $request.getParameter("name","");
167+ var pageNumber = $request.getParameter("pageNumber",0);
168+ var pageSize = $request.getParameter("pageSize",10);
169+ // 创建分页对象
170+ var pageRequest = $request.pageRequest(pageNumber,pageSize);
171+ // 动态组织sql
172+ var sql = "select * from api_mapping where 1 =1 ";
173+ var countSql = "select count(1) from api_mapping where 1 =1 ";
174+ // 动态组织参数
175+ var params = [];
176+ if(!"".equals(name)){
177+ sql += " and name = ? ";
178+ countSql += " and name = ? ";
179+ params.push(name);
180+ }
181+ sql += " limit ?,?";
182+ // 添加分页参数
183+ params.add(pageRequest.getOffset());
184+ params.add(pageRequest.getPageSize());
185+ // 执行分页查询
186+ return $jdbc.queryForPage(sql,countSql,pageRequest,params.toArray());
187+ ```
188+ * 动态条件查询
189+ ```
190+ // 获取name的请求参数
191+ var name = $request.getParameter("name","");
192+ // 动态组织sql
193+ String sql = "select * from api_mapping where 1=1 ";
194+ // 动态组织参数
195+ var params = [];
196+ if(!"".equals(name)){
197+ sql += " and name = ? ";
198+ params.add(name);
199+ }
200+ // 执行查询
201+ return $jdbc.queryForList(sql,params.toArray());
202+ ```
205203
206- @Test
207- @Transactional
208- void pageSort() {
209- demoRepository. deleteAll();
210- Demo demo1 = new Demo ();
211- demo1. setName(" 123" );
212- demoRepository. save(demo1);
213-
214- Demo demo2 = new Demo ();
215- demo2. setName(" 456" );
216- demoRepository. save(demo2);
217-
218- List<Integer > ids = Arrays . asList(demo1. getId(), demo2. getId());
219- System . out. println(ids);
220- demoRepository. pageSort(PageRequest . of(1 , 10 ), ids);
221-
222- Demo newDemo1 = demoRepository. getReferenceById(demo1. getId());
223- Demo newDemo2 = demoRepository. getReferenceById(demo2. getId());
224-
225- assertEquals(newDemo2. getSort(), 1 );
226- assertEquals(newDemo1. getSort(), 0 );
227- }
204+ 脚本语法介绍:
205+ * $request
206+ ```
207+ // 获取参数name的值,如果参数不存在,则返回默认值
208+ var name = $request.getParameter("name","");
209+ // 获取分页对象
210+ var pageRequest = $request.pageRequest(0,10);
211+ // 获取分页对象的页码
212+ var pageNumber = pageRequest.getPageNumber();
213+ // 获取分页对象的每页记录数
214+ var pageSize = pageRequest.getPageSize();
215+ // 获取分页对象的偏移量
216+ var offset = pageRequest.getOffset();
217+ ```
218+ * $jdbc
219+ ```
220+ // 查询jdbcSQL $jdbc.queryForList({sql},{params})
221+
222+ // 查询无条件的数据
223+ var res = $jdbc.queryForList("select * from api_mapping");
224+ // 查询有条件的数据
225+ var res = $jdbc.queryForList("select * from api_mapping where name = ?",name);
226+ // 查询多条件的数据
227+ var res = $jdbc.queryForList("select * from api_mapping where name = ? and url = ?",name,url);
228+
229+ // 分页查询 $jdbc.queryForPage({sql},{countSql},{pageRequest},{params})
230+ var res = $jdbc.queryForPage("select * from api_mapping where name = ? and url = ?",
231+ "select count(1) from api_mapping where name = ? and url = ?",pageRequest,params.toArray());
232+ ```
233+ * $jpa
234+ ```
235+ // 查询jpa $jpa.listQuery({clazz},{sql},{params})
228236
237+ // 查询无条件的数据
238+ var res = $jpa.listQuery(com.example.entity.NodeEntity.class,"from NodeEntity");
239+ // 查询有条件的数据
240+ var res = $jpa.listQuery(com.example.entity.NodeEntity.class,"from NodeEntity where name = ?",name);
229241```
0 commit comments