bernate中的Query一些基本用法

Java代码   收藏代码
  1. /** 
  2. * 添加 
  3. */  
  4. public void save(Stu stu){  
  5.    try {  
  6.     tran=this.GetSession().beginTransaction();  
  7.     this.GetSession().save(stu);  
  8.     tran.commit();  
  9.    } catch (HibernateException e) {  
  10.     throw e;  
  11.    }finally{  
  12.     this.CloseSession();  
  13.    }  
  14. }  
  15.   
  16. /** 
  17. * 使用HQL全查询 
  18. */  
  19. public List getallbyHQL(){  
  20.    List arr=null;  
  21.    try {  
  22.     String hql="from Stu";  
  23.     Query query=this.GetSession().createQuery(hql);  
  24.     arr=query.list();  
  25.    } catch (HibernateException e) {  
  26.     throw e;  
  27.    }finally{  
  28.     this.CloseSession();  
  29.    }  
  30.    return arr;  
  31. }  
  32.    /** 
  33. * 根据主键查询 
  34. */  
  35. public Stu getbyID(int id){  
  36.    Stu stu=null;  
  37.    try {  
  38.     stu=(Stu) this.GetSession().get(Stu.class, id);  
  39.    } catch (HibernateException e) {  
  40.     throw e;  
  41.    }finally{  
  42.     this.CloseSession();  
  43.    }  
  44.    return stu;  
  45. }  
  46.   
  47. /** 
  48. * 根据对象属性查询(使用Query) 
  49. */  
  50. public List getbyPropertyQuery(String name){  
  51.    List arr=null;  
  52.    try {  
  53.     //这里不能像SQL语一样select * from Stu where SName=:name,这是不对的。  
  54. //    Query query=this.GetSession().createQuery("from Stu where SName=:name");  
  55. //    query.setString("name", name);  
  56.     //或者  
  57.     Query query=this.GetSession().createQuery("from Stu where SName=?");  
  58.     query.setString(0, name);  
  59.     arr=query.list();  
  60.    } catch (HibernateException e) {  
  61.     throw e;  
  62.    }finally{  
  63.     this.CloseSession();  
  64.    }  
  65.    return arr;  
  66. }  
  67.   
  68. /** 
  69. * 根据对象属性查询(使用Criteria) 
  70. */  
  71. public List getbyPropertyCriteria(String name){  
  72.    List arr=null;  
  73.    try {  
  74.     Criteria cri=this.GetSession().createCriteria(Stu.class);  
  75.     Criterion c1=Expression.eq("SName", name);  
  76.     cri.add(c1);  
  77.     arr=cri.list();  
  78.    } catch (HibernateException e) {  
  79.     throw e;  
  80.    }finally{  
  81.     this.CloseSession();  
  82.    }  
  83.    return arr;  
  84. }  
  85.   
  86. /** 
  87. * 查询部分属性 
  88. */  
  89. public List getProperty(){  
  90.    List arr=new ArrayList();  
  91.    try {  
  92.     String hql="select s.SName,s.SSex from Stu as s";  
  93.     Query query=this.GetSession().createQuery(hql);  
  94.     List list=query.list();  
  95.     Iterator iter=list.iterator();  
  96.     while(iter.hasNext()){  
  97.      Object[] obj=(Object[]) iter.next();  
  98.      Stu s=new Stu();  
  99.      s.setSName(obj[0].toString());  
  100.      s.setSSex(obj[1].toString());  
  101.      arr.add(s);  
  102.     }  
  103.    } catch (HibernateException e) {  
  104.     this.CloseSession();  
  105.    }  
  106.    return arr;  
  107. }  
  108. /** 
  109. * 查询一个属性 
  110. */  
  111. public List getoneProperty(){  
  112.    List arr=new ArrayList();  
  113.    try {  
  114.     String hql="select s.SName from Stu as s";  
  115.     Query query=this.GetSession().createQuery(hql);  
  116.     Iterator iter=query.iterate();  
  117.     while(iter.hasNext()){  
  118.      Object obj=(Object) iter.next();  
  119.      Stu s=new Stu();  
  120.      s.setSName(obj.toString());  
  121.      arr.add(s);  
  122.     }  
  123.    } catch (HibernateException e) {  
  124.     this.CloseSession();  
  125.    }  
  126.    return arr;  
  127. }  
  128.   
  129. /** 
  130. *查询一个对象一个属性值 
  131. */  
  132. public Object getonlyProprotyValue(int s_id){  
  133.    Object obj=null;  
  134.    try {  
  135.     String hql="select s.SName from Stu as s where s.SId=?";  
  136.     Query query=this.GetSession().createQuery(hql);  
  137.     query.setInteger(0, s_id);  
  138.     obj=query.uniqueResult();  
  139.    } catch (HibernateException e) {  
  140.     throw e;  
  141.    }finally{  
  142.     this.CloseSession();  
  143.    }  
  144.    return obj;  
  145. }  
  146. /** 
  147. * SQL查询 
  148. */  
  149. public List getallBYSQL(){  
  150.    List arr=null;  
  151.    try {  
  152.     String sql="select {c.*} from stu as c";  
  153.     SQLQuery sqlquery=this.GetSession().createSQLQuery(sql);  
  154.     sqlquery.addEntity("c",Stu.class);  
  155.     arr=sqlquery.list();  
  156.    } catch (HibernateException e) {  
  157.     throw e;  
  158.    }finally{  
  159.     this.CloseSession();  
  160.    }  
  161.    return arr;  
  162. }  
  163.   
  164. /** 
  165. * 根据对象查询 
  166. */  
  167. public List getallByObject(Stu s){  
  168.    List arr=null;  
  169.    try {  
  170.     String hql="from Stu as s where s=:stuentity";  
  171.     //或者  
  172.     //String hql="from Stu as s where s.SId=:stuentity";  
  173.     Query query=this.GetSession().createQuery(hql);  
  174.     query.setEntity("stuentity", s);  
  175.     arr=query.list();  
  176.    } catch (HibernateException e) {  
  177.     throw e;  
  178.    }finally{  
  179.     this.CloseSession();  
  180.    }    
  181.    return arr;  
  182. }  
  183.   
  184. /** 
  185. * 模糊查询 
  186. */  
  187. public List getallQueryLike(String name){  
  188.    List arr=null;  
  189.    try {  
  190.     String hql="from Stu as s where s.SName like :name";  
  191.     Query query=this.GetSession().createQuery(hql);  
  192.     query.setString("name""%"+name+"%");  
  193.     //不能  
  194.     //query.setString("name", "'%"+name+"%'");  
  195.     arr=query.list();  
  196.    } catch (HibernateException e) {  
  197.     throw e;  
  198.    }finally{  
  199.     this.CloseSession();  
  200.    }  
  201.    return arr;  
  202. }  
  203.    /** 
  204. * 统计函数 
  205. */  
  206. public int CountStu(){  
  207.    int count=0;  
  208.    try {  
  209.     String hql="select count(*) from Stu";  
  210.     Query query=this.GetSession().createQuery(hql);  
  211.     count=(Integer) query.uniqueResult();  
  212.    } catch (HibernateException e) {  
  213.     throw e;  
  214.    }finally{  
  215.     this.CloseSession();  
  216.    }  
  217.    return count;  
  218. }  
  219.   
  220.    /** 
  221. * 条件统计 
  222. */  
  223. public int CountByWhere(String sex){  
  224.    int count=0;  
  225.    try {  
  226.     Query query=this.GetSession().createQuery("select count(*) from Stu where SSex=:sex");  
  227.     query.setString("sex", sex);  
  228.     count=(Integer)query.uniqueResult();  
  229.    } catch (HibernateException e) {  
  230.     throw e;  
  231.    }finally{  
  232.     this.CloseSession();  
  233.    }  
  234.    return count;  
  235. }  
  236.   
  237. /** 
  238. * 统计平均值 
  239. */  
  240. public float VagAge(){  
  241.    float vag=0;  
  242.    try {  
  243.     Query query=this.GetSession().createQuery("select avg(SAge) from Stu");  
  244.     vag=(Float)query.uniqueResult();  
  245.    } catch (HibernateException e) {  
  246.     throw e;  
  247.    }finally{  
  248.     this.CloseSession();  
  249.    }  
  250.    return vag;  
  251. }  
  252.   
  253. /** 
  254. * 求和函数 
  255. */  
  256. public int sumage(){  
  257.    int sum=0;  
  258.    try {  
  259.     Query query=this.GetSession().createQuery("select sum(SAge) from Stu");  
  260.     sum=(Integer)query.uniqueResult();  
  261.    } catch (HibernateException e) {  
  262.     throw e;  
  263.    }finally{  
  264.     this.CloseSession();  
  265.    }  
  266.    return sum;  
  267. }  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值