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

📄 tinfodao.java

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

import java.util.List;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Query;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import cn.hope.mana.pojo.CDept;
import cn.hope.mana.pojo.MGroup;
import cn.hope.mana.pojo.TInfo;
import cn.hope.mana.pojo.base.BaseTInfoDAO;


public class TInfoDAO extends BaseTInfoDAO {

	Logger log = Logger.getLogger(TInfoDAO.class.getName());
	/**
	 * Default constructor.  Can be used in place of getInstance()
	 */
	public TInfoDAO () {}
	
	private int count = 0;
	public int getCount() {
		return this.count;
	}
/*------------liul------------------------------------------------------------------------------*/	
	
	public List U05login(String userName,String password) throws HibernateException {
		List list = null;
		String sqlStr = "select tInfo from TInfo tInfo where tInfo.flag='0' and tInfo.TId='"+userName+"' and tInfo.TPassword='"+password+"'";
		try {
			initialize();
			list = this.getSession().find(sqlStr);
		} catch (HibernateException e) {
			log.error(e);
			e.printStackTrace();
			throw new HibernateException(e);
		} finally {
			closeCurrentThreadSessions();
		}
		return list;
	}
	
	public TInfo U05searchTid(String teacher) throws HibernateException {
		TInfo tInfo = new TInfo();
		List list = null;
		String sqlStr = "select tInfo from TInfo tInfo where tInfo.flag='0' and tInfo.TId='"+teacher+"'";
		try {
			initialize();
			list = this.getSession().find(sqlStr);
			if(list.size()>0){
				tInfo = (TInfo)list.get(0);
			}
		} catch (HibernateException e) {
			log.error(e);
			e.printStackTrace();
			throw new HibernateException(e);
		} finally {
			closeCurrentThreadSessions();
		}
		return tInfo;
	}
   
/* ------------------lizh操作开始----------------------------------------------	*/
	//这是查询教师的全部信息
	
	public List U03SearchTInfo() throws HibernateException {
		List list = null;
		String sqlStr = "select tInfo from TInfo tInfo where tInfo.flag='0'";
		try{
			initialize();
			list = this.getSession().find(sqlStr);
		} catch (HibernateException e) {
			log.error(e);
			e.printStackTrace();
			throw new HibernateException(e);
		} finally {
			closeCurrentThreadSessions();
		}
		return list;
	}
	
	//通过主键查询出一个教师
	
	public TInfo U03SearchTInfo(String key) throws HibernateException {
		TInfo tf=null;
		try{
			initialize();
			tf= this.load(key);
		} catch (HibernateException e) {
			log.error(e);
			e.printStackTrace();
			throw new HibernateException(e);
		} finally {
			closeCurrentThreadSessions();
		}
		return tf;
	}

   //对教师进行添加操作
	
	public String U03InsertTInfo(TInfo tinfo) throws HibernateException {
		String key=null;
		try{
			initialize();
			key=this.save(tinfo);
		} catch (HibernateException e) {
			log.error(e);
			e.printStackTrace();
			throw new HibernateException(e);
		} finally {
			closeCurrentThreadSessions();
		}
		return key;
	}
	
   //对教师进行修改操作
	
	public void U03UpdateTInfo(TInfo tinfo) throws HibernateException {
		try{
			initialize();
			this.update(tinfo);
		} catch (HibernateException e) {
			log.error(e);
			e.printStackTrace();
			throw new HibernateException(e);
		} finally {
			closeCurrentThreadSessions();
		}
	}
	
  //对教师进行删除操作
	
	public void U03DeleteTInfo(String key) throws HibernateException {
		try{
			initialize();
			this.delete(key);
		} catch (HibernateException e) {
			log.error(e);
			e.printStackTrace();
			throw new HibernateException(e);
		} finally {
			closeCurrentThreadSessions();
		}
	}
//*******************对教师的操作结束-----------------------------
   //这个是查询部门的全部信息
   public List U03SearchCDept() throws HibernateException {
	   	List list =null;
	   	String sqlStr ="select cdept from CDept cdept where cdept.flag='0'";
	   	try{
	   		initialize();
	   		list = this.getSession().find(sqlStr);
	   	}catch (HibernateException e) {
			log.error(e);
			e.printStackTrace();
			throw new HibernateException(e);
		}finally{
			closeCurrentThreadSessions();
		}
	   	return list;
   }
   
   //用主键查询部门
   public CDept U03SearchCDept(Integer dept) throws HibernateException {
	   	CDept dt=null;
	   	String sqlStr ="select cdept from CDept cdept where cdept.flag='0' and cdept.DId="+dept;
	   	try{
	   		initialize();
	   		dt = (CDept)this.getSession().find(sqlStr).get(0);
	   	}catch (HibernateException e) {
			log.error(e);
			e.printStackTrace();
			throw new HibernateException(e);
		}finally{
			closeCurrentThreadSessions();
		}
	   	return dt;
  }
   //删除指定的部门
   public void U03DeleteCDept(Integer key) throws HibernateException {
		try{
			initialize();
			this.delete(key);
		} catch (HibernateException e) {
			log.error(e);
			e.printStackTrace();
			throw new HibernateException(e);
		} finally {
			closeCurrentThreadSessions();
		}
	}
   
   //这个是查询用户组的全部信息
   
   public List U03SearchMGroup() throws HibernateException {
	   List list =null;
	   String sqlStr ="select g from MGroup g where g.flag='0' ";
	   try{
		   initialize();
		   list=this.getSession().find(sqlStr);
	   }catch(HibernateException e){
		   e.printStackTrace();
	   }finally{
		   closeCurrentThreadSessions();
	   }
	   return list;
  }
 //用主键查询用户组
  public MGroup U03SearchMGroup(Integer id) throws HibernateException{
		MGroup tf=null;
		String sqlStr = "select mgroup from MGroup mgroup where mgroup.flag='0' and mgroup.GId="+id;
		System.out.println(sqlStr);
		try{
			initialize();
			tf= (MGroup)this.getSession().find(sqlStr).get(0);
		} catch (HibernateException e) {
			log.error(e);
			e.printStackTrace();
			throw new HibernateException(e);
		} finally {
			closeCurrentThreadSessions();
		}
		return tf;
  }

 //*****************分页的的处理************************
	public List U03search(TInfo info, int start, int range, boolean isEq)
	throws HibernateException {
		List list = null;
		StringBuffer sqlCnt = new StringBuffer();// 取总记录数
		StringBuffer sqlStr = new StringBuffer();// 长字符串的拼合一定要用StringBuffer.append()

		sqlStr.append("select tinfo from TInfo tinfo where tinfo.flag='0' ");
		sqlCnt.append("select count(tinfo.TId) from TInfo tinfo where tinfo.flag='0' ");
		String tName=info.getTName();//获得教师姓名 
		Integer dId=info.getCDept().getDId();//获得当前教师所在的部门Id
		Integer gId=info.getMGroup().getGId();//获得当前教师所在的用户组Id
		if(isEq){
			if(StringUtils.isNotEmpty(tName)){
				sqlStr.append(" and tinfo.TName='"+tName+"'");
				sqlCnt.append(" and tinfo.TName='"+tName+"'");
			}	
			if(dId!=null && dId.intValue()>0){
				sqlStr.append(" and tinfo.CDept.DId="+dId);	
				sqlCnt.append(" and tinfo.CDept.DId="+dId);
			}
			if(gId!=null && gId.intValue()>0){
				sqlCnt.append(" and tinfo.MGroup.GId="+gId);
			}
		}
		else{
			if(StringUtils.isNotEmpty(tName)){
				sqlStr.append(" and tinfo.TName like '"+tName+"%'");
				sqlCnt.append(" and tinfo.TName like '"+tName+"%'");
			}
			if(dId!=null && dId.intValue()>0){
				sqlStr.append(" and tinfo.CDept.DId="+dId);
				sqlCnt.append(" and tinfo.CDept.DId="+dId);
			}
			if(gId!=null && gId.intValue()>0){
				sqlStr.append(" and tinfo.MGroup.GId="+gId);
				sqlCnt.append(" and tinfo.MGroup.GId="+gId);
			}
		}
		try {
			initialize();
			Query queryCnt = this.getSession().createQuery(
					sqlCnt.toString());
			Integer count = (Integer) queryCnt.uniqueResult();// 得到总记录数
			this.count = count.intValue();		
			Query query = this.getSession().createQuery(
					sqlStr.toString());
				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;//条件查询的结果 
	}
//------------------------------lizh操作结束------------------------------

}

⌨️ 快捷键说明

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