JPA复杂条件查询CriteriaBuilder中的一些运算
最开始我们平常加减乘除求余的写法一般是 add(+), sub(-), mul(*), div(/) , mod(%),但是CriteriaBuilder的下的函数命名有一些些不一样,可能不太熟悉的就找不到,前面就吃了这个亏了,prod也有"乘积"的意思在里面,具体操作代码如下(参考链接)
// Create path and parameter expressions:
Expression<Integer> path = country.get("population");
Expression<Integer> param = cb.parameter(Integer.class);
// Addition (+)
Expression<Integer> sum1 = cb.sum(path, param); // expression + expression
Expression<Integer> sum2 = cb.sum(path, 1000); // expression + number
Expression<Integer> sum3 = cb.sum(1000, path); // number + expression
// Subtraction (-)
Expression<Integer> diff1 = cb.diff(path, param); // expression - expression
Expression<Integer> diff2 = cb.diff(path, 1000); // expression - number
Expression<Integer> diff3 = cb.diff(1000, path); // number - expression
// Multiplication (*)
Expression<Integer> prod1 = cb.prod(path, param); // expression * expression
Expression<Integer> prod2 = cb.prod(path, 1000); // expression * number
Expression<Integer> prod3 = cb.prod(1000, path); // number * expression
// Division (/)
Expression<Integer> quot1 = cb.quot(path, param); // expression / expression
Expression<Integer> quot2 = cb.quot(path, 1000); // expression / number
Expression<Integer> quot3 = cb.quot(1000, path); // number / expression
// Modulo (%)
Expression<Integer> mod1 = cb.mod(path, param); // expression % expression
Expression<Integer> mod2 = cb.mod(path, 1000); // expression % number
Expression<Integer> mod3 = cb.mod(1000, path); // number % expression
// Math(abs, exp, sqrt)
Expression<Integer> abs = cb.abs(param); // ABS(expression)
Expression<Integer> neg = cb.neg(path); // -expression
Expression<Integer> sqrt = cb.sqrt(cb.literal(100)); // SQRT(expression)
参考链接:https://my.oschina.net/u/935814/blog/3034696
» 本文链接:https://blog.apires.cn/archives/1355.html
» 转载请注明来源:Java地带
» 《JPA复杂条件查询CriteriaBuilder中的一些运算》
» 本文章为Java地带整理创作,欢迎转载!转载请注明本文地址,谢谢!
» 部分内容收集整理自网络,如有侵权请联系我删除!
» 订阅本站:https://blog.apires.cn/feed/