接口规范,定义CURD操作
package edu.sasu.DAO; import java.util.List; /** * 所有实体类遵循的接口 * @author Administrator * * @param* @param */ public interface BaseDao { /** * 保存实体 * @param entity 实体类 */ public boolean save(T entity); /** * 删除实体 * @param entity 实体类 */ public boolean delete(T entity); /** * 根据实体ID删除实体 * @param entity 实体类 */ public boolean deleteById(T entity); /** * 跟新实体 * @param entity 实体类 */ //public boolean update(T entity); public boolean update(T entity, Object OID); /** * 根据实体d,查询单个实体 * @param entity 实体类 * @return */ //public T findById(T entity); public T findById(T entity, Object entityID); /** * 累出所有实体集合 * @param entityClass 实体类 * @return 实体类List */ public List findAll(String hql); public List findAll(T entity); /** * 保存和跟新方法 */ public boolean saveOrUpdate(T entity); }
实现BaseDao接口,实现其中的方法
package edu.sasu.DAOImpl; import java.io.Serializable; import java.util.ArrayList; import java.util.List; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.Transaction; import edu.sasu.DAO.BaseDao; import edu.sasu.Util.DBUtil; /** * 封装的基类操作 * @author Administrator * * @param*/ public class BaseDAOImpl implements BaseDao { public synchronized boolean delete(T entity) { // TODO Auto-generated method stub return false; } /** * 删除某条数据 */ public synchronized boolean deleteById(T entity) { Session session = DBUtil.getDBUtil().getSession(); Transaction tx = null; try { tx = session.beginTransaction(); session.delete(entity); tx.commit(); } catch (HibernateException e) { if(tx != null){ tx.rollback(); } return false; } return true; } public synchronized List findAll(T entity) { // TODO Auto-generated method stub return null; } /** * 查找单个数据 */ public synchronized T findById(T entity, Object entityID) { Session session = DBUtil.getDBUtil().getSession(); Transaction tx = null; T temp; try { tx = session.beginTransaction(); temp = (T) session.get(entity.getClass(),(Serializable) entityID); tx.commit(); } catch (HibernateException e) { if(tx != null){ tx.rollback(); } throw e; }finally{ } return temp; } /** * 保存数据 */ public synchronized boolean save(T entity) { Session session = DBUtil.getDBUtil().getSession(); Transaction tx = null; try { tx = session.beginTransaction(); //session.persist(entity); session.save(entity); tx.commit(); } catch (HibernateException e) { if(tx != null){ tx.rollback(); } //return false; e.printStackTrace(); } return true; } /** * 跟新数据 */ public synchronized boolean update(T entity, Object OID) { Session session = DBUtil.getDBUtil().getSession(); Transaction tx = null; try { tx = session.beginTransaction(); T temp = (T) session.get(entity.getClass(), (Serializable) OID); session.merge(entity); //如果update要抛异常 tx.commit(); } catch (HibernateException e) { if(tx != null){ tx.rollback(); } //return false; e.printStackTrace(); } return true; } /** * 查找符合hql的所有数据 */ public synchronized List findAll(String hql) { Session session = DBUtil.getDBUtil().getSession(); Transaction tx = null; List list = new ArrayList (); try { tx = session.beginTransaction(); list = session.createQuery(hql).list(); session.flush(); tx.commit(); } catch (HibernateException e) { if(tx != null){ tx.rollback(); } e.printStackTrace(); } return list; } /** * 保存和跟新方法 */ public synchronized boolean saveOrUpdate(T entity) { Session session = DBUtil.getDBUtil().getSession(); Transaction tx = null; try { tx = session.beginTransaction(); session.saveOrUpdate(entity); tx.commit(); } catch (HibernateException e) { if(tx != null){ tx.rollback(); } //return false; e.printStackTrace(); } return true; } }
基础工厂采用单例实现获取实例操作对象
package edu.sasu.factory; import edu.sasu.DAO.BaseDao; import edu.sasu.DAOImpl.BaseDAOImpl; /** * 基础工厂 * @author Administrator * * @param*/ public class BaseFactory { private static BaseFactory baseFactory; //单一实例 private BaseDao Instance; private BaseFactory(){ Instance = new BaseDAOImpl (); } public BaseDao getInstance(){ //不要把factory的new放在构造函数里面,不然会出现递归错误 return Instance; } public static BaseFactory getFactory(){ if(baseFactory == null){ baseFactory = new BaseFactory(); } return baseFactory; } }
以上定义baseDAO接口,baeDAOIMpl是接口的实现,baseFactory是工厂,对baseDAOimp实例化,
在类中调用为: User(一个PoJo类)
User entity= new User();
BaseFactory
执行保存操作: factory.getInstance().save(entity); entity User的实例对象
执行保存操作,请大家评点下此种写法有什么不好的地方,本人能力有限,觉得还可以,请大牛指教