package com.lh.dao;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.lh.model.PersonType;
/**
 * 管理联系人类别的DAO类
 * @author LH
 */
public class PersonTypeDao {
	private static PersonTypeDao instance=null;
	/**
	 * 静态方法,返回一个PersonTypeDao实例
	 * @return
	 */
	public static PersonTypeDao getInstance(){
		if(instance==null){
			instance = new PersonTypeDao();
		}
		return instance;
	}
	/**
	 * 根据id查询类别对象
	 * @param id 类别编号
	 * @return
	 */
	public PersonType selectPersonTypeById(String id){
		PersonType type = null;
		Session session = null;
		try{
			session = SessionFactoryProvider.getSession();
			Query query=session.createQuery("from PersonType where typeId=:id");
			query.setString("id", id);
			if(query.list()!=null&&query.list().size()>0){
				type=(PersonType)query.list().get(0);
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}finally{
			session.close();
		}
		return type;
	}
	/**
	 * 保存或更新类别对象
	 * @param type 类别对象
	 * @return 成功true,失败false
	 */
public boolean saveOrUpdateType(PersonType type){
	boolean result=false;
	Session session=null;
	Transaction trans = null;
	try{
		session=SessionFactoryProvider.getSession();//创建Session对象
		trans=session.beginTransaction();			//创建事务
		trans.begin();								//开始事务
		session.saveOrUpdate(type);					//保存或更新对象
		trans.commit();								//提交事务
		result=true;
	}catch(Exception ex){
		if(trans!=null&&trans.isActive()){
			trans.rollback();						//出现异常,事务回滚
		}
		ex.printStackTrace();
	}finally{
		session.close();
	}
	return result;
}
	/**
	 * 删除类别
	 * @param type
	 * @return
	 */
	public boolean deleteType(PersonType type){
		boolean result=false;
		Session session=null;
		Transaction trans=null;
		try{
			session=SessionFactoryProvider.getSession();
			trans=session.beginTransaction();
			trans.begin();
			session.delete(type);
			trans.commit();
			result=true;
		}catch(Exception ex){
			if(trans!=null&&trans.isActive()){
				trans.rollback();
			}
			ex.printStackTrace();
		}finally{
			session.close();
		}
		return result;
	}
	/**
	 * 查询类别信息,返回一个类别对象集合
	 * @return
	 */
	public List<PersonType> getType(){
		List<PersonType> list = null;
		Session session=null;
		try{
			session = SessionFactoryProvider.getSession();
			Query query=session.createQuery("From PersonType");
			list=query.list();
		}catch(Exception ex){
			ex.printStackTrace();
		}finally{
			session.close();
		}
		return list;
	}
	/**
	 * 查询类别信息,返回类别信息总数
	 * @return
	 */
	public int getTypeCount(){
		int count = 0;
		Session session=null;
		try{
			session = SessionFactoryProvider.getSession();
			Query query=session.createQuery("select count(id) from PersonType");
			if(query.list()!=null&&query.list().size()>0){
				count = Integer.parseInt(query.list().get(0).toString());
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}finally{
			session.close();
		}
		return count;
	}
	/**
	 * 分页查询类别信息
	 * @param first:查询的起始位置
	 * @param maxResult:每页查询的最多条数
	 * @return List:类别对象集合
	 */
	public List<PersonType> getTypePage(int first,int maxResult){
		List<PersonType> list = null;
		Session session=null;
		try{
			session = SessionFactoryProvider.getSession();
			Query query=session.createQuery("From PersonType");
			query.setFirstResult(first);
			query.setMaxResults(maxResult);
			list = query.list();
		}catch(Exception ex){
			ex.printStackTrace();
		}finally{
			session.close();
		}
		return list;
	}
}
最近下载更多
                
                过过成功尺寸     LV7
                2021年12月22日
            
            
        
                kiritoYCQ     LV5
                2020年11月28日
            
            
        
                dyihui     LV12
                2020年11月19日
            
            
        
                霖望1234     LV2
                2020年7月18日
            
            
        
                本机号码     LV2
                2020年7月6日
            
            
        
                秘制酒心巧克力     LV6
                2020年6月19日
            
            
        
                wangshangkun     LV2
                2020年5月25日
            
            
        
                木扬木     LV2
                2020年4月25日
            
            
        
                大杰打劫     LV8
                2020年3月24日
            
            
        
                gezongyuan     LV9
                2020年1月7日
            
            
        
最近浏览更多
                
                PISCESPLUS     LV5
                2024年9月3日
            
            
        
                ymrdhgv    
                2024年6月24日
            
            
                    暂无贡献等级
            
        
                晴曛     LV6
                2024年2月20日
            
            
        
                xiquyiyuan     LV10
                2024年1月12日
            
            
        
                WBelong     LV8
                2023年12月28日
            
            
        
                Henew168     LV2
                2023年12月4日
            
            
        
                李朝磊     LV18
                2023年10月23日
            
            
        
                陆程江     LV2
                2023年5月28日
            
            
        
                uni-code_0123     LV1
                2023年4月20日
            
            
        
                ssmss951     LV3
                2023年2月27日
            
            
        
                
                
                