在三列中搜索全名

时间:2019-06-19 11:48:55

标签: spring-boot hibernate-native-query

我要搜索“名字姓氏中间名” 名称列中的“名字”。 “姓氏”列中的“姓氏”。 Middlename列中的“ middlename”。我也只需要按姓氏或中间名单独搜索。

我试图通过这种方式找到。但这不起作用!有人可以帮忙吗?

  @Query(value = "select * from users as u\n" +
            "where :character like (concat('%',u.name),'%')  or :character like (concat('%',u.surname),'%')\n" +
            "order by registration_time desc ", nativeQuery = true)
    List<Users> findUsersByOrderByFullName(@Param("character")String character);

1 个答案:

答案 0 :(得分:0)

String list2[]=new String[3];
    list2[0]="firstname";
    list2[1]="lastname";
    list2[2]="middlename";
    int c = 0;
    String qry = "", qry2 = "";
    if(sSearch==null)
    sSearch="";

    if (sSearch.trim().equals("")){
    qry2 += "";
    }
    else{
    qry2 +=" (  a.firstname like '%"+sSearch+"%'  or a.lastname like '%"+sSearch+"%'  or a.middlename like '%"+sSearch+"%'  ) ";
    } 
    for (int i = 0; i < list.length; i++) {
    if (list[i] != null) {
    if (c == 0) {   
    qry += " where a." + list2[i] + " ='"+list[i]+"' ";
    } else {
    qry += " and a." + list2[i] + " ='"+list[i]+"' ";
    }
    c++;
    }
    }
    if(qry.equals("")){
    if(!qry2.equals("")){
    qry += " where ";
    }
    } else{
    if(!qry2.equals("")){
    qry += " and ";
    }
    }
    List recordList = sessionFactory.getCurrentSession()
    .createSQLQuery(" select  * "
    + " from users a "+qry+qry2+" ")