第三節:Activiti6.0——Query的API使用
阿新 • • 發佈:2020-06-24
一、概述
介紹:對於資料庫的資訊都可以使用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
複製程式碼