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

📄 dboperationparent.java

📁 彩铃小项目的原代码 下载
💻 JAVA
字号:
package hibernate.iml;

import java.util.Collection;
import java.util.List;

import common.HibernateSessionFactory;

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

public abstract class DBOperationParent {
	protected Session session;
	private int pageSize;		//页面大小 
	private int totalRec;		//总记录数
	private int pageCount;		//页面大小
	private String pageStr;		//分页链接
	
	private static String fontHead = "<font color='#990000'><b>";
	private static String fontTail = "</b></font>";
	
	/**
	 * 构造函数
	 */
	public DBOperationParent() {
		try {
			pageSize = 25;
			pageStr = "";
			session = HibernateSessionFactory.currentSession();
		} catch (HibernateException e) {
			e.printStackTrace();
		}		
	}
	
	/**
	 * 添加记录
	 * @param object
	 * @throws HibernateException 
	 */
	public void insert(Object object) throws HibernateException{
		try {
			if(!session.isOpen()) session = HibernateSessionFactory.currentSession();
			Transaction tx = session.beginTransaction();
			session.save(object);
			tx.commit();
			HibernateSessionFactory.closeSession();
		} catch (HibernateException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 更新记录
	 * @param personinfo
	 * @param key
	 * @return 是否更新成功
	 */
	abstract public boolean update(Object objectTmp,String key) throws HibernateException ;

	/**
	 * method 删除指定记录
	 * @param objectClass
	 * @param key
	 * @throws HibernateException 
	 */
	public void delete(Class objectClass , String key) throws HibernateException{
		try {
			if(!session.isOpen()) session = HibernateSessionFactory.currentSession();
			Transaction tx = session.beginTransaction();
			session.delete(session.load(objectClass,key));
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			HibernateSessionFactory.closeSession();
		}
	}
	
	/**
	 * 得到单个记录值
	 * @param objectClass
	 * @param key
	 * @return Personinfo
	 * @throws HibernateException 
	 */	
	public Object getInfo(Class objectClass ,String key) throws HibernateException{
		Object object = null;
		try {
			if(!session.isOpen()) session = HibernateSessionFactory.currentSession();
			object = session.load(objectClass,key);
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			HibernateSessionFactory.closeSession();
		}
		return object;
	}
	
	/**
	 * 依据分页信息得到所有记录值
	 * @param sql
	 * @param iPage
	 * @return Collection
	 * @throws HibernateException 
	 */	
	public Collection getAll(String sql , int iPage , String toURL) throws HibernateException{
		List lists = null;
		try {
			if(!session.isOpen()) session = HibernateSessionFactory.currentSession();
			String sqlT = sql;
			if(sqlT.indexOf("order by")>0) sqlT = sqlT.substring(0,sqlT.indexOf("order by"));
			
			totalRec = ((Integer)session.iterate("select count(*)"+sqlT).next()).intValue();
			//把其它值都计算出来
			pageCount = totalRec/pageSize + ((totalRec%pageSize)>0?1:0);
			Query query = session.createQuery(sql);
			int limit = 0;
			if(iPage>0) limit = pageSize*(iPage-1);
			if(iPage>pageCount) limit = pageSize*(pageCount-1);
			setPageStr(toURL,iPage);
			
			query.setFirstResult(limit);
			query.setMaxResults(pageSize);
			lists = query.list();
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			HibernateSessionFactory.closeSession();
		}
		return lists;
	}
	
	/**
	 * 得到所有记录值
	 * @param sql
	 * @param iPage
	 * @return Collection
	 * @throws HibernateException 
	 */	
	public Collection getAll(String sql) throws HibernateException{
		List lists = null;
		try {
			if(!session.isOpen()) session = HibernateSessionFactory.currentSession();
			
			Query query = session.createQuery(sql);
			lists = query.list();
		} catch (HibernateException e) {
			e.printStackTrace();
		}  finally {
			HibernateSessionFactory.closeSession();
		}
		return lists;
	}

	public int getPageCount() {
		return pageCount;
	}

	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		if(pageSize<1) this.pageSize = 20;
		else this.pageSize = pageSize;
	}

	public int getTotalRec() {
		return totalRec;
	}

	public void setTotalRec(int totalRec) {
		this.totalRec = totalRec;
	}

	public String getPageStr() {
		return pageStr;
	}
	
	/**
	 * 设置多页链接
	 * @param link
	 * @param iPage
	 */
	public void setPageStr(String link , int iPage) {
		if(iPage>pageCount) iPage=pageCount;
		pageStr = "共有记录"+getColorStr(totalRec)+"条";
		pageStr += " 页次:"+getColorStr(iPage)+"/<b>"+pageCount+"</b>页 "+getColorStr(pageSize)+"/页";
		int iStart = iPage - 5,iEnd;		
		iStart = (iStart<1)?1:iStart;
		iEnd = iStart + 9;
		iEnd = (iEnd>pageCount)?pageCount:iEnd;
		iStart = iEnd - 9;
		iStart = (iStart<1)?1:iStart;
		if((iEnd>0)&&(iPage>1)) pageStr += " <a href='"+link + 1 + "' title='首页'><font face='Webdings'>9</font></a>";
		for(;iStart<=iEnd;iStart++){
			pageStr += " <a href='"+link + iStart + "' title='转到第" + iStart + "页'>[" + iStart + "]</a>";
		}
		if((iEnd>0)&&(iPage<iEnd)) pageStr += " <a href='"+link + iEnd + "' title='尾页'><font face='Webdings'>:</font></a>";
	}
	
	/**
	 * 返回带颜色的文字
	 * @param inVal
	 * @return
	 */
	private String getColorStr(int inVal){
		return fontHead+inVal+fontTail;
	}
}

⌨️ 快捷键说明

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