//创建 public Long createObject(Object obj) throws Exception() { return (Long)getHibernateTemplate().save(obj); } //查询一个对象 public Object getObject(String name,String pk) throws Exception() { String queryStr = "from users as u where u.id = ? and u.name = ?"; return (Object)getHibernateTemplate().find(queryStr,new String[]{pk,name}).get(0); } //修改一个对象 public void updateObject(Object obj) throws Exception() { getHibernateTemplate().saveorupdate(obj); } //修改一个对象2 public void updateObject2(Long pk,String newName) throws Exception() { Object obj = (Object)getHibernateTemplate().get(Object.class,pk) obj.setName(newName); getHibernateTemplate().saveorupdate(obj); } //查询一个对象的属性 public String getObjectName(Long pk) throws Exception { Object obj = (Object)getHibernateTemplate().get(Object.class,pk); return obj.name; } //查询此属性的对象数量 public Long countObject(String name) throws Exception { String queryStr = "select count(*) from users u where u.name = " + name; Query query = getHibernateTemplate().getSessionFactory.getCurrentSession().createQuery(queryStr); return Long.parseLong(query.uniqueResult().toString()); } //查询所有对象 public List<Object> hasObject(String name) throws Exception { String queryStr = "from users as u where u.name = ? order by u.pk desc"; List<Object> objs = getHibernateTemplate().find(queryStr,name); return objs; } //查询所有对象方式2 public List<Object> hasObject2(String name) throws Exception { String queryStr = "from users as u where u.name = ? order by u.pk desc"; Query query = getHibernateTemplate().getSessionFactory.getCurrentSession.createQuery(queryStr); return query.list(); } //查询所有对象__分页 public List<Object> hasObject(String name,int pageindex,int pagesize) throws Exception { String queryStr = "from users as u where u.name = ? order by u.pk desc"; Query query = getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(queryStr); query.setFirstResult((pageindex-1)*pagesize); query.setMaxResult(pagesize); return query.list(); } //删除一个对象 public void deleteObject(long pk) { Object obj = getHibernateTemplate().get(Object.class,pk); if(null == obj) return; getHibernateTemplate().delete(obj); } //setParameter案例 private void changeIsdelete(long msgid,int isdelete)throws Exception { String queryString = "update CtMmsMessage model set model.isdelete=?, model.updatetime=? where model.pk=?"; Query query = getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(queryString); query.setParameter(0, isdelete); query.setParameter(1, new Date()); query.setParameter(2, msgid); query.executeUpdate(); } //addScalar案例 public List<MmsDestSendResult> getMmsSendResultDetail(long msgid,int pageindex,int pagesize)throws Exception { String queryString = "select r.destphone destphone,m.crttime submittime,r.addtime sendtime,r.result result," + "(select min(w.name) from ct_mms_whitelist w where w.mobile=r.destphone and w.numspnumguid=m.spnid) destname from " + "(select t.msgid,t.destphone,t.addtime,1 result from ct_mms_a t where t.msgid=:msgid union all " + "select t.msgid,t.destphone,t.addtime,1 result from ct_mms_b t,ct_mms_statereport s where t.aimsgid=s.nmsgid(+) and s.nmsgid is null and t.msgid=:msgid union all " + "select t.msgid,t.destphone,t.addtime,0 result from ct_mms_b t,ct_mms_statereport s where t.aimsgid=s.nmsgid and s.sstat like 'DELIVRD%' and t.msgid=:msgid union all " + "select t.msgid,t.destphone,t.addtime,3 result from ct_mms_c t where t.msgid=:msgid union all " + "select t.msgid,t.destphone,t.addtime,3 result from ct_mms_b t,ct_mms_statereport s where t.aimsgid=s.nmsgid and s.sstat not like 'DELIVRD%' and s.sstat not like 'INVALID%' and t.msgid=:msgid union all " + "select t.msgid,t.destphone,t.addtime,2 result from ct_mms_b t,ct_mms_statereport s where t.aimsgid=s.nmsgid and s.sstat like 'INVALID%' and t.msgid=:msgid" + ") r,ct_mms_message m where r.msgid=m.pk order by r.result,r.destphone"; Query query = getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(queryString) .addScalar("destphone") .addScalar("destname") .addScalar("submittime") .addScalar("sendtime") .addScalar("result",Hibernate.INTEGER); query.setParameter("msgid",msgid); query.setResultTransformer(Transformers.aliasToBean(MmsDestSendResult.class)); query.setFirstResult((pageindex-1)*pagesize); //pageindex is 1-based query.setMaxResults(pagesize); return query.list(); }