📄 comdaoimp.java
字号:
package com.yd.dao;
import java.io.Serializable;
import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.yd.hbn.HibernateSessionFactory;
import com.yd.pojos.Dep;
/**
* 通用接口的实现类
* @author Administrator
*这个类中间的方法都只得到session对象,启动事务进行处理
*都没有关闭session对象,session对象在过滤器中间来关闭
*/
public class ComDAOIMP implements ComDAO {
protected Logger log=Logger.getLogger(this.getClass());
public boolean add(Object ob) {
Session session = HibernateSessionFactory.getSession();
Transaction tx = session.beginTransaction();
try {
session.save(ob);
tx.commit();
log.info("add方法执行成功");
return true;
} catch (Exception e) {
log.error("ComDAOIMP z中间的add方法错误",e);
tx.rollback();
return false;
}
}
public boolean delete(Object ob) {
Session session = HibernateSessionFactory.getSession();
Transaction tx = session.beginTransaction();
try {
session.delete(ob);
tx.commit();
return true;
} catch (Exception e) {
log.info("delete",e);
tx.rollback();
return false;
}
}
public boolean deleteById(Class cl, Serializable id) {
Session session = HibernateSessionFactory.getSession();
Transaction tx = session.beginTransaction();
try {
Object ob=session.load(cl, id);
session.delete(ob);
tx.commit();
return true;
} catch (Exception e) {
e.printStackTrace();
tx.rollback();
return false;
}
}
public <cl> List<cl> getAllOjbect(Class cl) {
Session session = HibernateSessionFactory.getSession();
try {
Query q=session.createQuery("from "+cl.getName());
List ls=q.list();
ls.size();
return ls;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public boolean update(Object ob) {
Session session = HibernateSessionFactory.getSession();
Transaction tx = session.beginTransaction();
try {
session.update(ob);
tx.commit();
return true;
} catch (Exception e) {
e.printStackTrace();
tx.rollback();
return false;
}
}
public<E> E getOjbectById(Class cl, Serializable id) {
Session session = HibernateSessionFactory.getSession();
try {
E ob=(E)session.load(cl, id);
return ob;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -