单表动态
BooleanBuilder booleanBuilder = new BooleanBuilder();
String username = "%zhang%";
User.Gender gender = User.Gender.BOY;
if (StringUtils.isNotBlank(username)) {
booleanBuilder.and(user.name.like(username));
}
if (gender != null) {
List<User.Gender> genderArrayList = new ArrayList<>();
genderArrayList.add(User.Gender.BOY);
genderArrayList.add(User.Gender.GIRL);
booleanBuilder.and(user.gender.in(genderArrayList));
}
if (startDate != null) {
booleanBuilder.and(user.createdDate.loe(startDate));
}
if (endDate != null) {
booleanBuilder.and(user.createdDate.goe(endDate));
}
List<User> list2 = jpaQueryFactory.selectFrom(user)
.where(booleanBuilder).offset(0).limit(10).fetch();
List<User> fetch = jpaQueryFactory.select(user)
.where(booleanBuilder).offset(0).limit(10).fetch();
System.out.println(fetch);
System.out.println(list2);
多对多
List<Tuple> fetch = jpaQueryFactory.select(user, role).from(user)
.leftJoin(userRole).on(user.id.eq(userRole.userId))
.leftJoin(role).on(userRole.roleId.eq(role.id))
.where(role.id.intValue().eq(2))
.fetch();
for (Tuple tuple : fetch) {
System.out.println(tuple.get(user));// user对象
System.out.println(tuple.get(role));
}