我的查询如下:
@Query(value = "select * from participant p where case when ?1 is not null then p.BUSINESS_TYPE_ID = ?1 else p.BUSINESS_TYPE_ID is not null end "
+"and case when ?2 is not null then p.CITY_ID = ?2 else p.CITY_ID is not null end "
+"and case when ?3 is not null then p.COUNTRY_ID = ?3 else p.COUNTRY_ID is not null end "
+"and case when ?4 is not null then p.REGION_ID = ?4 else p.REGION_ID is not null end "
+"and case when ?5 is not null then p.PAYMENT_METHOD_TYPE_ID = ?5 else p.PAYMENT_METHOD_TYPE_ID is not null end "
+"and case when ?6 is not null then p.CURRENCY_ID = ?6 else p.CURRENCY_ID is not null end;", nativeQuery = true)
它在MySQL
上正常工作,但我需要创建动态,它适用于MySQL和Oracle。
可以在QueryDSL
或Spring Data JPA中使用吗?
谢谢!
答案 0 :(得分:0)
您可以使用QueryDSL(以下只是一个要点)
JPAQuery<?> query = new JPAQueryFactory(mEntityManager).query();
QParticipant participant = QParticipant.participant;
BooleanBuilder builder = new BooleanBuilder();
builder = (null != parameter1? builder.and(participant.businessTypeId.isNotNull().and(participant.businessTypeId.eq(parameter1))): builder.and(participant.businessTypeId.isNotNull()));
builder = (null != parameter2? builder.and(participant.cityId.isNotNull().and(participant.cityId.eq(parameter2))): builder.and(participant.cityId.isNotNull()));
//... (similar for countryId, regionId, paymentMethodTypeId, currencyId)
return query.select(participant).from(participant).where(builder).fetchAll();