Hibernate Criteria 组合查询条件
Configuration config = new Configuration();SessionFactory sessionFactory = config.configure().buildSessionFactory();Criteria criteria = session.createCriteria(MyPoJo.class);//等于条件//Restrictions.eq(String,object);criteria.add(Restrictions.eq("UserID", 1001));//其它条件以此类推 Restrictions.gt 对应SQL中的 “field > value ” 表达式 Restrictions.ge 对应SQL中的 “field >= value” 表达式 Restrictions.lt 对应SQL中的 “field < value” 表达式 Restrictions.le 对应SQL中的 “field <= value” 表达式 Restrictions.between 对应SQL中的 “between” 表达式 Restrictions.in 传入一个数组进行IN查询 Restrictions.like 进行模糊查询 Restrictions.ilike 不区分大小写的模糊查询
/** * 查询诉讼费录入的记录信息,返回的对象是自定义工具类cn.net.withub.tz.common.Pager * @param JLID * @param jiaoKuanBM * @param anJianBS * @param jiaoKuanRBM * @param yuJiaoJE * @param chuangJianRBM * @param SYS_TIME_START * @param SYS_TIME_END * @param is_Complete * @param sortName * @param sortOrder * @param pageNo * @param pageSize * @return */public Pager<TSpSsfIn> getSuSongFIn(String JLID, String jiaoKuanBM, String anJianBS,String jiaoKuanRBM, double yuJiaoJE, String chuangJianRBM,String SYS_TIME_START, String SYS_TIME_END,String yuJiaoSJ_START,String yuJiaoSJ_END,String queRenSJ_START,String queRenSJ_END, int is_Complete,String chuangJianR,String jiaoKuanR,String queRenR,String feiYongLX,int is_ZhiXing,String sortName, String sortOrder,int pageNo,int pageSize) {Pager<TSpSsfIn> pager = new Pager<TSpSsfIn>();//分页的装载工具类try {Criteria criteria = daoTool.createCriteria(TSpSsfIn.class);//创建Hibernate参数式查询 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd");//定义日期格式化工具,用于查询条件增加//判断组装查询条件if(StringHelper.isNotEmpty(JLID)){criteria.add(Restrictions.eq("JLID", JLID));}if(StringHelper.isNotEmpty(jiaoKuanBM)){criteria.add(Restrictions.eq("jiaoKuanBM", jiaoKuanBM));}if(StringHelper.isNotEmpty(feiYongLX)){criteria.add(Restrictions.eq("feiYongLX", feiYongLX));}if(StringHelper.isNotEmpty(anJianBS)){criteria.add(Restrictions.eq("anJianBS", anJianBS));}if(StringHelper.isNotEmpty(jiaoKuanRBM)){criteria.add(Restrictions.eq("jiaoKuanRBM", jiaoKuanRBM));}if(yuJiaoJE != -1){criteria.add(Restrictions.eq("yuJiaoJE", yuJiaoJE));}if(StringHelper.isNotEmpty(yuJiaoSJ_START)){criteria.add(Restrictions.ge("yuJiaoSJ", Timestamp.valueOf(yuJiaoSJ_START + " 00:00:00")));}if(StringHelper.isNotEmpty(yuJiaoSJ_END)){criteria.add(Restrictions.le("yuJiaoSJ", Timestamp.valueOf(yuJiaoSJ_END + " 23:59:59")));}if(StringHelper.isNotEmpty(SYS_TIME_START)){ criteria.add(Restrictions.ge("SYS_TIME", Timestamp.valueOf(SYS_TIME_START + " 00:00:00")));}if(StringHelper.isNotEmpty(SYS_TIME_END)){criteria.add(Restrictions.le("SYS_TIME", Timestamp.valueOf(SYS_TIME_END + " 23:59:59")));}if(StringHelper.isNotEmpty(queRenSJ_START)){criteria.add(Restrictions.ge("queRenJKRQ", Timestamp.valueOf(queRenSJ_START + " 00:00:00")));}if(StringHelper.isNotEmpty(queRenSJ_END)){criteria.add(Restrictions.le("queRenJKRQ", Timestamp.valueOf(queRenSJ_END + " 00:00:00")));}if(is_Complete != -1){criteria.add(Restrictions.eq("is_Complete", is_Complete));}if(is_ZhiXing != -1){criteria.add(Restrictions.eq("is_ZhiXing", is_ZhiXing));}if(StringHelper.isNotEmpty(chuangJianR)){criteria.createAlias("creator", "creator");//增加表关联并做查询操作(等同于在SQL中加入了两张表的直等关联,具体关联子表用什么字段是通过HRM.XML配置文件进行配置)criteria.add(Restrictions.ilike("creator.userName", chuangJianR,MatchMode.ANYWHERE));}if(StringHelper.isNotEmpty(queRenR)){criteria.createAlias("userInfo", "userInfo");//增加表关联并做查询操作criteria.add(Restrictions.ilike("userInfo.userName", queRenR,MatchMode.ANYWHERE));}if(StringHelper.isNotEmpty(jiaoKuanR)){criteria.add(Restrictions.ilike("jiaoKuanR", jiaoKuanR,MatchMode.ANYWHERE));}// 获取根据条件分页查询的总行数 int rowCount = (Integer) criteria.setProjection(Projections.rowCount()).uniqueResult(); criteria.setProjection(null); //进行排序判断方式和需要排序的字段并加入if(StringHelper.isNotEmpty(sortName) && StringHelper.isNotEmpty(sortOrder)){if(sortOrder.equals("asc")){criteria.addOrder(Order.asc(sortName));}else if(sortOrder.equals("desc")){criteria.addOrder(Order.desc(sortName));}}//设置分页信息criteria.setFirstResult((pageNo - 1) * pageSize); //从那条数据开始 criteria.setMaxResults(pageSize); //获取条数 criteria.setResultTransformer(CriteriaSpecification.ROOT_ENTITY); //只返回根对象,不加此限制结果将返回数组,包含根对象与关联对象 List<TSpSsfIn> result = criteria.list(); //获取查询结果 //装载查询结果 pager.setPageNo(pageNo); pager.setPageSize(pageSize); pager.setRowCount(rowCount); pager.setResult(result);} catch (Exception e) {log.error("[SuSongFManagerImp.getSuSongFIn(String JLID, String jiaoKuanBM, String anJianBS," +"String jiaoKuanRBM, double yuJiaoJE, String chuangJianRBM," +"String SYS_TIME_START, String SYS_TIME_END, int is_Complete," +"String sortName, String sortOrder,int pageNo,int pageSize)]"+e.getMessage());}return pager;}