首页 > 代码库 > 基于Struts2+Hibernate的DetachedCriteria多条件查询

基于Struts2+Hibernate的DetachedCriteria多条件查询

上一篇我们讲诉了基于SSH框架利用Criteria的多条件查询,这一篇我们就接着来看基于SSH框架利用DetachedCriteria的多条件查询。

一、Jsp表单查询页

技术分享
  1    <form action="${pageContext.request.contextPath}/CourseTypeAction_listCourse.action"     
  2      method="post">
  3    	<table width="88%" border="0" class="emp_table" style="width:80%;">
  4    	  <tr>
  5   	    <td width="10%">课程类别:</td>
  6    	    <td><input type="text" name="courseName" size="30" value="http://www.mamicode.com/" /></td>
  7    	  </tr>
  8   	  <tr>
  9   	    <td >课程简介:</td>
 10   	    <td > <input type="text" name="remark" size="30" value="http://www.mamicode.com/" /></td>
 11   	  </tr>
 12  	  <tr>
 13   	    <td >总学时:</td>
 14   	    <td ><input type="text" name="totalStart" size="12" value="http://www.mamicode.com/" />  至   
 15                 <input type="text" name="totalEnd" size="12" value="http://www.mamicode.com/" /></td>
 16  	  </tr>
 17   	  <tr>
 18   	    <td>课程费用:</td>
 19   	    <td ><input type="text" name="lessonCostStart" size="12" value="http://www.mamicode.com/" /> 至  
 20               <input type="text" name="lessonCostEnd" size="12" value="http://www.mamicode.com/" /></td>
 21   	  </tr>
 22   	</table><br> 
 23      <input type="submit"/>
 24   </form>
 25 
Jsp Code

二、javaBean(Course.java)

技术分享
  1 package com.fhcq.bean;
  2 
  3 public class Course {
  4 
  5 	String courseType;
  6 	Double courseCost;
  7 	Integer studyTime;
  8 	String remark;
  9 
 10 	public String getCourseType() {
 11 		return courseType;
 12 	}
 13 	public void setCourseType(String courseType) {
 14 		this.courseType = courseType;
 15 	}
 16 	public Double getCourseCost() {
 17 		return courseCost;
 18 	}
 19 	public void setCourseCost(Double courseCost) {
 20 		this.courseCost = courseCost;
 21 	}
 22 	public Integer getStudyTime() {
 23 		return studyTime;
 24 	}
 25 	public void setStudyTime(Integer studyTime) {
 26 		this.studyTime = studyTime;
 27 	}
 28 	public String getRemark() {
 29 		return remark;
 30 	}
 31 	public void setRemark(String remark) {
 32 		this.remark = remark;
 33 	}
 34 
 35 }
 36 
CourseBean Code

三、Course.hbm.xml

技术分享
  1 <?xml version="1.0" encoding="UTF-8"?>
  2 <!DOCTYPE hibernate-mapping PUBLIC
  3     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  4     "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
  5 <hibernate-mapping package="com.cskaoyan.course.bean">
  6 
  7     <!-- name是类名  如果有包就不用写全类名,否则要写。  table 表示对应的表名 。建议都要写  -->
  8     <class name="Course" table="course">
  9 
 10            <!--使用Id配置主键 必须配置  -->
 11            <id name="courseId" column="courseId">
 12                   <!--目前照常,主键生成策略  -->
 13                   <generator class="native"></generator>
 14            </id>
 15 
 16          <!--我们的CourseBean的每个普通字段 跟数据库表course每一列的对应  -->
 17          <property name="courseType"></property>
 18          <property name="courseCost"></property>
 19          <property name="studyTime"></property>
 20          <property name="remark"></property>
 21 
 22     </class>
 23 </hibernate-mapping>
XML Code

四、

 

CourseDaoImpl.java

技术分享
  1 @Override
  2 public List<Course> showPageCourseByCondition(String courseName, String remark,         
  3       String totalStart, String totalEnd,String lessonCostStart, String lessonCostEnd) {
  4    	 // TODO Auto-generated method stub
  5    	if(StringUtils.isBlank(courseName) && StringUtils.isBlank(remark)&&StringUtils.isBlank
  6            (totalStart) && StringUtils.isBlank(totalEnd)&&StringUtils.isBlank
  7            (lessonCostStart) && StringUtils.isBlank(lessonCostEnd)){
  8    	      return showCoursesByPage();
  9    	}else{
 10   	      DetachedCriteria forClass = DetachedCriteria.forClass(Course.class);
 11   	      if(StringUtils.isNotBlank(courseName)){
 12   	            forClass.add(Restrictions.like("courseName", courseName.replace(" ", ""), 
 13                  MatchMode.ANYWHERE));
 14   	      }
 15   	      if(StringUtils.isNotBlank(remark)){
 16     		  forClass.add(Restrictions.like("remark", remark.replace(" ", ""),
 17                 MatchMode.ANYWHERE));
 18   	      }
 19   	      if(StringUtils.isNotBlank(totalStart)){
 20   		  Integer totalStart1 =Integer.parseInt(totalStart);
 21   		  forClass.add(Restrictions.ge("total", totalStart1));
 22   	      }
 23   	      if(StringUtils.isNotBlank(totalEnd)){
 24   		  Integer totalEnd1 =Integer.parseInt(totalEnd);
 25   		  forClass.add(Restrictions.le("total", totalEnd1));
 26            }
 27   	      if(StringUtils.isNotBlank(lessonCostStart)){
 28   		  double lessonCostStart1 = Double.valueOf(lessonCostStart).doubleValue();
 29  		  forClass.add(Restrictions.ge("courseCost", lessonCostStart1));
 30   	      }
 31  	      if(StringUtils.isNotBlank(lessonCostEnd)){
 32   		  double lessonCostEnd1 = Double.valueOf(lessonCostEnd).doubleValue();
 33   		  forClass.add(Restrictions.le("courseCost", lessonCostEnd1));
 34   	      }
 35 
 36   	List<Course> findByCriteria = (List<Course>) getHibernateTemplate().findByCriteria 
 37            (forClass);
 38   	return findByCriteria;
 39   	}
 40 }
DaoImpl Code

基于Struts2+Hibernate的DetachedCriteria多条件查询