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

📄 studentdao.java

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

import java.util.List;
import org.apache.log4j.Logger;

import net.sf.hibernate.HibernateException;
import cn.hope.front.pojo.Student;
import org.apache.commons.lang.StringUtils;
import net.sf.hibernate.Query;

import cn.hope.front.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 Student searchByKey(String SUsername) throws HibernateException {
		Student Student = null;
		String sqlStr = "select student from Student student where student.flag='0' and student.id='"
				+ SUsername + "'";
		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 void update(Student student) throws HibernateException {
		try {
			initialize();
			this.update(student);
		} catch (HibernateException e) {
			log.error(e);
			e.printStackTrace();
			throw new HibernateException(e);
		} finally {
			closeCurrentThreadSessions();
		}
	}

	public StudentDAO () {}

	public Student searchByKey(String uname) throws HibernateException {
		Student student = null;
		String sqlStr = "select student from Student student where student.flag='0' and student.SUsername='"
				+ uname + "'";
		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;
	}

	
	//--------------zhang hong tao ------------------
	public Student U01D(String name,String psw) throws HibernateException {
		List list = null;
		Student student=null;
		String sqlStr = "from Student s where s.flag='0' and s.SUsername='"+name+"' and s.SPassword='"+psw+"'";
		try {
			initialize();
			//查寻名字		
			list = this.getSession().find(sqlStr);//从数据库提取名字和密码
			System.out.println(list);
			if(list!=null){
				if(list.size()>0){	//放到实体中去		
					student =(Student) list.get(0);
		}else{
			student=null;
			}
		}
			System.out.println(list);
					return student;
			//return list;		
		} 
		catch (HibernateException e) {
		log.error(e);
		e.printStackTrace();
		throw new HibernateException(e);
		} finally {
			closeCurrentThreadSessions();
		}
	}
		public List U01D4(String name) throws HibernateException {
		List list = null;
		//Student student=null;
		String sqlStr = "from Student s where s.flag='0' and s.SUsername='"+name+"'";
		try {
			initialize();
			list = this.getSession().find(sqlStr);
			//查寻名字				
					return list;
		} 
		catch (HibernateException e) {
		log.error(e);
		e.printStackTrace();
		throw new HibernateException(e);
		} finally {
			closeCurrentThreadSessions();
		}
	}
		public String U01D3insert(Student student) throws HibernateException {
			String falg=null;
			try {
				initialize();// 初始化session
				falg=this.save(student);// 调用生成好的父类方法save(),返回被插入数据的主键
				
			} catch (HibernateException e) {
				log.error(e);
				e.printStackTrace();
				throw new HibernateException(e);
			} finally {
				closeCurrentThreadSessions();// 关闭当前session
			}
			return falg;
		}
          //-------------------------------------
	

	
	public List searchAroundC1()throws HibernateException{
		
		String sqlStr = "select student from Student student where student.flag='0'";
		List list = null;
		try {
			initialize();
			list = this.getSession().find(sqlStr);
		} 
		catch(HibernateException e){
			log.error(e);
			e.printStackTrace();
			throw new HibernateException(e);
		}
		finally{
			closeCurrentThreadSessions();
		}
		return list;
	}
	
	public List searchAroundinfo(Student student,int start, int range, boolean isEq)throws HibernateException{
		StringBuffer sqlStr = new StringBuffer();// 长字符串的拼合一定要用StringBuffer.append()
		StringBuffer sqlCnt = new StringBuffer();// 取总记录数
		StringBuffer condition = new StringBuffer();// 查询条件
		
		sqlStr.append("select student from Student student where student.flag='0' ");
		sqlCnt.append("select count(student.id) from Student student where student.flag='0' ");//计算结果总和
		List list = null;
		
		try{
			initialize();
			Query query = this.getSession().createQuery(sqlStr.toString());
			

			if(student!=null&&StringUtils.isNotEmpty(student.getSUsername())){
				condition.append("and student.SUsername='"+student.getSUsername()+"' ");
				query = this.getSession().createQuery(sqlStr.toString()+condition);
			}
			if(student!=null&&StringUtils.isNotEmpty(student.getSAandw())){
				
				condition.append("and s_aandw='"+student.getSAandw()+"' ");
				query = this.getSession().createQuery(sqlStr.toString()+condition);
			}
			if(student!=null)
			{
				if(student.getSClass()!=null&&student.getSClass().getSClassid().intValue()>0)
				{
					condition.append("and student.SClass.SClassid='"+student.getSClass().getSClassid()+"'");
					query = this.getSession().createQuery(sqlStr.toString()+condition);
				}
			}
			
			if(student != null&&StringUtils.isNotEmpty(student.getSKge())){
				if(!student.getSKge().equals("0")){
					condition.append("and s_kge='"+student.getSKge()+"' ");
					query = this.getSession().createQuery(sqlStr.toString()+condition);
				}
			}
			if(student != null){
				
				if(student.getMaxDate()!= null){
					condition.append("and s_nmd<'"+student.getMaxDate()+"' ");	
				}
				if(student.getMinDate() != null){
					condition.append("and s_nmd>'"+student.getMinDate()+"' ");	
				}
				query = this.getSession().createQuery(sqlStr.toString()+condition);
			}
			
			
			Query queryCnt = this.getSession().createQuery(
					sqlCnt.toString() + condition);// 通过hibernate的Query方法查询数据库(自己看文档)
			Integer count = (Integer) queryCnt.uniqueResult();// 得到总记录数
			this.count = count.intValue();
			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;
	}
	
	private int count = 0;
	
	public int getCount() {
		return this.count;
	}
	public Student studentInfo(String id)throws HibernateException{
		List list = null;
		StringBuffer sqlStr = new StringBuffer();
		Student student = new Student();
		sqlStr.append("select student from Student student where student.flag='0' and student.SUsername='"+id+"'");
		try{
			initialize();
			Query query = this.getSession().createQuery(sqlStr.toString());
			list = query.list();
			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;
	}

}

⌨️ 快捷键说明

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