Hibernate 定义了 CriteriaSpecification 接口规范用来完成面向对象的条件查询,Criteria 和 DetachedCriteria 就是 CriteriaSpecification 的子接口。
在进行本篇的内容之前,我们先来大致说一说 Criteria 和 DetachedCriteria 的主要区别:
Criteria 和 DetachedCriteria 的创建的形式不一样, Criteria 是在线的,所以它是由 Hibernate Session 进行创建的;而 DetachedCriteria 是离线的,创建时无需 Session。
本篇完整实现如下:
一、导 jar 包
1 antlr-2.7.6.jar 2 asm-commons-3.3.jar 3 commons-collections-3.1.jar 4 commons-fileupload-1.3.jar 5 commons-io-2.0.1.jar 6 commons-lang3-3.1.jar 7 commons-logging-1.1.3.jar 8 dom4j-1.6.1.jar 9 freemarker-2.3.19.jar 10 hibernate3.jar 11 hibernate-jpa-2.0-api-1.0.1.Final.jar 12 javassist-3.12.0.GA.jar 13 jta-1.1.jar 14 mysql-connector-java-5.0.8-bin.jar 15 ognl-3.0.6.jar 16 slf4j-api-1.6.1.jar 17 spring-beans-4.3.8.RELEASE.jar 18 spring-context-4.3.8.RELEASE.jar 19 spring-core-4.3.8.RELEASE.jar 20 spring-expression-4.3.8.RELEASE.jar 21 spring-jdbc-4.3.8.RELEASE.jar 22 spring-orm-4.3.8.RELEASE.jar 23 spring-tx-4.3.8.RELEASE.jar 24 spring-web-4.3.8.RELEASE.jar 25 struts2-core-2.3.15.3.jar 26 struts2-spring-plugin-2.3.15.3.jar 27 xwork-core-2.3.15.3.jar
二、配置 Struts2 核心过滤器 (web.xml)
12 7 8struts2 34 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter 5 69 struts2 10/* 11
三、Jsp 表单查询页 (list.jsp)
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <%@ taglib uri="/struts-tags" prefix="s" %> 3 <% 4 String path = request.getContextPath(); 5 String basePath = request.getScheme()+"://"+request.getServerName() 6 +":"+request.getServerPort()+path+"/"; 7 %> 8 9 10 11 1291 9213 14 My JSP 'index.jsp' starting page 15 16 17 18 19 20 23 24 25 26
四、封装 UserBean (User.java)
1 package com.fhcq.bean; 2 3 import java.io.Serializable; 4 5 public class User implements Serializable { 6 7 private Integer userID; 8 private String userName; 9 private String gender; 10 private Integer age; 11 private String fileName; 12 private String remark; 13 14 public Integer getUserID() { 15 return userID; 16 } 17 public void setUserID(Integer userID) { 18 this.userID = userID; 19 } 20 public String getUserName() { 21 return userName; 22 } 23 public void setUserName(String userName) { 24 this.userName = userName; 25 } 26 public String getGender() { 27 return gender; 28 } 29 public void setGender(String gender) { 30 this.gender = gender; 31 } 32 public Integer getAge() { 33 return age; 34 } 35 public void setAge(Integer age) { 36 this.age = age; 37 } 38 public String getFileName() { 39 return fileName; 40 } 41 public void setFileName(String fileName) { 42 this.fileName = fileName; 43 } 44 public String getRemark() { 45 return remark; 46 } 47 public void setRemark(String remark) { 48 this.remark = remark; 49 } 50 51 }
五、配置数据库表生成策略 (User.hbm.xml)
1 2 56 7 8 9 10 11 2312 13 15 1614 17 18 19 20 21 22
六、设置数据库连接参数 (hibernate.cfg.xml)
1 4 56 7 8 9 3610 com.mysql.jdbc.Driver 11 12 13root 14 15123456 16 1718 jdbc:mysql://localhost:3306/usermanager 19 20 21 22true 23 24true 25 26update 27 28true 29 30 31org.hibernate.dialect.MySQLDialect 32 3334 35
七、配置 Struts (struts.xml)
1 2 5 67 8 9 10 11 12 13 19 2016 18/index.jsp 17
八、表示层
8.1、QueryUserByConditionAction
1 package com.fhcq.action; 2 3 import java.util.List; 4 5 import com.fhcq.bean.User; 6 import com.fhcq.service.IUserService; 7 import com.fhcq.service.UserServiceImpl; 8 import com.opensymphony.xwork2.ActionContext; 9 import com.opensymphony.xwork2.ActionSupport; 10 import com.opensymphony.xwork2.ModelDriven; 11 import com.opensymphony.xwork2.util.ValueStack; 12 13 public class QueryUserByConditionAction extends ActionSupport implements 14 ModelDriven{ 15 16 User user= new User(); 17 private String ageStart; 18 private String ageEnd; 19 private String isUpload; 20 IUserService service; 21 22 public String queryCourseByCondition(){ 23 24 String userName = user.getUserName(); 25 String gender = user.getGender(); 26 String remark = user.getRemark(); 27 28 List userlist = service.queryUserByCondition 29 (userName,gender,ageStart,ageEnd,isUpload,remark); 30 System.out.println("QueryUserByConditionAction queryUser() userlist= 31 "+userlist); 32 33 ActionContext context = ActionContext.getContext(); 34 ValueStack valueStack = context.getValueStack(); 35 valueStack.push(userlist); 36 37 return SUCCESS; 38 } 39 40 @Override 41 public User getModel() { 42 // TODO Auto-generated method stub 43 return user; 44 } 45 46 public User getUser() { 47 return user; 48 } 49 50 public void setUser(User user) { 51 this.user = user; 52 } 53 54 public String getIsUpload() { 55 return isUpload; 56 } 57 58 public void setIsUpload(String isUpload) { 59 this.isUpload = isUpload; 60 } 61 62 public String getAgeStart() { 63 return ageStart; 64 } 65 66 public void setAgeStart(String ageStart) { 67 this.ageStart = ageStart; 68 } 69 70 public String getAgeEnd() { 71 return ageEnd; 72 } 73 74 public void setAgeEnd(String ageEnd) { 75 this.ageEnd = ageEnd; 76 } 77 78 public IUserService getService() { 79 return service; 80 } 81 82 public void setService(IUserService service) { 83 this.service = service; 84 } 85 86 }
九、业务层
9.1、QueryUserService
1 package com.fhcq.service; 2 3 import java.util.List; 4 5 import com.fhcq.bean.User; 6 7 public interface IUserService { 8 9 ListqueryUserByCondition(String userName, String gender,String ageStart, 10 String ageEnd, String isUpload,String remark); 11 }
9.2、QueryUserServiceImpl
1 package com.fhcq.service; 2 3 import java.util.List; 4 5 import com.fhcq.bean.User; 6 import com.fhcq.dao.IUserDao; 7 import com.fhcq.dao.UserDaoImpl; 8 9 public class UserServiceImpl implements IUserService{ 10 11 IUserDao dao; 12 13 @Override 14 public ListqueryUserByCondition(String userName, String gender, 15 String ageStart, String ageEnd, String isUpload,String remark) { 16 // TODO Auto-generated method stub 17 List queryUserByCondition = dao.queryUserByCondition 18 (userName,gender,ageStart,ageEnd,isUpload,remark); 19 return queryUserByCondition; 20 } 21 22 public IUserDao getDao() { 23 return dao; 24 } 25 26 public void setDao(IUserDao dao) { 27 this.dao = dao; 28 } 29 30 }
十、数据持久化层
10.1、QueryUserDao
1 package com.fhcq.dao; 2 3 import java.util.List; 4 5 import com.fhcq.bean.User; 6 7 public interface IUserDao { 8 9 ListqueryUserByCondition(String userName, String gender, String ageStart, 10 String ageEnd, String isUpload, String remark); 11 12 }
Criteria 在线实现多条件查询:
10.2、配置 Spring (applicationContext.xml)
1 27 8 9 10 11 13 1412
10.3、HibernateUtils 方法封装
1 package com.fhcq.utils; 2 3 import org.hibernate.Session; 4 import org.hibernate.SessionFactory; 5 import org.hibernate.cfg.Configuration; 6 7 public class HibernateUtils { 8 9 static SessionFactory sessionFactory; 10 static { 11 Configuration configure = new Configuration().configure(); 12 sessionFactory = configure.buildSessionFactory(); 13 //增加 关闭虚拟机的时候自动释放 14 Runtime.getRuntime().addShutdownHook(new Thread(new Runnable(){ 15 @Override 16 public void run() { 17 // TODO Auto-generated method stub 18 System.out.println("当运行结束的时候执行!"); 19 sessionFactory.close(); 20 } 21 })); 22 } 23 24 public static Session getSession(){ 25 Session openSession = sessionFactory.openSession(); 26 return openSession; 27 } 28 29 public static Session getCurrentSession(){ 30 Session openSession = sessionFactory.getCurrentSession(); 31 return openSession; 32 } 33 }
10.4、QueryUserDaoImpl
1 package com.fhcq.dao; 2 3 import java.util.List; 4 5 import org.apache.commons.lang3.StringUtils; 6 import org.hibernate.Criteria; 7 import org.hibernate.Query; 8 import org.hibernate.Session; 9 import org.hibernate.SessionFactory; 10 import org.hibernate.cfg.Configuration; 11 import org.hibernate.criterion.Restrictions; 12 import org.springframework.orm.hibernate3.support.HibernateDaoSupport; 13 14 import com.fhcq.bean.User; 15 import com.fhcq.utils.HibernateUtils; 16 17 public class UserDaoImpl implements IUserDao{ 18 19 @Override 20 public ListqueryUserByCondition(String userName, String gender, 21 String ageStart, String ageEnd, String isUpload, String remark) { 22 // TODO Auto-generated method stub 23 24 //如果查询页所有条件都没未填选的话,点击查询按钮则查询所有用户 25 if(StringUtils.isBlank(userName) && StringUtils.isBlank(gender) && 26 StringUtils.isBlank(ageStart) && StringUtils.isBlank(ageEnd) && 27 StringUtils.isBlank(isUpload) && StringUtils.isBlank(remark)){ 28 29 Session session = HibernateUtils.getSession(); 30 session.beginTransaction(); 31 //----------------------------- 32 Query createQuery = session.createQuery("from User"); 33 List userlist = createQuery.list(); 34 //------------------------------ 35 session.getTransaction().commit(); 36 session.close(); 37 return userlist; 38 39 }else{ 40 41 Configuration configure = new Configuration().configure(); 42 SessionFactory sessionFactory = configure.buildSessionFactory(); 43 Session openSession = sessionFactory.openSession(); 44 //------------------------------ 45 46 Criteria createCriteria = openSession.createCriteria(User.class); 47 48 if(StringUtils.isNotBlank(userName)){ 49 createCriteria.add(Restrictions.like("userName", userName.replace 50 ("", ""))); 51 } 52 if(StringUtils.isNotBlank(gender)){ 53 createCriteria.add(Restrictions.eq("gender", gender)); 54 } 55 if(StringUtils.isNotBlank(ageStart)){ 56 Integer ageStart1 =Integer.parseInt(ageStart); 57 createCriteria.add(Restrictions.ge("age", ageStart1)); 58 } 59 if(StringUtils.isNotBlank(ageEnd)){ 60 Integer ageEnd1 =Integer.parseInt(ageEnd); 61 createCriteria.add(Restrictions.le("age", ageEnd1)); 62 } 63 if(StringUtils.isNotBlank(isUpload)){ 64 65 if("1".equals(isUpload)){ 66 67 createCriteria.add(Restrictions.isNotNull("fileName")); 68 69 }else{ 70 71 createCriteria.add(Restrictions.isNull("fileName")); 72 } 73 74 } 75 if(StringUtils.isNotBlank(remark)){ 76 createCriteria.add(Restrictions.like("remark", 77 remark.replace("", ""))); 78 } 79 80 List userlist = createCriteria.list(); 81 82 //------------------------------ 83 openSession.close(); 84 sessionFactory.close(); 85 return userlist; 86 87 } 88 } 89 }
DetachedCriteria 离线实现多条件查询:
10.2、配置 Spring (applicationContext.xml)
1 27 8 10 11 14 1512 1317 19 2018 21 23 2422 25 27 2826
10.3、QueryUserDaoImpl
1 package com.fhcq.dao; 2 3 import java.util.List; 4 5 import org.apache.commons.lang3.StringUtils; 6 import org.hibernate.criterion.DetachedCriteria; 7 import org.hibernate.criterion.Restrictions; 8 import org.hibernate.criterion.MatchMode; 9 import org.springframework.orm.hibernate3.support.HibernateDaoSupport; 10 11 import com.fhcq.bean.User; 12 13 public class UserDaoImpl extends HibernateDaoSupport implements IUserDao{ 14 15 ListfindByCriteria ; 16 17 @Override 18 public List queryUserByCondition(String userName, String gender, 19 String ageStart, String ageEnd,String isUpload, String remark) { 20 // TODO Auto-generated method stub 21 //如果查询页所有条件都没未填选的话,点击查询按钮则查询所有用户 22 if(StringUtils.isBlank(userName) && StringUtils.isBlank(gender) && 23 StringUtils.isBlank(ageStart) && StringUtils.isBlank(ageEnd) && 24 StringUtils.isBlank(isUpload) && StringUtils.isBlank(remark)){ 25 26 findByCriteria = (List ) getHibernateTemplate().findByCriteria 27 (DetachedCriteria.forClass(User.class)); 28 29 }else{ 30 31 DetachedCriteria forClass = DetachedCriteria.forClass(User.class); 32 if(StringUtils.isNotBlank(userName)){ 33 forClass.add(Restrictions.like("userName", userName.replace 34 ("", ""), MatchMode.ANYWHERE)); 35 } 36 if(StringUtils.isNotBlank(gender)){ 37 forClass.add(Restrictions.eq("gender", gender)); 38 } 39 if(StringUtils.isNotBlank(ageStart)){ 40 Integer ageStart1 =Integer.parseInt(ageStart); 41 forClass.add(Restrictions.ge("age", ageStart1)); 42 } 43 if(StringUtils.isNotBlank(ageEnd)){ 44 Integer ageEnd1 =Integer.parseInt(ageEnd); 45 forClass.add(Restrictions.le("age", ageEnd1)); 46 } 47 if(StringUtils.isNotBlank(isUpload)){ 48 49 if("1".equals(isUpload)){ 50 51 forClass.add(Restrictions.isNotNull("fileName")); 52 }else{ 53 54 forClass.add(Restrictions.isNull("fileName")); 55 } 56 } 57 if(StringUtils.isNotBlank(remark)){ 58 forClass.add(Restrictions.like("remark",remark.replace 59 ("", ""), MatchMode.ANYWHERE)); 60 } 61 62 findByCriteria = (List ) getHibernateTemplate().findByCriteria 63 (forClass); 64 } 65 if(findByCriteria.size()>0){ 66 return findByCriteria; 67 } 68 return null; 69 } 70 71 }