欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Hibernate-04

程序员文章站 2022-06-06 10:12:31
HQL查询语法 聚合查询:count,sum ,svg,max,min 投影查询 Criteria查询语法:和HQL一样的也是面向对象的ORM查询 离线Criteria 查询优化 ......

hql查询语法

  

查询:
public class dome{ session session = hibernaeuitls.opensession(); transaction tx = session.begintransaction(); //待执行执行sql     string hql = "from user";
   string hqls = "from cn.it.user"//类路径下
   //排序查询
    string orderby = "from cn.it.user order by u_id asc";
    string orderby2 = "from cn.it.user order by u_id desc";
   //条件查询
    string wherehql = "from cn.it.user where u_id = ?";
    string wherehql = "from cn.it.user where u_id =:id";
    
//创建查询对象 query query = session.createquery(hql); list list = query.list(); system.out.println(list); //提交事务 tx.commit(); //关闭session session.close(); }
//分页
public void fun4(){
        session session = hibernateutils.opensession();
        transaction tx = session.begintransaction();
        //----------------------------------------------------
        string hql1 = " from  cn.it.user  ";//完整写法
        
        query query = session.createquery(hql1);
        
        //limit ?,?
        // (当前页数-1)*每页条数
        query.setfirstresult(4);
        query.setmaxresults(5);
        
        list list = query.list();
        
        system.out.println(list);
        //----------------------------------------------------
        tx.commit();
        session.close();
        
    }

聚合查询:count,sum ,svg,max,min

public void fun5(){
        session session = hibernateutils.opensession();
        transaction tx = session.begintransaction();
        //----------------------------------------------------
        string hql1 = " select count(*) from  cn.it.user ";
        string hql2 = " select sum(cust_id) from  cn.it.user  ";
        string hql3 = " select avg(cust_id) from  cn.it.user  ";
        string hql4 = " select max(cust_id) from  cn.it.user  ";
        string hql5 = " select min(cust_id) from  cn.it.user  ";
        
        query query = session.createquery(hql5);
        
        number number  = (number) query.uniqueresult();
        
        system.out.println(number);
        //----------------------------------------------------
        tx.commit();
        session.close();
        
    }

 

 

投影查询

public void fun6(){
        session session = hibernateutils.opensession();
        transaction tx = session.begintransaction();
        //----------------------------------------------------//
        string hql1 = " select u_name from  cn.it.user  ";
        string hql2 = " select u_name,u_id from  cn.it.user  ";
        string hql3 = " select new user(u_id,u_name) from  cn.it.user  ";
        
        query query = session.createquery(hql3);
        
        list list = query.list();
        
        system.out.println(list);
        
        //----------------------------------------------------//
        tx.commit();
        session.close();
        
    }

 

criteria查询语法:和hql一样的也是面向对象的orm查询

简单查询:
public void fun(){ session session = hibernateutils.opensession(); transaction tx = session.begintransaction(); //-------------------------------------------------- user c = session.createcritreria(user.class); list <user> list = c.list(): system.out.println(list); //-------------------------------------------------- tx.commit(); session.clone(); }
#条件查询
----------
//---------------------------------------------------------------
criteria c = session.createcriteria(user.clsss);
//添加
c.add(restrictions.eq("u_id",32));
list<user> list = c.list();
system.out.println(list);

//---------------------------------------------------------------
tx.commit();
session.close();
#分页
----------
//---------------------------------------------------------------
criteria c = session.createcriteria(user.clsss);

c.setfirstresult(1);
c.setmaxresult(4);


list<user> list = c.list();
system.out.println(list);

//---------------------------------------------------------------
tx.commit();
session.close();
#排序

//---------------------------------------------------------------
criteria c = session.createcriteria(user.clsss);

c.addorder(order.asc("u_id"));
//c.addorder(order.desc("u_id"));


list<user> list = c.list();
system.out.println(list);

//---------------------------------------------------------------
tx.commit();
session.close();
统计

//---------------------------------------------------------------
criteria c = session.createcriteria(user.clsss);

c.setprojection(projections.rowcount());


list<user> list = c.list();
system.out.println(list);

//---------------------------------------------------------------
tx.commit();
session.close();

离线criteria

public void fun(){
     detachedcriteria dc = detachedcriteria.forclass(user.class);
     dc.add(restrictions.ideq(61));
    //-------------------------------------------------------------   
     session session = hibernateutils.opensession();
     transaction tx = session.begintransaction();
     //-----------------------------------------------------------
     criteria c = dc.getexecutablecriteria(session);   
     list list = c.list();
     system.out.println(list);
 
    //-------------------------------------------------------------
     tx.commit();
     session.close();
}        

 

查询优化