hibernate學習(查詢),hibernate學習(

來源:互聯網
上載者:User

hibernate學習(查詢),hibernate學習(

資料查詢是hibernate的一個亮點,hibernate為程式猿提供了多種的查詢方式,分為以下三種:
1.hibernate語言查詢,也就是我們今天需要說的hql查詢,這種查詢是完全物件導向的方式來查詢,將查詢語句封裝為對象來進行操作。符合物件導向的思維來維護資料庫。
2.hibernate標準化查詢:(criteria query)將查詢語句封裝成對象進行操作。
3.原聲sql查詢:直接使用標準sql語言來進行查詢。

hql查詢所有

在學習hql查詢之前,我先插入一些記錄到userinfo表中:

//解析hibernate.cfg.xml檔案Configuration cfg = new Configuration().configure();//建立SessionFactory(建立串連池)SessionFactory factory = cfg.buildSessionFactory();//建立sessionSession session = factory.openSession();Transaction transaction = session.beginTransaction();for (int i = 0; i < 80; i++) {    UserInfo info = new UserInfo();    info.setUserName("test"+i);    info.setUserPass(String.valueOf(Math.random()*1));    session.save(info);}transaction.commit();

這裡我給userinfo表中一次性插入了80條記錄。解下來使用hql查詢所有的資料:

String hql = "from UserInfo";Query query = session.createQuery(hql);List<UserInfo>lists = query.list();for (UserInfo userInfo : lists) {    System.out.println(userInfo.getUserName());}

這裡,查詢所有並沒像sql中有”select *”那樣,而是直接省略了該語句,其實hql和sql是一樣的,只不過一切都是物件導向的查詢,將需要查詢的表名換成對應的實體類名稱,將需要查詢的欄位換成對應實體類的屬性,即可。

hql條件查詢

1.查詢id>10並且id<20的記錄

String hql = "from UserInfo where userId > 10 and userId < 20 ";//或者String hql = "from UserInfo where userId between 10 and 20 ";

2.按照userId降序排列

String hql = "from UserInfo order by userId desc";

3.使用參數預留位置查詢

String hql = "from UserInfo where userId between ? and ? order by userId desc";Query query = session.createQuery(hql);query.setInteger(0, 30);query.setInteger(1,40);List<UserInfo>lists = query.list();

4.綁定有意義的參數預留位置

String hql = "from UserInfo where userId between :begin and :end order by userId desc";Query query = session.createQuery(hql);query.setInteger("begin", 20);query.setInteger("end",40);List<UserInfo>lists = query.list();

5.查詢userId值最大的記錄

String hql = "select max(userId) from UserInfo";Query query = session.createQuery(hql);List<UserInfo>lists = query.list();Object result = query.uniqueResult();System.out.println(result);

可以看到由於這裡只會查詢出一條記錄,所以使用query.uniqueResult();來查詢。
6.hql分頁查詢
查詢從第3條記錄開始,間隔5條記錄

String hql = "from UserInfo";Query query = session.createQuery(hql);query.setFirstResult(3);query.setMaxResults(5);List<UserInfo>lists = query.list();

7.只查詢某些欄位

String hql = "select new com.mydb.entity.UserInfo(userId,userPass) from UserInfo";Query query = session.createQuery(hql);List<UserInfo>lists = query.list();

這裡需要注意就是由於這裡使用了兩個參數的建構函式,因此,需要在UserInfo實體類當中添加這兩個參數的構造方法。
或者可以這樣寫:

String hql = "select userId,userPass from UserInfo";Query query = session.createQuery(hql);List lists = query.list();for (Object object : lists) {    Object[]objects = (Object[]) object;    System.out.println(objects[0]+"----++++"+objects[1]);}
sql查詢

查詢userinfo表中的所有資料

String sql = "select * from userinfo";SQLQuery sqlQuery = session.createSQLQuery(sql);sqlQuery.addEntity(UserInfo.class);List<UserInfo> lists = sqlQuery.list();for (UserInfo userInfo : lists) {    System.out.println(userInfo.getUserName());}

說明一點:sqlQuery.addEntity(UserInfo.class);是添加hibernate查詢以後從object轉換到的類型。

好了,關於hibernate的查詢,就學習到這裡。

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.