Hibernate多条件查询方法收录
来源:互联网 发布:好的炒股软件 编辑:程序博客网 时间:2024/05/29 12:28
- 本文介绍了两种Hibernate多条件查询的方法。一个是通用方法,另一个则是用sql拼接,将搜索的多个条件在外部(即调用方)封装在了数组中。
1. Hibernate多条件查询通用方法
- //value[i]为第i个查询条件propertyName[i]的值 (本方法已通过测试)
- /*多条件查询,查询条件的值为空时自动除去该条件
- * rigor为true时采用精确查询
- */
- public List searchByPropertys(String model,String[]propertyName,Object[] value,int page,boolean rigor){
- StringBuffer sqlBuffer = new StringBuffer();
- String ralation=" like ";
- if(rigor){
- ralation=" = ";
- }
- sqlBuffer.append("from "+model+" as model\n");
- int len=propertyName.length;
- List list=new ArrayList();
- boolean first=true;
- for(int i=0;i< len;i++){
- if(value[i]!=null){
- if(first){
- sqlBuffer.append(" where "+ "model."+ propertyName[i] + ralation+" ?\n");
- list.add(value[i]);
- first=false;
- }else{
- sqlBuffer.append(" and "+ "model."+ propertyName[i] +ralation+ " ?\n");
- list.add(value[i]);
- }
- }
- }
- try {
- Session session=getSession();
- Query queryObject = session.createQuery(sqlBuffer.toString());
- for(int i=0;i< list.size();i++){
- if(rigor){
- queryObject.setParameter(i, list.get(i));
- }else{
- queryObject.setParameter(i, "%"+list.get(i)+"%");
- }
- }
- list=queryObject.list();
- closeSession(session);
- return list;
- } catch (RuntimeException re) {
- log.error("find by property name failed", re);
- throw re;
- }
- }
2:hibernate多条件组合查询 之 sql 拼接
这个方法与上面第一节中的相同,只不过上面的方法是将搜索的多个条件在外部(即调用方)封装在了数组中。
- public static void main(String[] args) {
- Session session = null;
- Transaction tx = null;
- List list = null;
- Criteria criteria = null;
- try {
- session = HibernateSessionFactory.getSession();
- tx = session.beginTransaction();
- DetachedCriteria detachedCriteria = DetachedCriteria
- .forClass(InfoTab.class);
- String sql=" 1=1 ";
- Integer pareaId = 0; // 父地区;
- Integer careaId = 0; // 子地区;
- Integer categoryId = 0; // 类别;
- String infoPrivider = "中介"; // 来源;
- String houseType= "地下室"; // 房屋类型;
- Integer hxBedRoom=0; // 室;
- Integer hxLivingRoom=0; // 厅;
- String hzHouseStatus="有房出租"; // 合租类型;
- String hzRequestSex="男"; // 性别要求;
- String fixUp="尚未"; // 装修程度;
- Integer lcHeightMolecuse=0; // 楼层;
- String orientation="东南"; // 朝向要求;
- Integer buildArea=2000; // 建筑面积;
- Integer useArea=80; // 使用面积;
- Integer rentalDigit=2000; // 租金/价格;
- String title= "出租"; // 标题;
- if(pareaId!=0)
- {
- sql+="pareaId=" + pareaId;
- }
- if(careaId!=0)
- {
- sql+=" and careaId=" + careaId;
- }
- if(categoryId!=0)
- {
- sql+=" and categoryId=" + categoryId;
- }
- if(!infoPrivider.equals(""))
- {
- sql+=" and infoPrivider='" + infoPrivider + "'";
- }
- if(!houseType.equals(""))
- {
- sql+=" and houseType='" + houseType +"'";
- }
- if(hxBedRoom!=0)
- {
- sql+=" and hxBedRoom=" + hxBedRoom;
- }
- if(hxLivingRoom!=0)
- {
- sql+=" and hxLivingRoom=" + hxLivingRoom;
- }
- if(!hzHouseStatus.equals(""))
- {
- sql+=" and hzHouseStatus='" + hzHouseStatus + "'";
- }
- if(!hzRequestSex.equals(""))
- {
- sql+=" and hzRequestSex='" + hzRequestSex +"'";
- }
- if(!fixUp.equals(""))
- {
- sql+=" and fixUp='" + fixUp + "'";
- }
- if(lcHeightMolecuse!=0)
- {
- sql+=" and lcHeightMolecuse=" + lcHeightMolecuse;
- }
- if(!orientation.equals(""))
- {
- sql+=" and orientation='" + orientation + "'";
- }
- if(buildArea!=0)
- {
- sql+=" and buildArea=" + buildArea;
- }
- if(useArea!=0)
- {
- sql+=" and useArea=" + useArea;
- }
- if(rentalDigit!=0)
- {
- sql+=" and rentalDigit=" + rentalDigit;
- }
- if(!title.equals(""))
- {
- sql+=" and title like '%" + title + "%'";
- }
- sql+=" order by id desc";
- System.out.println(sql);
- detachedCriteria.add(Restrictions.sqlRestriction(sql));
- criteria = detachedCriteria.getExecutableCriteria(session);
- list = criteria.list();
- for(int i=0;i< list.size();i++)
- {
- InfoTab infoTab = (InfoTab)list.get(i);
- System.out.println(infoTab.getTitle() +" "+ infoTab.getCategoryId() +" "+ infoTab.getPareaName() +" "+ infoTab.getCareaName() +" " + infoTab.getHouseType() +" " + infoTab.getInfoPrivider());
- }
- tx.commit();
- } catch (HibernateException he) {
- he.printStackTrace();
- }
- }
3:hibernate多条件组合查询 之 Map与like结合 (形成带模糊的查询)
将所有的条件都封装在Map集合中,字符串封装在like中
//根据条件查询电影信息(示例查询),(输不输入都可以查询) @SuppressWarnings("unchecked") public List<FilmInfo> queryByCondition(String filmname,int typeid,String actor, String director,double ticketprice,double pricemin,double pricemax){ Session session=HibernateSessionFactory.getSession(); List<FilmInfo> result=null; try{ Criteria criteria=session.createCriteria(FilmInfo.class); Map map=new HashMap(); if(filmname!=null){ //模糊查询 criteria.add(Restrictions.like("filmname",'%'+filmname+'%')); } if(typeid!=0){ //加到Map集合中 map.put("typeid", typeid); } if(actor!=null){ criteria.add(Restrictions.like("actor",'%'+actor+'%')); } if(director!=null){ criteria.add(Restrictions.like("director",'%'+director+'%')); } if(ticketprice!=0){ map.put("ticketprice",ticketprice); } criteria.add(Restrictions.allEq(map)); if((pricemin!=0)&&(pricemax!=0)){ criteria.add(Restrictions.between("ticketprice", pricemin, pricemax)); } result=criteria.list(); return result; }catch (HibernateException e) { e.printStackTrace(); System.out.println("查询电影信息失败!"); }finally{ session.close(); } return null; }
- Hibernate多条件查询方法收录
- hibernate 多条件查询方法 收录
- Hibernate多条件查询方法收录
- Hibernate多条件查询方法收录
- hibernate 3.0 多条件查询方法
- hibernate search -- 多条件查询
- hibernate多条件动态查询
- Hibernate多条件查询操作
- Hibernate一对多条件查询
- hibernate的gethibernatetemplate find条件查询方法
- Hibernate的多条件查询通用方法(查询条件个数不限,能进行模糊、精确2种查...
- jpa多条件查询方法
- Hibernate通用多条件不定参数查询
- hibernate 多条件与查询:Restrictions
- Hibernate多条件模糊分页查询
- hibernate的多条件动态查询
- Hibernate设置多个查询条件
- Hibernate多条件查询,HQL拼接
- SQl in() 里面字段如何是 string类型的
- c语言数据库编程--ODBC
- ORA-12519
- android 蓝牙学习笔记
- linux网络常用命令
- Hibernate多条件查询方法收录
- 常用ASCII码
- Android读写文件详解
- Linux下查看文件和文件夹大小的df和du命令
- Android上的bug定位(troubleshooting)
- 敏捷开发一千零一问系列之二:序言及解决问题的心法(无住)
- 取得手机 IP地址
- spring scope解析
- UIControl控件编程