一、概述
介绍:对于数据库的信息都可以使用Query接口提供的方法进行查询。此处介绍Query接口的所有API方法使用。为方便演示,使用的是act_id_group表。以下为:Query的所有方法:
public interface Query<T extends Query<?, ?>, U extends Object> {
/**
* 该方法需要在调用orderByXxxx后使用
*/
T asc();
/**
* 该方法需要在调用orderByXxxx后使用
*/
T desc();
long count();
U singleResult();
List<U> list();
List<U> listPage(int firstResult, int maxResults);
}
二、测试
- 首先往act_id_group表中添加数据:
ProcessEngine pe = ProcessEngines.getDefaultProcessEngine();
IdentityService is = pe.getIdentityService();
for (int i = 0; i < 10; i++) {
Group group = is.newGroup(String.valueOf(i));
group.setName("name_" + i);
group.setType("type_" + i);
is.saveGroup(group);
}
//关闭流程引擎
pe.close();
System.exit(0);
- 使用list
//获取流程引擎
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//获取身份服务
IdentityService service = processEngine.getIdentityService();
List<Group> list = service.createGroupQuery().list();
list.forEach(group -> {
System.out.println(group.getId() + "\t\t" + group.getName() + "\t\t" + group.getType());
});
//结果:
//0 name_0 type_0
//1 name_1 type_1
//2 name_2 type_2
//3 name_3 type_3
//4 name_4 type_4
//5 name_5 type_5
//6 name_6 type_6
//7 name_7 type_7
//8 name_8 type_8
//9 name_9 type_9
- 使用ListPage方法,分页查询,跟mysql的limit使用一样
List<Group> list = service.createGroupQuery().listPage(1, 4);
- 使用asc和des进行升序和降序
//升序
List<Group> list1 = service.createGroupQuery().orderByGroupName().asc().list();
//降序
List<Group> list2 = service.createGroupQuery().orderByGroupName().desc().list();
//使用多次排序时,排一次序就指定一次顺序
List<Group> list3 = service.createGroupQuery().orderByGroupName().desc().orderByGroupType().asc().list();
- 使用count统计数量
long count = service.createGroupQuery().count();
- 使用SingleResult查询单个数据
//单个数据查询
//如果查询出的数据有多个,会报异常:org.activiti.engine.ActivitiException: Query return 2 results instead of max 1
//groupName是根据name进行过滤
Group group = service.createGroupQuery().groupName("name_2").singleResult();
- 使用多条件查询
//多条件查询,需满足所有条件
List<Group> list = service.createGroupQuery().groupName("name_2").groupId("2").list();
- 使用原生的自定义Sql
//使用自定义sql
List<Group> list = service.createNativeGroupQuery()
.sql("select * from act_id_group where NAME_ = #{name}")
.parameter("name", "name_4")
.list();
- 使用like模糊查询
List<Group> list = service.createGroupQuery().groupNameLike("%2").list();
//结果:2 name_2 type_2