常用的HQL語句<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
Java代碼 <?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" />
- <SPAN style="FONT-SIZE: small">
- 1.hql更新
- String hql = "update PhUser set realName=?";
- int row=this.getSession().createQuery(hql).setString(0, "小李想").executeUpdate();
- PhUser 類名
- 2.hql刪除
- String hql = "delete PhUser a where a.userId=2";
- int row=this.getSession().createQuery(hql).executeUpdate();
- 還有個這種的格式:
- final String hql = "delete PhRoleFunction as a where a.roleId = "
- + roleId;
- this.getHibernateTemplate().execute(new HibernateCallback() {
- public Object doInHibernate(Session session)
- throws HibernateException, SQLException {
- return session.createQuery(hql).executeUpdate();
- }
- });更新也可以寫成這樣的格式
- 3.hql單表查詢
- String hql = "from PhUser a where a.userId=" + userId;
- List list = this.getHibernateTemplate().find(hql);
- 4.hql多表查詢
- (1)String hql = "select new map(a.CUId as CUId,a.unitName as unitName,b.CUFId as CUFId,b.UFName as UFName) from PhCorrelativeUnit a,PhCorrelativeUnitFunction b where a.CUId=b.CUId";
- List list = this.getHibernateTemplate().find(hql);
- 多個表的字段放到map中,map的鍵值就是as後面的別名,如果沒有as就是字段名
- (2) String hql = "select new com.phantom.appeal.action.bean.DealPaper(a.id as id,a.billId as billId,a.state as state,a.creator as creator,a.createtime as createtime ,b.eventContent as eventContent ,c.realName as realName,b.billCode as billCode,b.citName as citName ) from PhDealBill a,PhAcceptBill b,PhUser c where a.departmentId="+ billid+ " and a.state=0 and a.billId=b.billId and a.creator =c.userId order by a.billId";
- return this.getHibernateTemplate().find(hql);
- 另外就是寫一個類,對應你要查詢的字段,這裏的類名是new com.phantom.appeal.action.bean.DealPaper,裏面對應查詢的字段名
- 5.得到記錄數
- String hql = "select count(*) from PhUser";
- List list = this.getHibernateTemplate().find(hql);
- return ((Long) list.get(0)).intValue();
- </SPAN>