//基于HibernateUtil类
public abstract class HibernateBaseDao {
/*protected Session session=null;
protected final Session getSession(){
session=HibernateUtil.getSession();
return session;
}
protected final void closeSession(){
HibernateUtil.close();
}*/
/**
* 主键查询
* @param cla
* @param id
* @return
*/
/* protected final Object get(Class cla,Serializable id){
Object obj=null;
this.getSession();
try {
obj=this.session.get(cla, id);
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
this.closeSession();
}
return obj;
}*/
/**
* 保存对象返回主键
* @param obj
* @return
*/
/*protected final Serializable save(Object obj){
Serializable id=null;
if(obj==null){
return -1;
}
this.getSession();
Transaction tran=null;
try {
tran=this.session.beginTransaction();
id=this.session.save(obj);
tran.commit();
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
if(tran != null){
tran.rollback();
}
} finally{
this.closeSession();
}
return id;
}*/
/**
* 修改
* @param obj
* @return
*/
/*protected final int update(Object obj){
int flag=0;
if(obj==null){
return -1;
}
this.getSession();
Transaction tran=null;
try {
tran=this.session.beginTransaction();
this.session.update(obj);
tran.commit();
flag=1;
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
if(tran != null){
tran.rollback();
}
} finally{
this.closeSession();
}
return flag;
}*/
/**
* 删除
* @param obj
* @return
*/
/*protected final int delete(Object obj){
int flag=0;
if(obj==null){
return -1;
}
this.getSession();
Transaction tran=null;
try {
tran=this.session.beginTransaction();
this.session.delete(obj);
tran.commit();
flag=1;
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
if(tran != null){
tran.rollback();
}
} finally{
this.closeSession();
}
return flag;
}*/
/**
* 按照hql语句查询
* @param hql
* @param params
* @return
*/
protected final List findByHql(String hql,Map params){
List list=null;
this.getSession();
Query query=this.session.createQuery(hql);
if(params!=null && params.size()>0){
query.setProperties(params);
}
list=query.list();
this.closeSession();
return list;
}
/**
* 对象查询
* @param obj
* @return
*/
protected final List findByExample(Object obj,MatchMode mode){
List list=null;
this.getSession();
Criteria c=this.session.createCriteria(obj.getClass());
Example ex=Example.create(obj);
if(mode!=null){
ex.enableLike(mode);
}
c.add(ex);
list =c.list();
this.closeSession();
return list;
}
/**
* 使用QBC查询
* @param det
* @return
*/
protected final List findByCriteria(DetachedCriteria det){
this.getSession();
Criteria c=det.getExecutableCriteria(this.session);
List list=c.list();
this.closeSession();
return list;
}