如何在maven项目中使用动态查询?



是否可以像下面的代码那样执行动态jpa或本机查询

I passed "WHERE"条件作为存储库类的字符串是正确的方法吗?

List<Doctor1> doctorsList = null;
String str1 = null;
if (date != null) {
str1 = " d.hospitalId = " +hospitalId + " AND dt.date = '" + date + "' AND d.active = 1";

if ((date != null) && (gender != null)) {
str1 = str1 + " AND d.gender = '" + gender + "'";
}
if ((date != null) && (specializationId != null)) {
str1 = str1 + " AND dsa.doctorId = d.doctorId AND dsa.active = 1";

}
else if (gender != null) {
str1 = " d.hospitalId = " +hospitalId + " AND d.gender = '" + gender + "' AND d.active = 1";

if ((gender != null) && (specializationId != null)) {
str1 = str1 + " AND dsa.doctorId = d.doctorId AND dsa.active = 1";
}.........
}
doctorsList = doctorsRepository.selectDoctorDetails(str1);
<<p>DoctorRepository类/strong>
@Query("select distinct d from Follow_My_Doct.tbl_doctors d LEFT JOIN DoctorDutyTime ddt ON ddt.doctorId = d.doctorId AND ddt.active = 1 LEFT JOIN DoctorSpecilaizationAssociation dsa ON dsa.active = 1 AND dsa.doctorId = d.doctorId :str1")
List<Doctor1> selectDoctorDetails(String str1);

@Query(value ="select distinct * from Follow_My_Doct.tbl_doctors d LEFT JOIN Follow_My_Doct.tbl_doctors_duty_time ddt ON ddt.doctorId = d.doctorId AND ddt.active = 1 LEFT JOIN Follow_My_Doct.tbl_doctor_specialization_association dsa ON dsa.active = 1 AND dsa.doctorId = d.doctorId ?1, nativeQuery = true)
List<Doctor1> selectDoctorDetails(String str1);

我不确定。这种方法可能会解决您的问题。我们用Sturts framework的时候做过这个。你可以试试-

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
...
...
@PersistenceContext
private EntityManager entityManager;
public void test() {
String queryStr = "select * from doctor where ";
Query query = entityManager.createNativeQuery(queryStr);
List<Doctor1> doctorsList = null;
String str1 = null;
if (date != null) {
str1 = " d.hospitalId = ?1 AND dt.date = ?2 AND d.active = ?3";
query.setParameter(1, hospitalId);
query.setParameter(2, date);
query.setParameter(3, 1);
//for number tracking inside if-else, use extra variable.              
if ((date != null) && (gender != null)) {
//update using similar way
str1 = str1 + " AND d.gender = '" + gender + "'";
}
if ((date != null) && (specializationId != null)) {
//
str1 = str1 + " AND dsa.doctorId = d.doctorId AND dsa.active = 1";                
}

}else if (gender != null) {
str1 = " d.hospitalId = " +hospitalId + " AND d.gender = '" + gender + "' AND d.active = 1";

if ((gender != null) && (specializationId != null)) {
str1 = str1 + " AND dsa.doctorId = d.doctorId AND dsa.active = 1";
}
}
doctorsList = doctorsRepository.selectDoctorDetails(entityManager,query, str1);
}

IndoctorRepository:

public Doctor1 selectDoctorDetails(EntityManager entityManager, Query query, String str) {

try {
return new Doctor1((Object[]) query.getSingleResult());
} catch (Exception e) {
e.printStackTrace();

}
}

相关内容

  • 没有找到相关文章

最新更新