使用HQL语句方式实现多表多条件组合模糊查询

最近在做struts2+hibernate项目整合,其中有一个两表多条件组合模糊查询功能:

  1. 将页面查询条件封装为对象,传至后台处理:
    /*查询实例类/
    package com.newer.bean;

import java.util.Date;

/**
* 模糊查询对象实体类
*
* @author Siglam Lee
*
*/
public class SearchModelBean {
// 计划名称
private String plan_Name;
// 所属任务的任务id
private int task_Id;
// 起始开始时间
private Date beginDateFrom;
// 末尾开始时间
private Date beginDateTo;
// 起始结束时间
private Date endDateFrom;
// 末尾结束时间
private Date endDateTo;
// 是否反馈
private String isFeedBack;

public SearchModelBean(String plan_Name, int task_Id, Date beginDateFrom,
        Date beginDateTo, Date endDateFrom, Date endDateTo,
        String isFeedBack) {
    super();
    this.plan_Name = plan_Name;
    this.task_Id = task_Id;
    this.beginDateFrom = beginDateFrom;
    this.beginDateTo = beginDateTo;
    this.endDateFrom = endDateFrom;
    this.endDateTo = endDateTo;
    this.isFeedBack = isFeedBack;
}

public SearchModelBean() {
}

public String getPlan_Name() {
    return plan_Name;
}

public void setPlan_Name(String plan_Name) {
    this.plan_Name = plan_Name;
}

public int getTask_Id() {
    return task_Id;
}

public void setTask_Id(int task_Id) {
    this.task_Id = task_Id;
}

public Date getBeginDateFrom() {
    return beginDateFrom;
}

public void setBeginDateFrom(Date beginDateFrom) {
    this.beginDateFrom = beginDateFrom;
}

public Date getBeginDateTo() {
    return beginDateTo;
}

public void setBeginDateTo(Date beginDateTo) {
    this.beginDateTo = beginDateTo;
}

public Date getEndDateFrom() {
    return endDateFrom;
}

public void setEndDateFrom(Date endDateFrom) {
    this.endDateFrom = endDateFrom;
}

public Date getEndDateTo() {
    return endDateTo;
}

public void setEndDateTo(Date endDateTo) {
    this.endDateTo = endDateTo;
}

public String getIsFeedBack() {
    return isFeedBack;
}

public void setIsFeedBack(String isFeedBack) {
    this.isFeedBack = isFeedBack;
}

@Override
public String toString() {
    return "SearchModelBean [plan_Name=" + plan_Name + ", task_Id="
            + task_Id + ", beginDateFrom=" + beginDateFrom
            + ", beginDateTo=" + beginDateTo + ", endDateFrom="
            + endDateFrom + ", endDateTo=" + endDateTo + ", isFeedBack="
            + isFeedBack + "]";
}

}

  1. 使用HQL语句查询指定字段,新建一个BeanDto类。
    /*dto实体类/
    package com.newer.dto;

import java.io.Serializable;
import java.util.Date;

public class PlanDto implements Serializable {

private static final long serialVersionUID = 1L;
private int planId;
private String plaName;
private Date beginDate;
private Date endDate;
private String isFeedback;
private String taskName;

public PlanDto() {
}

public PlanDto(int planId, String plaName, Date beginDate, Date endDate,
        String isFeedback, String taskName) {
    super();
    this.planId = planId;
    this.plaName = plaName;
    this.beginDate = beginDate;
    this.endDate = endDate;
    this.isFeedback = isFeedback;
    this.taskName = taskName;
}

public int getPlanId() {
    return planId;
}

public void setPlanId(int planId) {
    this.planId = planId;
}

public String getPlaName() {
    return plaName;
}

public void setPlaName(String plaName) {
    this.plaName = plaName;
}

public Date getBeginDate() {
    return beginDate;
}

public void setBeginDate(Date beginDate) {
    this.beginDate = beginDate;
}

public Date getEndDate() {
    return endDate;
}

public void setEndDate(Date endDate) {
    this.endDate = endDate;
}

public String getIsFeedback() {
    return isFeedback;
}

public void setIsFeedback(String isFeedback) {
    this.isFeedback = isFeedback;
}

public String getTaskName() {
    return taskName;
}

public void setTaskName(String taskName) {
    this.taskName = taskName;
}

}
`
3. 具体代码块如下:
/* Dao层方法:根据条件对计划进行模糊查询 /
@Override
public List searchPlan(int empId, SearchModelBean bean) {
StringBuffer hql = new StringBuffer(
“select new com.newer.dto.PlanDto”
+ “(p.planId, p.planName, p.beginDate,p.endDate,p.isFeedback,t.taskName)”
+ “from Task t,Plan p where t.taskId=p.task.taskId and t.employeeByImplementorId.employeeId=?”);
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd”);
if (bean != null) {
String plan_Name = bean.getPlan_Name();
int task_Id = bean.getTask_Id();
Date beginDateFrom = bean.getBeginDateFrom();
Date beginDateTo = bean.getEndDateTo();
Date endDateFrom = bean.getEndDateFrom();
Date endDateTo = bean.getEndDateTo();
String isFeedBack = bean.getIsFeedBack();
if (plan_Name != null && !plan_Name.equals(“”)) {
hql.append(“and p.planName like ‘%” + plan_Name + “%’”);
}

        if (task_Id > 0) {
            hql.append("and t.taskId=" + task_Id);
        }
        if (beginDateFrom != null && !beginDateFrom.equals("")) {
            String beginDateFromStr = sdf.format(beginDateFrom);
            hql.append("and p.beginDate>to_date('" + beginDateFromStr
                    + "','yyyy-mm-dd')");
        }
        // 转换时注意单引号,否则会抛出oracle ORA-01840:输入值对于日期格式不够长的异常
        if (beginDateTo != null && !beginDateTo.equals("")) {
            String beginDateToStr = sdf.format(beginDateTo);
            hql.append("and p.beginDateto_date('" + endDateFromStr
                    + "','yyyy-mm-dd')");
        }
        if (endDateTo != null && !endDateTo.equals("")) {
            String endDateToStr = sdf.format(endDateTo);
            hql.append("and p.endDate

ps:由于hibernate是ORM框架,因此需要使用面向对象的思想去思考:

如查询三张表的指定字段,如果使用传统的sql语句多表连接查询非常繁琐,使用session.createsqlquery的方式创建查询对象时也因无法指定addEntity的xxx.Class参数值实现困难,因此我们可以通过ORM框架特性,只查询一张表,通过关系映射配置,使用对象.属性的方式获取到另外两张表的值,非常方便。

你可能感兴趣的:(Hibernate框架)