Code Bye

关于Spring Data JPA 忽略大小写模糊查询问题

list=knResourceDao.findAll(new Specification<KnResource>(){
                @Override public Predicate toPredicate(Root<KnResource> root,CriteriaQuery<?> query,CriteriaBuilder cb){
                    Predicate predicate=cb.conjunction();
                    List<Expression<Boolean>> expressions=predicate.getExpressions();
                    expressions.add(cb.equal(root.<KnResource.ResourceType>get(“type”),KnResource.ResourceType.FUNCTION));
                    if(searchParams!=null&&searchParams.size()!=0){
                        if(searchParams.containsKey(“LIKE_active”)&&!Strings.isNullOrEmpty(searchParams.get(“LIKE_active”).toString())){
                            expressions.add(cb.equal(root.<KnRole.ActiveType>get(“active”),KnResource.ActiveType.valueOf(searchParams.get(“LIKE_active”).toString())));
                        }
                        if(searchParams.containsKey(“LIKE_name”)&&!Strings.isNullOrEmpty(searchParams.get(“LIKE_name”).toString())){
                            expressions.add(cb.like(root.<String>get(“name”),”%”+searchParams.get(“LIKE_name”)+”%”));
                        }
                        if(searchParams.containsKey(“LIKE_markName”)&&!Strings.isNullOrEmpty(searchParams.get(“LIKE_markName”).toString())){
                            expressions.add(cb.like(root.<String>get(“markName”),”%”+searchParams.get(“LIKE_markName”)+”%”));
                        }
                        if(searchParams.containsKey(“LIKE_version”)&&!Strings.isNullOrEmpty(searchParams.get(“LIKE_version”).toString())){
                            expressions.add(cb.like(root.<String>get(“version”),”%”+searchParams.get(“LIKE_version”)+”%”));
                        }
                    }
                    if(beginTime!=null){
                        expressions.add(cb.greaterThanOrEqualTo(root.<Long>get(“createTime”),beginTime));
                    }
                    if(endTime!=null){
                        expressions.add(cb.lessThanOrEqualTo(root.<Long>get(“createTime”),endTime));
                    }
                    return predicate;
                }
            },pageRequest);

代码如上,请问我怎么将以上的模糊查询变成忽略大小写模糊查询?


40分
数据库有函数,  甚至有些数据库存储都是不区分大小写的. 
已经想出一种解决办法
 if(searchParams.containsKey(“LIKE_name”)&&!Strings.isNullOrEmpty(searchParams.get(“LIKE_name”).toString())){
                            expressions.add(cb.like(cb.upper(root.<String>get(“name”)),”%”+searchParams.get(“LIKE_name”).toString().toUpperCase()+”%”));
                        }
还有更多的解决方法吗?
回复1楼,我们这个要兼容各个数据库,有些Oracle数据库特有的函数都不能用

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明关于Spring Data JPA 忽略大小写模糊查询问题