一、多条件查询
List <possessionData>List =newArrayList<>();
QueryBuilder<possessionData,Integet> queryBuilder =possessionDao.queryBuilder();
// queryBuilder.
Where<possessionData,Integet> queryWhere = queryBuilder.where();
try{queryWhere.or(
queryWhere.eq("isCheck",isCheck),queryWhere.eq("checkNoUp",checkNoUp)
);queryWhere.and();
queryWhere.eq("desc",desc);
queryWhere.and();
queryWhere.eq("createDateYM",YM);
//将查询语句转换成原生的SQL语句打印出来
System.out.println(queryBuilder.prepareStatementString()+"00000000000000");
possessionDataList = queryWhere.query();
打印出来的SQL语句:
SELECT * FROM `possession` WHERE (((`isCheck` = 1 OR `checkNoUp` = 1 ) AND `desc` = '信息中心' ) AND `createDateYM` = '0201710' ) 00000000000000
由此可以看出使用"OR"需要注意 将其放在最前面 。
二:多表联查
public List queryForBuilderNoCheckDataInYM(String desc, String invCode, String invUserNo, String YM, int isCheck, int checkNoUp, int invStutas, int pageSize, int page){
List findNoCheckPdas = new ArrayList<>();
List resultList ;
GenericRawResults results;
StringBuffer queryBuffer =new StringBuffer("select description, tagNumber, modelNumber, cmaNumber, serialNumber, desc,localtion ,fullName,descCondition\n" + " from (\n" + " select description,tagNumber, modelNumber,cmaNumber, serialNumber, desc,localtion,fullName,descCondition\n" + " from glf where tagNumber not in (\n" + " select tagNumber from possession\n" + " )");
if (EmptyUtils.isNotEmpty(desc)){
queryBuffer.append("and desc like '%"+desc+"%'");
}
if (EmptyUtils.isNotEmpty(invCode)){
queryBuffer.append("and tagNumber like '%"+invCode+"%'");
}
if (EmptyUtils.isNotEmpty(YM)){
queryBuffer.append("and createDateYM = '"+YM+"'");
}
queryBuffer.append(")");
try {
results = glfDao.queryRaw(String.valueOf(queryBuffer));
resultList = results.getResults();
findNoCheckPdas = ListToObject(resultList);
catch (SQLException e) {
e.printStackTrace();
}
return findNoCheckPdas;
}
网友评论