表结构如下  表名 主键 名字 年龄 手机号用户表 User id name age mobile
客户表 Cust id name age mobile  主键 user外键 cust外键
服务表 Service id userId custId //两个外键均为多对一关系
根据前台要查询的四个值做SQL拼接类似于这样子
select * from service s  
left join user user on s.userId = user.Id
left join Cust cust on s.custId = cust.Id
where 1=1if (userName != null){
user.name like '%userName%'
}
if (userAge != null){
and user.age = userAge
}
if (userMobile != null){
and user.mobile like '%userMobile%'
}
if (custName != null){
  and cust.name like '%custName%'
}if (cutsAge != null){
and cust.age = custAge
}
if (cutsMobile != null){
and cust.mobile like '%cutsMobile%'
}
我想说的是Hibernate做这种根据不定传参做动态查询的很难写请高手支招!具体代码给详细点并且写上详细注释, 谢谢您!

解决方案 »

  1.   

    我大概写了一下,ServiceDAOImpl.java如下
    import java.util.LinkedHashMap;
    import java.util.List;import org.hibernate.Query;import com.xiaoyu.DAO.ServiceDAO;
    import com.xiaoyu.beans.Service;
    import com.xiaoyu.utils.HibernateUtils;
    public class ServiceDAOImpl extends BaseDAOImpl<Service> implements ServiceDAO { @SuppressWarnings("unchecked")
    public List<Service> findAll(String hql,LinkedHashMap<String,String> linkedMap){
    Query query=HibernateUtils.getSession().createQuery("select s from Service as s join s.user as u join s.cust as c "+(hql==null || "".equals(hql.trim())? "": "where 1=1 "+ hql));
    setQueryParams(query, linkedMap);

    return query.list();
    }
    //这是处理参数的
    private static void setQueryParams(Query query, LinkedHashMap<String,String> linkedMap){
    for (String  key : linkedMap.keySet()) {
    query.setString(key, (String) linkedMap.get(key));
    }


    }
    下面是ServiceAction.java
    import java.util.LinkedHashMap;
    import java.util.List;import com.xiaoyu.DAO.ServiceDAO;
    import com.xiaoyu.DAO.impl.ServiceDAOImpl;
    import com.xiaoyu.beans.Service;public class ServiceAction {
    private String username;
    private String userage;
    private String usermobile;
    private String custname;
    private String custage;
    private String custmobile;
    ...//省去了setter 和getter方法
    public String findService(){
    StringBuffer sb=new StringBuffer();
    LinkedHashMap<String,String> l=new LinkedHashMap<String, String>(); if(!username.equals("")){
    sb.append(" and u.name like :name ");
    l.put("name", username+"%");
    }
    if(!userage.equals("")){
    sb.append("  and u.age =:age ");
    l.put("age", userage);
    }
    if(!usermobile.equals("")){
    sb.append("   and u.mobile =:mobile ");
    l.put("mobile", usermobile);
    }
    if(!custname.equals("")){
    sb.append("  and c.name like :name ");
    l.put("name", custname+"%");
    }
    if(!custage.equals("")){
    sb.append("  and c.age =:age ");
    l.put("age", custage);
    }
    if(!custmobile.equals("")){
    sb.append("  and c.mobile = :mobile ");
    l.put("mobile", custmobile);
    }
    ServiceDAO sDAO =new ServiceDAOImpl(); list=sDAO.findAll(sb.toString(), l);

    for (Service service : list) {
    System.out.println(service.getCust().getName());
    }
    return "success";
    }
    下面是两jsp页面 index.jsp
     <s:form action="find">
        <s:textfield name="username" label="用户姓名:"/><br>
        <s:textfield name="userage" label="用户年龄:"/><br>
        <s:textfield name="usermobile" label="手机号码:"/><br>
        <s:textfield name="custname" label="客户姓名:"/><br>
        <s:textfield name="custage" label="客户年龄:"/><br>
        <s:textfield name="custmobile" label="客户手机号码:"/><br>
        <s:submit value="查询"></s:submit>
        </s:form>
    list.jsp页面
    <s:iterator value="list">
         <s:property value="id"/>
         <s:property value="user.name"/>
         <s:property value="cust.name"/><br>
        </s:iterator>
        
      

  2.   

    怕 SQL注入 试试用Criteria 类吧
      

  3.   

    DetachedCriteria detachedCriteria = DetachedCriteria.forClass(TCustServiceRec.class, "rec")
              .createAlias("customer", "cust")
                   .add(Restrictions.like("cust.branch.jgbh","%"+ teamId.trim() + "%"));

    if(name != null && !"".equals(name.trim())){ 
    detachedCriteria .createAlias("serviceManager", "manager").add(
                     Restrictions.like("manager.name", "%" + name.trim() + "%")
                    );
    }List<TCustServiceRec> rec2List = detachedCriteria.getExecutableCriteria(custServiceRecManager.getCurrentManagerSession()).list();
    哎,大哥啊,我要的是类似于这种啊..不是HQL拼接,那个我也会
      

  4.   

    直接用spring的hibernateDaoSupport啊