标签:avg set cti sql 条件 comm 路径 tran limit
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(); }
查询优化
标签:avg set cti sql 条件 comm 路径 tran limit
原文地址:https://www.cnblogs.com/jackerzhou/p/9906718.html