问题描述
我正在尝试通过使用Spring Specification在where子句中创建一个复杂的条件。因此,我正在尝试构建此本机sql:
SELECT * FROM Student
WHERE grade > 10
AND age is null
OR (
( gender = 1 and age > 20 ) or (gender !=1 and age > 18)
)
SELECT * FROM Student
WHERE grade > 10
AND ( age is null
OR gender = 1
and age > 20
OR gender !=1
and age > 18 )
)
这就是我结合规格的方式:
Specification<Student> spec = new GradeSpec()
.and(new AgeNotNullSpec())
.or(new Gender1Spec().or(new Gender2Spec()))
我在做错什么,或者应该在where子句中将几个条件归为一组?
解决方法
尝试以这种方式更改语句
Specification<Student> spec = (new GradeSpec().and(new AgeNotNullSpec()))
.or(new Gender1Spec().or(new Gender2Spec()));