8 Activiti数据查询(一)
本文节选自《疯狂Workflow讲义(第2版)》。
本书代码目录:https://gitee.com/yangenxiong/CrazyActiviti
本文要点
Activiti的数据查询、排序机制
8.1 Activiti数据查询
Activiti提供了一套数据查询API供开发者使用,可以使用各个服务组件的createXXXQuery方法来获取这些查询对象。本小节将结合用户组数据来讲解Activiti的数据查询设计,这些设计应用于整个Activiti的数据查询体系。
8.1.1 查询对象
Activiti的各个服务组件(XXXService)均提供了createXXXQuery方法,例如本章的IdentityService中的createGroupQuery方法和createUserQuery方法,TaskService中的craeteTaskQuery方法等,这些方法返回的是一个Query实例,例如createGroupQuery返回的是GroupQuery,GroupQuery是Query的子接口。
Query是全部查询对象的父接口,该接口定义了若干个基础方法,各个查询对象均可以使用这些公共方法,包括设置排序方式、数据量统计(count)、列表、分页和唯一记录查询。这些方法描述如下:
asc:设置查询结果的排序方式为升序。
count:计算查询结果的数据量。
desc:设置查询结果的排序方式为降序。
list:封装查询结果,返回相应类型的集合。
listPage:分页返回查询结果。
singleResult:查询单条符合条件的数据,如果查询不到,则返回null,如果查询到多条记录,则抛异常。
下面将以用户组数据为例,讲解这些方法的使用以及注意事项。
8.1.2 list方法
Query接口的list方法,将查询对象对应的实体数据以集合形式返回,返回的集合需要指定元素类型,如果没有查询条件,则会将表中全部的数据查出,默认按照主键(ID_列)升序排序。代码清单6-4中使用list方法。
代码清单6-4:codes\06\6.2\list-data\src\org\crazyit\activiti\ListData.java
/** * 使用Query的list方法 * * @author yangenxiong * */ public class ListData { public static void main(String[] args) { // 创建流程引擎 ProcessEngine engine = ProcessEngines.getDefaultProcessEngine(); // 得到身份服务组件实例 IdentityService identityService = engine.getIdentityService(); // 写入5条用户组数据 createGroup(identityService, "1", "GroupA", "typeA"); createGroup(identityService, "2", "GroupB", "typeB"); createGroup(identityService, "3", "GroupC", "typeC"); createGroup(identityService, "4", "GroupD", "typeD"); createGroup(identityService, "5", "GroupE", "typeE"); // 使用list方法查询全部的部署数据 List<Group> datas = identityService.createGroupQuery().list(); for (Group data : datas) { System.out.println(data.getId() + "---" + data.getName() + " "); } } // 将用户组数据保存到数据库中 static void createGroup(IdentityService identityService, String id, String name, String type) { // 调用newGroup方法创建Group实例 Group group = identityService.newGroup(id); group.setName(name); group.setType(type); identityService.saveGroup(group); } }
在代码清单6-4中,先往数据库中写入5条用户组数据,然后调用Query的list方法将全部数据查出,需要注意的是,在不设置任何排序条件以及排序方式的情况下,将会以主键升序的方式返回结果,代码清单6-4的运行结果如下:
1---GroupA 2---GroupB 3---GroupC 4---GroupD 5---GroupE
8.1.3 listPage方法
listPage方法与list方法类似,最终也是以主键升序排序返回结果集,与list方法不一样的是,listPage方法需要提供两个int参数,第一个参数数据的开始索引,从0开始,第二个参数为结果数量,不难看出,该方法适用于分页查询。代码清单6-5使用listPage方法进行查询。
代码清单6-5:codes\06\6.2\list-page\src\org\crazyit\activiti\ListPage.java
/** * 使用Query的listPage方法 * @author yangenxiong * */ public class ListPage { /** * @param args */ public static void main(String[] args) { //创建流程引擎 ProcessEngine engine = ProcessEngines.getDefaultProcessEngine(); // 得到身份服务组件实例 IdentityService identityService = engine.getIdentityService(); // 写入5条用户组数据 createGroup(identityService, "1", "GroupA", "typeA"); createGroup(identityService, "2", "GroupB", "typeB"); createGroup(identityService, "3", "GroupC", "typeC"); createGroup(identityService, "4", "GroupD", "typeD"); createGroup(identityService, "5", "GroupE", "typeE"); //调用listPage方法,从索引为2的记录开始,查询3条记录 List<Group> datas = identityService.createGroupQuery().listPage(2, 3); for (Group data : datas) { System.out.println(data.getId() + "---" + data.getName() + " "); } } // 将用户组数据保存到数据库中 static void createGroup(IdentityService identityService, String id, String name, String type) { // 调用newGroup方法创建Group实例 Group group = identityService.newGroup(id); group.setName(name); group.setType(type); identityService.saveGroup(group); } }
代码清单6-5中,使用了listPage方法,查询用户组的数据,设置从第二条记录开始,查询3条记录,该方法与MySQL的LIMIT关键字类似。代码清单6-5运行结果如下:
3---GroupC 4---GroupD 5---GroupE
8.1.4 count方法
该方法用于计算查询结果的数据量,类似于SQL中的SELECT COUNT语句,如果不加任何的条件,将会统计整个表的数据量。代码清单6-6使用count方法。
代码清单6-6:codes\06\6.2\count-data\src\org\crazyit\activiti\Count.java
/** * 使用Query的count方法 * * @author yangenxiong * */ public class Count { public static void main(String[] args) { // 创建流程引擎 ProcessEngine engine = ProcessEngines.getDefaultProcessEngine(); // 得到身份服务组件实例 IdentityService identityService = engine.getIdentityService(); // 写入5条用户组数据 createGroup(identityService, UUID.randomUUID().toString(), "GroupA", "typeA"); createGroup(identityService, UUID.randomUUID().toString(), "GroupB", "typeB"); createGroup(identityService, UUID.randomUUID().toString(), "GroupC", "typeC"); createGroup(identityService, UUID.randomUUID().toString(), "GroupD", "typeD"); createGroup(identityService, UUID.randomUUID().toString(), "GroupE", "typeE"); // 使用list方法查询全部的部署数据 long size = identityService.createGroupQuery().count(); System.out.println("Group 数量:" + size); } // 将用户组数据保存到数据库中 static void createGroup(IdentityService identityService, String id, String name, String type) { // 调用newGroup方法创建Group实例 Group group = identityService.newGroup(id); group.setName(name); group.setType(type); identityService.saveGroup(group); } }
本文节选自《疯狂Workflow讲义(第2版)》。