HQL:为多个参数添加冒号分隔的字符串以进行搜索
HQL : Adding colon seperated strings for multiple parameters to search with
我正在开发一个 Spring-MVC 项目,在该项目中我使用 Hibernate 作为 ORM 工具。该项目的特点之一是它可以通过提供国家、城市等各种参数来搜索其他用户。现在,我想通过多个国家进行搜索。出于这个原因,我向该方法发送了一个 :
分隔的国家/地区名称。
由于最初该方法仅适用于单个国家/地区,因此我修改了查询以按此顺序工作。由于查询很复杂,这是传递多个参数的正确方法吗?谢谢。
@Override
public List<Student> addHostSearchHistory(HostSearchHistory hostSearchHistory, Long hostId) {
String queryString = giveMeFormattedHostSearchString("AND",hostSearchHistory);
Query query = session.createQuery(queryString);
if (!(hostSearchHistory.getCity() == null)) {
if (!(hostSearchHistory.getCity().equals(""))) {
query.setParameter("city", "%"+hostSearchHistory.getCity().toUpperCase()+"%");
}
}
List<Student> studentList = query.list();
Query query1 = session.createQuery(giveMeFormattedHostSearchString("OR",hostSearchHistory));
if (!(hostSearchHistory.getCity() == null)) {
if (!(hostSearchHistory.getCity().equals(""))) {
query1.setParameter("city", "%"+hostSearchHistory.getCity().toUpperCase()+"%");
}
}
}
private String giveMeFormattedHostSearchString(String clause, HostSearchHistory hostSearchHistory){
StringBuilder sb = new StringBuilder();
sb.append("from Student as s where ");
if (!(hostSearchHistory.getCountry() == null)) {
if (!(hostSearchHistory.getCountry().isEmpty())) {
String[] countries = hostSearchHistory.getCountry().split(":");
sb.append("(");
for(String s : countries){
sb.append(" ").append("upper(s.studentCountry) like ").append(s);
}
sb.append(")");
}
}
if (!(hostSearchHistory.getCity() == null)) {
if (!(hostSearchHistory.getCity().isEmpty())) {
sb.append(" ").append(clause).append(" ").append("upper(s.city) like :city");
}
}
}
有没有更好更不易出错的方法来搜索多个国家.. 请注意,我只包含了 3 个参数以避免混乱。谢谢你。
你最好从dao中提取查询生成逻辑。您可以将它委托给另一个 class 或者只在该 dao 中创建一个方法。但是,当事情变得很大时,最好将其委托给另一个 class.
对了,springdata jpa已经帮我们完成了query的生成逻辑。它几乎可以满足CRUD中的所有需求。你可以看看。
(根据相关评论提取)
如果它只是一个 foo.country in (:countryList)
那么 Hibernate,或者 Spring 数据可以提供帮助。但是,对于您需要使用 LIKE
搜索国家列表的情况,您必须手动构建它。您的构造方式(将值直接嵌入结果查询中)不是可取的,因为它容易出错并且使您的代码开放以进行 SQL 注入。
正确构建查询的方法之一是使用 Criteria API。
我正在开发一个 Spring-MVC 项目,在该项目中我使用 Hibernate 作为 ORM 工具。该项目的特点之一是它可以通过提供国家、城市等各种参数来搜索其他用户。现在,我想通过多个国家进行搜索。出于这个原因,我向该方法发送了一个 :
分隔的国家/地区名称。
由于最初该方法仅适用于单个国家/地区,因此我修改了查询以按此顺序工作。由于查询很复杂,这是传递多个参数的正确方法吗?谢谢。
@Override
public List<Student> addHostSearchHistory(HostSearchHistory hostSearchHistory, Long hostId) {
String queryString = giveMeFormattedHostSearchString("AND",hostSearchHistory);
Query query = session.createQuery(queryString);
if (!(hostSearchHistory.getCity() == null)) {
if (!(hostSearchHistory.getCity().equals(""))) {
query.setParameter("city", "%"+hostSearchHistory.getCity().toUpperCase()+"%");
}
}
List<Student> studentList = query.list();
Query query1 = session.createQuery(giveMeFormattedHostSearchString("OR",hostSearchHistory));
if (!(hostSearchHistory.getCity() == null)) {
if (!(hostSearchHistory.getCity().equals(""))) {
query1.setParameter("city", "%"+hostSearchHistory.getCity().toUpperCase()+"%");
}
}
}
private String giveMeFormattedHostSearchString(String clause, HostSearchHistory hostSearchHistory){
StringBuilder sb = new StringBuilder();
sb.append("from Student as s where ");
if (!(hostSearchHistory.getCountry() == null)) {
if (!(hostSearchHistory.getCountry().isEmpty())) {
String[] countries = hostSearchHistory.getCountry().split(":");
sb.append("(");
for(String s : countries){
sb.append(" ").append("upper(s.studentCountry) like ").append(s);
}
sb.append(")");
}
}
if (!(hostSearchHistory.getCity() == null)) {
if (!(hostSearchHistory.getCity().isEmpty())) {
sb.append(" ").append(clause).append(" ").append("upper(s.city) like :city");
}
}
}
有没有更好更不易出错的方法来搜索多个国家.. 请注意,我只包含了 3 个参数以避免混乱。谢谢你。
你最好从dao中提取查询生成逻辑。您可以将它委托给另一个 class 或者只在该 dao 中创建一个方法。但是,当事情变得很大时,最好将其委托给另一个 class.
对了,springdata jpa已经帮我们完成了query的生成逻辑。它几乎可以满足CRUD中的所有需求。你可以看看。
(根据相关评论提取)
如果它只是一个 foo.country in (:countryList)
那么 Hibernate,或者 Spring 数据可以提供帮助。但是,对于您需要使用 LIKE
搜索国家列表的情况,您必须手动构建它。您的构造方式(将值直接嵌入结果查询中)不是可取的,因为它容易出错并且使您的代码开放以进行 SQL 注入。
正确构建查询的方法之一是使用 Criteria API。