zoukankan      html  css  js  c++  java
  • Hibernate 还算完整的通用DAO类

    package dao;
    import org.hibernate.Session;
    import org.hibernate.Transaction;
    import org.hibernate.Query;
    import po.HibernateSessionFactory;
    import po.StuInfo;
    import po.StuMarks;
    import java.util.*;

    public class Dao {

    public List query1(){
      Session session=HibernateSessionFactory.getSession();
      String sql="select si from StuInfo si";
      Query query=session.createQuery(sql);
      List list=query.list();
      session.close();
      return list;
    }
    //模糊查询
    public List query2(){
      Session session=HibernateSessionFactory.getSession();
      String sql="select si from  StuInfo si where si.stuAddress like '%北京%'";
      Query query=session.createQuery(sql);
      List list=query.list();
      return list;
    }
    //预处理查询
    public List query3(){
      Session session=HibernateSessionFactory.getSession();
      String sql="select si from  StuInfo si where si.stuAddress like ?";
      Query query=session.createQuery(sql);
      query.setString(0,"%北京%");
      List list=query.list();
      return list;
    }

    public List query4(){
      Session session=HibernateSessionFactory.getSession();
      String sql="select si from  StuInfo si where si.stuSeat>=:seat";
      Query query=session.createQuery(sql);
      query.setInteger("seat",2);
      List list=query.list();
      return list;
    }
    //多表查询
    public List query5(){
      Session session=HibernateSessionFactory.getSession();
      String sql="select si from  StuInfo si,StuMarks sm where sm.stuInfo=si and sm.writtenExam>60";
      Query query=session.createQuery(sql);
      
      List list=query.list();
      return list;
    }
           //分页
    public List query6(int pageNo,int pageSize){
      Session session=HibernateSessionFactory.getSession();
      String sql="select si from  StuInfo si order by si.stuSeat asc";
      Query query=session.createQuery(sql);
      int beginnumber=pageSize*(pageNo-1);
      query.setFirstResult(beginnumber);
      query.setMaxResults(pageSize);
      List list=query.list();
      return list;
    }
    //统计函数
    public int query7(){
      Session session=HibernateSessionFactory.getSession();
      String sql="select count(si) from StuInfo si";
      Query query=session.createQuery(sql);
      Integer count=(Integer)query.uniqueResult();
      
      return count.intValue();
    }
    //子查询
    public List query8(){
      Session session=HibernateSessionFactory.getSession();
      String sql="select si from StuInfo as si,StuMarks sm where sm.stuInfo=si and sm.writtenExam>(select avg(sm.writtenExam) from sm)";
                    Query query=session.createQuery(sql);
      List list=query.list();

      return list;
    }


    }
  • 相关阅读:
    day 40 MySQL之视图、触发器、事务、存储过程、函数
    js多元运算
    继承以及Super
    Git工作流指南:Gitflow工作流
    JS中的逻辑运算符&&、||,位运算符|,&
    js的prototype理解
    JS对象—数组总结(创建、属性、方法)
    React-Native中props用法详解
    AJAX中同步和异步的区别和使用场景
    web前端之性能
  • 原文地址:https://www.cnblogs.com/soundcode/p/1911920.html
Copyright © 2011-2022 走看看