• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
bobird的学习笔记
博客园    首页    新随笔    联系   管理    订阅  订阅
兼容各个数据库的sql语句 Criteria

 

 

兼容各个数据库的sql语句 Criteria

Session session = HibernateUtil.getSessionFactory().openSession();
   Criteria criteria = session.createCriteria(People.class);
  
   //List list = criteria.list();    //select * from people
   
   //criteria.add(Restrictions.gt("id",2));     //select * from people where id > 2;
  
   //criteria.add(Restrictions.between("id", 1, 5)); //select * from people where id between 1 and 5;
  
   //criteria.add(Restrictions.isNull("password"));    //select * from people where password is noll;
  
   //criteria.add(Restrictions.eq("name", "cf"));        //select * from people where name = 'cf' ;
  
   //criteria.add(Restrictions.like("name", "_f"));         //select * from people where name like '_f';
  
   //criteria.add(Restrictions.lt("id", 6));                //select * from people where id < 6;
  
   //criteria.add(Restrictions.and(Restrictions.ge("id", 2), Restrictions.le("id", 3)));
   //select * from people where id>=2 and id<=3;
  
   //criteria.add(Restrictions.or(Restrictions.eq("name", "cf"), Restrictions.gt("id",2)));
   //select * from people where id>2 or name='cf';
  
   //criteria.add(Restrictions.in("id", new Integer[]{1,2,3}));
   //select * from people where id in (1,2,3);
  
   //criteria.addOrder(Order.asc("name"));
   //select * from people order by name asc;
  
   //criteria.setFirstResult(1);
   //criteria.setMaxResults(2);
   //select * from people limit 1,2;
  
   //criteria.setProjection(Projections.count("id"));
   //select count(id) from people ;
  
   //criteria.setProjection(Projections.avg("id"));
   //select avg(id) from people;
  
   //criteria.setProjection(Projections.groupProperty("name"));
   //select name from people group by name;
  
  
   //criteria.setProjection(Projections.rowCount());
   //select count(*) from people ;
  
   //ProjectionList projectionList = Projections.projectionList();
   //projectionList.add(Projections.groupProperty("name"));
   //projectionList.add(Projections.rowCount());
   //criteria.setProjection(projectionList);
   //select name ,count(*) from people group by name;
  
   List list = criteria.list();

posted on 2014-11-04 09:14  bobird  阅读(370)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3