spring Data jpa 动态写sql

public static  Specification isLongTermCustomer(HardwareMonitorPO hardwareMonitorPO) {
return new Specification() {
public Predicate toPredicate(Root root, CriteriaQuery query,
CriteriaBuilder builder) {

List list = new ArrayList();  

//Predicate p = cb.and(p3,cb.or(p1,p2));

//where  p4 and (p1 and (p2 or p3))
//root.get

if(!StringUtils.isEmpty(hardwareMonitorPO.getItemCode())){
list.add(builder.and(builder.equal(root.get("itemCode"), hardwareMonitorPO.getItemCode())));
}
if(!StringUtils.isEmpty(hardwareMonitorPO.getItemName())){
list.add(builder.and(builder.equal(root.get("itemName"), hardwareMonitorPO.getItemName())));
}

Predicate[] p = new Predicate[list.size()];  

query.where(builder.and(list.toArray(p)));
return null;
}
};
}

你可能感兴趣的:(spring Data jpa 动态写sql)