IQuery的两个扩展类IBaseQuery和IMultiQuery,它们的实现类分别是BaseQueryImpl和MultiQueryImpl,前者通常用于单表查询,后者则用于多表的复杂查询,包括连接查询。
构造简单单表查询:
SELECT ID,NAME,STATUS
FROM T_URS_NAV
ORDER BY ORDER_NUM ASC
public List<UrsNav> list(String name){
IBaseQuery query = new BaseQueryImpl();
query.table(UrsNav.class);
query.whereLike(UrsNav.T_NAME, name);
query.orderAsc(UrsNav.T_ORDER_NUM);
query.createQuery(UrsNav.T_ID, UrsNav.T_NAME, UrsNav.T_STATUS);
return ursNavService.query(query);
}
|
复杂关联表查询,因传入的name与ursNavId是空的,所以条件被忽略:
SELECT u0.ID,u0.URS_NAV_ID,u0.NAME,u0.PARENT_ID,u0.AUTO_CODE,u0.STATUS,u0.ORDER_NUM,
u1.NAME AS URS_NAV_NAME
FROM T_URS_CATALOG u0,T_URS_NAV u1
WHERE u0.URS_NAV_ID = u1.ID
ORDER BY u0.ORDER_NUM ASC
public List<UrsCatalog> list(String name,String ursNavId){
IMultiQuery query = new MultiQueryImpl();
AliasTable catalog= query.table(UrsCatalog.class);
AliasTable nav = query.table(UrsNav.class);
query.whereEqual(catalog.c(UrsCatalog.T_URS_NAV_ID), nav.c(UrsNav.T_ID));
query.whereLike(catalog.c(UrsCatalog.T_NAME), name);//name空值,忽略
query.whereEqual(catalog.c(UrsCatalog.T_URS_NAV_ID), ursNavId);//ursNavId空值,忽略
query.createQuery(UrsCatalog.class,
catalog.cs(
UrsCatalog.T_ID,
UrsCatalog.T_URS_NAV_ID,
UrsCatalog.T_NAME,
UrsCatalog.T_PARENT_ID,
UrsCatalog.T_AUTO_CODE,
UrsCatalog.T_STATUS),
nav.cs().names(UrsNav.T_NAME).asNames("URS_NAV_NAME"));
return ursSubNavService.query(query);
}
|