我有一个 Querydsl 问题。 (壮举。BooleanBuilder)
I have a Querydsl question. (Feat. BooleanBuilder)
@Test
void testDepartment() throws Exception {
String nameParam1 = "ComputerScience";
String nameParam2 = "Department of Mathematics";
String nameParam3 = "Department of Music";
String nameParam4 = "Department of Language and Literature";
String locParam = "null";
List<Department> result = seacrhDepartment(nameParam1, nameParam2, nameParam3, nameParam4, locParam);
assertThat(result.size()).isEqualTo(0);
}
private List<Department> seacrhDepartment(String nameParam1, String nameParam2, String nameParam3, String nameParam4, String locParam) {
BooleanBuilder builder = new BooleanBuilder();
if (nameParam1 != null) {
builder.and(department.name.eq(nameParam1));
}
if (nameParam2 != null) {
builder.and(department.name.eq(nameParam2));
}
if (nameParam3 != null) {
builder.and(department.name.eq(nameParam3));
}
if (nameParam4 != null) {
builder.and(department.name.eq(nameParam4));
}
if (locParam != null) {
builder.and(department.name.eq(locParam));
}
return queryFactory
.selectFrom(department)
.where(builder)
.fetch();
}
@BeforeEach
public void before() {
queryFactory = new JPAQueryFactory(em);
Department department1 = new Department("ComputerScience");
Department department2 = new Department("Department of Mathematics");
Department department3 = new Department("Department of Music");
Department department4 = new Department("Department of Language and Literature");
em.persist(department1);
em.persist(department2);
em.persist(department3);
em.persist(department4);
}
我预先输入了数据..但是为什么必须在这里传递isEqual(0)..?
既然所有的条件都设置成builder.and,设置成isEqualTo(5)不就pass了吗?
我应该用什么测试来搜索单词 com 并获得计算机科学系?
没有 Department
满足所有筛选条件。您可能想使用 builder.or
而不是 builder.and
.
@Test
void testDepartment() throws Exception {
String nameParam1 = "ComputerScience";
String nameParam2 = "Department of Mathematics";
String nameParam3 = "Department of Music";
String nameParam4 = "Department of Language and Literature";
String locParam = "null";
List<Department> result = seacrhDepartment(nameParam1, nameParam2, nameParam3, nameParam4, locParam);
assertThat(result.size()).isEqualTo(0);
}
private List<Department> seacrhDepartment(String nameParam1, String nameParam2, String nameParam3, String nameParam4, String locParam) {
BooleanBuilder builder = new BooleanBuilder();
if (nameParam1 != null) {
builder.and(department.name.eq(nameParam1));
}
if (nameParam2 != null) {
builder.and(department.name.eq(nameParam2));
}
if (nameParam3 != null) {
builder.and(department.name.eq(nameParam3));
}
if (nameParam4 != null) {
builder.and(department.name.eq(nameParam4));
}
if (locParam != null) {
builder.and(department.name.eq(locParam));
}
return queryFactory
.selectFrom(department)
.where(builder)
.fetch();
}
@BeforeEach
public void before() {
queryFactory = new JPAQueryFactory(em);
Department department1 = new Department("ComputerScience");
Department department2 = new Department("Department of Mathematics");
Department department3 = new Department("Department of Music");
Department department4 = new Department("Department of Language and Literature");
em.persist(department1);
em.persist(department2);
em.persist(department3);
em.persist(department4);
}
我预先输入了数据..但是为什么必须在这里传递isEqual(0)..? 既然所有的条件都设置成builder.and,设置成isEqualTo(5)不就pass了吗?
我应该用什么测试来搜索单词 com 并获得计算机科学系?
没有 Department
满足所有筛选条件。您可能想使用 builder.or
而不是 builder.and
.