⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 studentdao.java

📁 持久层hibernate技术使用的一个例子
💻 JAVA
字号:
package cn.hope.mana.pojo.dao;
import java.util.List;

import org.apache.log4j.Logger;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Query;
import cn.hope.mana.pojo.SClass;
import cn.hope.mana.pojo.Student;
import cn.hope.mana.pojo.base.BaseStudentDAO;


public class StudentDAO extends BaseStudentDAO {
    Logger log = Logger.getLogger(StudentDAO.class.getName());
	/**
	 * Default constructor.  Can be used in place of getInstance()
	 */
	public StudentDAO () {}
    private int count = 0;
    
    public int getCount(){
    	return this.count;
    }
   //添加学生信息
    public String u04insert(Student student) throws HibernateException{
    	try{
    		initialize();//初始化session
    		return this.save(student);//调用父类save()方法,返回被插入数据的主键
          }catch(HibernateException e){
        	  log.error(e);
        	  e.printStackTrace();
        	  throw new HibernateException(e);
          }finally{
        	  closeCurrentThreadSessions();
          }
    }
    //修改学生信息
    public void u04modify(Student student) throws HibernateException{
    	try{
    		initialize();//初始化session
    		this.update(student);//调用父类的update()方法
    	}catch(HibernateException e){
    		log.error(e);
    		e.printStackTrace();
    		throw new HibernateException(e);
    	}finally{
    		closeCurrentThreadSessions();
    	}
    }
    
   
 //查询所有学生
    public List u04searchAll(Student student, int start, int range)throws HibernateException{
    	List list = null;
    	StringBuffer  sqlCnt = new StringBuffer();//取得总记录数
    	StringBuffer  sqlStr = new StringBuffer();//长字符串的拼合一定要用StringBuffer.append()
    	StringBuffer condition = new StringBuffer();//查询条件
    	sqlCnt.append("select count(student.id) from Student student where student.flag = '0'");//id:实体主键的映射
    	sqlStr.append("select student from Student student where student.flag='0'");
    	try{
    		initialize();
    		Query queryCnt = this.getSession().createQuery(
    				sqlCnt.toString() + condition);
    		Integer count = (Integer)queryCnt.uniqueResult();//// 得到总记录数
    		this.count = count.intValue();
    		
    		condition.append(" order by student.SUsername desc");
    		Query query = this.getSession().createQuery(
    				sqlStr.toString() + condition);
    		query.setFirstResult(start);
    		query.setMaxResults(range);
    		list = query.list();
    	}catch(HibernateException e){
    		log.error(e);
    		e.printStackTrace();
    		throw new HibernateException(e);
    	}finally{
    		closeCurrentThreadSessions();
    	}
    	return list;
    	}
   //按id查询 
    public Student u04searchByKey(String id,boolean isEq) throws HibernateException {
		Student student = null;
		String sqlStr;
		 sqlStr = "select student from Student student where student.flag='0' and student.id= '"
				+ id + "'";
		 if(isEq){
			 sqlStr = "select student from Student student where student.flag='0' and student.id like %'"
					+ id + "%'";
			}
		try {
			initialize();
			
			List list = this.getSession().find(sqlStr);
			if (list.size() > 0) {
				student = (Student) list.get(0);
			}
		} catch (HibernateException e) {
			log.error(e);
			e.printStackTrace();
			throw new HibernateException(e);
		} finally {
			closeCurrentThreadSessions();
		}
		return student;
	}
    
    //查询班级
    public List u04ClassSearchAll(SClass sClass)throws HibernateException{
    	List list = null;
    	StringBuffer  sqlStr = new StringBuffer();//长字符串的拼合一定要用StringBuffer.append()
    	
       	sqlStr.append("select sClass from SClass sClass where sClass.flag='0'");
    	try{
    		initialize();   		    		
    	    		Query query = this.getSession().createQuery(
    				sqlStr.toString());
    		list = query.list();
    	}catch(HibernateException e){
    		log.error(e);
    		e.printStackTrace();
    		throw new HibernateException(e);
    	}finally{
    		closeCurrentThreadSessions();
    	}
    	return list;
    	}
    public List u04seaClass(int classid, int start, int range)throws HibernateException{
    	List list = null;
    	StringBuffer  sqlCnt = new StringBuffer();//取得总记录数
    	StringBuffer  sqlStr = new StringBuffer();//长字符串的拼合一定要用StringBuffer.append()
    	StringBuffer condition = new StringBuffer();//查询条件
    	
    	sqlCnt.append("select count(student.id) from Student student where student.flag = '0' and student.SClass='"+classid+"'");//id:实体主键的映射
    	sqlStr.append("select student from Student student where student.flag='0' and student.SClass='"+classid+"'");
    	try{
    		initialize();
    		Query queryCnt = this.getSession().createQuery(
    				sqlCnt.toString() + condition);
    		Integer count = (Integer)queryCnt.uniqueResult();//// 得到总记录数
    		this.count = count.intValue();
    		
    		condition.append(" order by student.SUsername desc");
    		Query query = this.getSession().createQuery(
    				sqlStr.toString() + condition);
    		query.setFirstResult(start);
    		query.setMaxResults(range);
    		list = query.list();
    	}catch(HibernateException e){
    		log.error(e);
    		e.printStackTrace();
    		throw new HibernateException(e);
    	}finally{
    		closeCurrentThreadSessions();
    	}
    	return list;
    	}
    public List u04seaName(String nameText, int start, int range,boolean isEq)throws HibernateException{
    	List list = null;
    	StringBuffer  sqlCnt = new StringBuffer();//取得总记录数
    	StringBuffer  sqlStr = new StringBuffer();//长字符串的拼合一定要用StringBuffer.append()
    	StringBuffer condition = new StringBuffer();//查询条件
        if(isEq){
    	sqlCnt.append("select count(student.id) from Student student where student.flag = '0' and student.SName='"+nameText+"'");//id:实体主键的映射
    	sqlStr.append("select student from Student student where student.flag='0' and student.SName='"+nameText+"'");
        }
        else{
        	sqlCnt.append("select count(student.id) from Student student where student.flag = '0' and student.SName like'"+nameText+"%'");//id:实体主键的映射
        	sqlStr.append("select student from Student student where student.flag='0' and student.SName like'"+nameText+"%'");
        }
    
       	try{
    		initialize();
    		Query queryCnt = this.getSession().createQuery(
    				sqlCnt.toString() + condition);
    		Integer count = (Integer)queryCnt.uniqueResult();//// 得到总记录数
    		this.count = count.intValue();
    		
    		condition.append(" order by student.SUsername desc");
    		Query query = this.getSession().createQuery(
    				sqlStr.toString() + condition);
    		query.setFirstResult(start);
    		query.setMaxResults(range);
    		list = query.list();
    	}catch(HibernateException e){
    		log.error(e);
    		e.printStackTrace();
    		throw new HibernateException(e);
    	}finally{
    		closeCurrentThreadSessions();
    	}
    	return list;
    	}
}
	
	
	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -