第三节:Activiti6.0——Query的API使用

时间:2022-07-25
本文章向大家介绍第三节:Activiti6.0——Query的API使用,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

一、概述

介绍:对于数据库的信息都可以使用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);
}

二、测试

  1. 首先往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);
  2. 使用list//获取流程引擎 ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); //获取身份服务 IdentityService service = processEngine.getIdentityService(); List<Group> list = service.createGroupQuery().list(); list.forEach(group -> { System.out.println(group.getId() + "tt" + group.getName() + "tt" + 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
  3. 使用ListPage方法,分页查询,跟mysql的limit使用一样List<Group> list = service.createGroupQuery().listPage(1, 4);
  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();
  5. 使用count统计数量long count = service.createGroupQuery().count();
  6. 使用SingleResult查询单个数据//单个数据查询 //如果查询出的数据有多个,会报异常:org.activiti.engine.ActivitiException: Query return 2 results instead of max 1 //groupName是根据name进行过滤 Group group = service.createGroupQuery().groupName("name_2").singleResult();
  7. 使用多条件查询//多条件查询,需满足所有条件 List<Group> list = service.createGroupQuery().groupName("name_2").groupId("2").list();
  8. 使用原生的自定义Sql//使用自定义sql List<Group> list = service.createNativeGroupQuery() .sql("select * from act_id_group where NAME_ = #{name}") .parameter("name", "name_4") .list();
  9. 使用like模糊查询List<Group> list = service.createGroupQuery().groupNameLike("%2").list(); //结果:2 name_2 type_2