指定字段排序
平台默认排序字段,参考IdModel,按创建时间和ID倒序(ordering = "createDate DESC, id DESC")
方法1:模型指定排序
模型定义增加排序字段。@Model.Advanced(ordering = "xxxxx DESC, yyyy DESC")
@Model.model(PetShop.MODEL_MODEL)
@Model(displayName = "宠物店铺",summary="宠物店铺",labelFields ={"shopName"})
@Model.Code(sequence = "DATE_ORDERLY_SEQ",prefix = "P",size=6,step=1,initial = 10000,format = "yyyyMMdd")
@Model.Advanced(ordering = "createDate DESC")
public class PetShop extends AbstractDemoIdModel {
public static final String MODEL_MODEL="demo.PetShop";
// …………
}
方法2:Page查询中可以自定排序规则
- API参考 pro.shushi.pamirs.meta.api.dto.condition.Pagination#orderBy
public <G, R> Pagination<T> orderBy(SortDirectionEnum direction, Getter<G, R> getter) { if (null == getSort()) { setSort(new Sort()); } getSort().addOrder(direction, getter); return this; }
- 具体示例
@Function.Advanced(type= FunctionTypeEnum.QUERY) @Function.fun(FunctionConstants.queryPage) @Function(openLevel = {FunctionOpenEnum.API}) public Pagination<PetShop> queryPage(Pagination<PetShop> page, IWrapper<PetShop> queryWrapper){ page.orderBy(SortDirectionEnum.DESC, PetShop::getCreateDate); page = new PetShop().queryPage(page, queryWrapper); return page; }
方法3:查询的wapper中指定
- API参考:pro.shushi.pamirs.framework.connectors.data.sql.AbstractWrapper#orderBy
@Override public Children orderBy(boolean condition, boolean isAsc, R... columns) { if (ArrayUtils.isEmpty(columns)) { return typedThis; } SqlKeyword mode = isAsc ? ASC : DESC; for (R column : columns) { doIt(condition, ORDER_BY, columnToString(column), mode); } return typedThis; }
具体示例
public List<PetShop> queryList(String name) {
List<PetShop> petShops = Models.origin().queryListByWrapper(
Pops.<PetShop>lambdaQuery().from(PetShop.MODEL_MODEL)
.orderBy(true, true, PetShop::getCreateDate)
.orderBy(true, true, PetShop::getId)
.like(PetShop::getShopName, name));
return petShops;
}
设置查询不排序
方法1:关闭平台默认排序字段,设置模型的ordering,改成:ordering = "1=1"
模型定义增加排序字段。@Model.Advanced(ordering = "1=1")
@Model.model(PetShop.MODEL_MODEL)
@Model(displayName = "宠物店铺",summary="宠物店铺",labelFields ={"shopName"})
@Model.Code(sequence = "DATE_ORDERLY_SEQ",prefix = "P",size=6,step=1,initial = 10000,format = "yyyyMMdd")
@Model.Advanced(ordering = "1=1")
public class PetShop extends AbstractDemoIdModel {
public static final String MODEL_MODEL="demo.PetShop";
// …………
}
在ORDER BY 1=1中,1=1是一个条件表达式,它总是会返回true(或者在某些数据库中是1),因为1等于1。因此,这个条件实际上没有改变排序的结果,结果仍然会按照默认的顺序进行排序。这种写法通常用于一些动态生成SQL语句的场景中,可以在不知道具体列名的情况下按照顺序进行排序。
所以,ORDER BY 1=1实际上等效于没有使用ORDER BY子句,或者说是按照默认顺序进行排序。
方法2:查询是设置Sortable属性
// 示例1:
LambdaQueryWrapper<PetShop> query = Pops.<PetShop>lambdaQuery();
query.from(PetShop.MODEL_MODEL);
query.setSortable(Boolean.FALSE);
query.orderBy(true, true, PetShop::getId);
List<PetShop> petShops2 = new PetShop().queryList(query);
System.out.printf(petShops2.size() + "");
// 示例2:
List<PetShop> petShops3 = new PetShop().queryList(
Pops.<PetShop>lambdaQuery().from(PetShop.MODEL_MODEL).setSortable(Boolean.FALSE));
System.out.printf(petShops3.size() + "");
// 示例3:
IWrapper<PetShop> wrapper = Pops.<PetShop>lambdaQuery()
.from(PetShop.MODEL_MODEL).setBatchSize(-1).setSortable(Boolean.FALSE);
List<PetShop> petShops4 = new PetShop().queryList(wrapper);
System.out.printf(petShops4.size() + "");
// 示例4:
QueryWrapper<PetShop> wrapper2 = new QueryWrapper<PetShop>().from(PetShop.MODEL_MODEL).setSortable(Boolean.FALSE);
List<PetShop> petShops5 = new PetShop().queryList(wrapper2);
System.out.printf(petShops5.size() + "");
Oinone社区 作者:望闲原创文章,如若转载,请注明出处:https://doc.oinone.top/backend/11449.html
访问Oinone官网:https://www.oinone.top获取数式Oinone低代码应用平台体验