一次比对查询单表中多个字段

前端输入客户名称,在使用hql查询时,要同时比对表中,客户名称,客户简称,拼音简写,客户编码等多个字段

hql写法

String fdCustomerName=cv.poll("docMain.fdCustomerName");
if(fdCustomerName!=null){
whereBlock.append(" and ((crmCstmContact.docMain.fdCustomerName like '%"+fdCustomerName+"%')");
whereBlock.append(" or (crmCstmContact.docMain.fdNamePinyin like '%"+fdCustomerName.toUpperCase()+"%')");
whereBlock.append(" or (crmCstmContact.docMain.docNumber like '%"+fdCustomerName+"%'))");
}

控制台打印出语句为

1 = 1 and ((crmCstmContact.docMain.fdCustomerName like '%ts%') or (crmCstmContact.docMain.fdNamePinyin like '%TS%') or (crmCstmContact.docMain.docNumber like '%ts%'))

 

注意:

and优先级大于or,所以优先级相同时要使用()括起来

数据库中存储大写拼音简码时,,填写查询语句时要把字符串转换大写格式,否则不能成功匹配

 

hql语句在设置where条件时,参数为已知list

hql语句为

String where="a表.主表属性.fdId in (:list)";

hqlInfo.setParameter("list", list);
hqlInfo.setWhereBlock(where);

此list不可为list合集,如查询结果为list大合集中多个小list,请双层for循环遍历组成一个list做成参数使用,注意排除null

必须使用参数传入,不可使用拼接例如String where="a表.主表属性.fdId in ("+list+")";------此种写法将报错转换失败

 

posted on 2018-06-12 09:50  我想长点脑子  阅读(429)  评论(0编辑  收藏  举报