HQL查詢語句用法@Query
1. HQL查簡單詢語句用法Query
//如果查詢具體的一個或兩個資料(不是全部資料)就要在HQL語句中加SELECT如果是全部查詢就省略了直接FROM開頭
和SQL語句一樣
hql =" FROM Person p WHERE p.age/3 =10 "
hql =" FROM Person p WHERE( p.age>10) and(p.name =’lxh’) "
hql =" FROM Person p WHERE p.name is not null "
hql =" FROM Person p WHERE p.age between 20 and 30 "
hql =" FROM Person p WHERE p.name in (‘lch’,’ldf’) "
hql =" FROM Person p WHERE( p.age>10) and(p.name =’lxh’) "
hql =" FROM Person p WHERE p.name like ‘l%’ "
排列
hql =" FROM Person p order by p.age "
hql =" FROM Person p order by p.age desc "降序排列
hql =" FROM Person p order by p.age desc ,p.name "先按降序再按升序排列
分組
hql =" SELECT p.sex,avg(p.age) FROM Person p group by p.sex "
hql =" SELECT p.sex,avg(p.age) FROM Person p group by p.sex having avg(p.age) >20 "
更新 ,刪除
Update User set name =’lxh’ where name =’lll’
hql =" Delete Person WHERE name = ‘lxh’ "
hql =" FROM Person p WHERE p.name like ‘l%’ "
hql =" FROM Person p WHERE p.name like ‘l%’ "
分頁輸出
Java程式碼
- //分頁輸出(從輸出資料中找中間一個區間的一段輸出)
- Configuration conf = new Configuration().configure();
- SessionFactory factory = conf.buildSessionFactory();
- Session session = factory.openSession();
- Person u = null;
- //通過Query介面全部查詢
- List ll =null;
- String hql2 ="FROM Person as p";
- Query qq = session.createQuery(hql2);
- qq.setFirstResult(0);//從第一條資料開始
- qq.setMaxResults(2); //輸出兩條資料
- ll = qq.list();
- //固定模式輸出
- Iterator iter1 =ll.iterator();
- while(iter1.hasNext()){
- u = (Person)iter1.next();
- System.out.println(u.getName());
- 例子
- Session session = new Configuration().configure().buildSessionFactory().openSession();
- Criteria cri = session.createCriteria(Person.class);
- //String hql ="SELECT distinct p.sex FROM Person AS p ";
- String hql ="SELECT COUNT(p.id) FROM Person AS p ";//求記錄數
- Query q =session.createQuery(hql);
- List all = q.list();
- Iterator iter = all.iterator();
- while(iter.hasNext()){
- System.out.println(iter.next());
- Configuration conf = new Configuration().configure();
- SessionFactory factory = conf.buildSessionFactory();
- Session session = factory.openSession();
- User u = null;
- //通過Query介面查詢單個數據
- List l = null;
- String hql ="FROM User as p WHERE p.id = ?";//必須是User類名,而不是USERS表名,因為查詢的是物件,而不是表,物件就對應著表
- Query q = session.createQuery(hql);
- q.setString(0,"111");
- l = q.list();//q.list();查詢語句
- //固定模式輸出
- Iterator iter =l.iterator();
- if(iter.hasNext()){
- u = (User)iter.next();
- }
- System.out.println(u.getName());//查詢操作
- //通過Query介面全部查詢
- List ll =null;
- String hql2 ="FROM User as p";
- Query qq = session.createQuery(hql2);
- ll = qq.list();
- //固定模式輸出
- Iterator iter1 =ll.iterator();
- while(iter1.hasNext()){
- u = (User)iter1.next();
- System.out.println(u.getName());
- }
- //模糊查詢
- List lll =null;
- String hql3 = "FROM User as p WHERE p.name LIKE ?";
- Query qqq = session.createQuery(hql3);
- qqq.setString(0, "%"+"c"+"%");
- lll= qqq.list();
- //固定模式輸出
- Iterator iter2 =lll.iterator();
- if(iter2.hasNext()){
- u = (User)iter2.next();
- }
- System.out.println(u.getName());//查詢操作