基于hibernate对数据库操作封装
hibernate将数据库层抽象出来,且很方便的进行操作,本文对其进行了二次封装,方便调用。类图如下所示
将对于数据库的操作抽象成为泛型接口,BaseDao实现泛型接口,UserDao实例化泛型,DaoFactory包含有
所有对于实体操作对象,当需要对某个实体操作时,只需要调用相应的get方法即可。
IBaseDao<T>接口
public interface IBaseDao<T> { public boolean save(T obj); public boolean saveOrUpdate(T obj); public boolean update(T domain); public boolean delete(T obj); public List<T> findByProperty(String propertyName,Object value); public List<T> findAll(String orderBy,boolean isAsc); public List<T> query(String sql); public int queryCount(String sql); public boolean executeSql(String sql); }
BaseDao<T>实现了接口
public class BaseDao<T> implements IBaseDao{ private Class clazz; public BaseDao(Class clazz){ this.clazz=clazz; } public Session getSession() { return HibernateSessionFactory.getSession(); } public Transaction getTransaction() { return getSession().beginTransaction(); } private String getTableName() { return clazz.getSimpleName(); } @Override public boolean save(Object obj) { Session session = null; try { session = getSession(); session.beginTransaction(); session.save(obj); session.getTransaction().commit(); return true; } catch (Exception e) { e.printStackTrace(); session.getTransaction().rollback(); return false; } finally { if (session != null) { if (session.isOpen()) { // 关闭session session.close(); } } } } @Override public boolean saveOrUpdate(Object obj) { Session session = null; try { session = getSession(); session.beginTransaction(); session.saveOrUpdate(obj); session.getTransaction().commit(); return true; } catch (Exception e) { e.printStackTrace(); session.getTransaction().rollback(); return false; } finally { if (session != null) { if (session.isOpen()) { // 关闭session session.close(); } } } } @Override public boolean update(Object obj) { Session session = null; try { session = getSession(); session.beginTransaction(); session.update(obj); session.getTransaction().commit(); return true; } catch (Exception e) { e.printStackTrace(); session.getTransaction().rollback(); return false; } finally { if (session != null) { if (session.isOpen()) { // 关闭session session.close(); } } } } @Override public boolean delete(Object obj) { Session session = null; try { session = getSession(); session.beginTransaction(); session.delete(obj); session.getTransaction().commit(); return true; } catch (Exception e) { e.printStackTrace(); session.getTransaction().rollback(); return false; } finally { if (session != null) { if (session.isOpen()) { // 关闭session session.close(); } } } } @SuppressWarnings("unchecked") @Override public List<T> findByProperty(String propertyName, Object value) { try { String queryString = "from " +getTableName()+ " as model where model." + propertyName + "= ?"; Query queryObject = getSession().createQuery(queryString); queryObject.setParameter(0, value); return queryObject.list(); } catch (RuntimeException re) { throw re; } finally { getSession().close(); } } @SuppressWarnings("unchecked") @Override public List<T> findAll(String orderBy, boolean isAsc) { try { String queryString = "from " + getTableName(); if (isAsc) { queryString += " order by " + orderBy + " ASC"; } else { queryString += " order by " + orderBy + " DESC"; } Query queryObject = getSession().createQuery(queryString); return queryObject.list(); } catch (RuntimeException re) { throw re; } finally { getSession().close(); } } @SuppressWarnings("unchecked") @Override public List<T> query(String sql) { try { SQLQuery queryObject = getSession().createSQLQuery(sql); queryObject.addEntity(clazz); return queryObject.list(); } catch (RuntimeException re) { throw re; } finally { getSession().close(); } } @Override public int queryCount(String sql) { try { Query query = getSession().createSQLQuery(sql); BigInteger result = (BigInteger) query.uniqueResult(); return result.intValue(); } catch (RuntimeException re) { throw re; } finally { getSession().close(); } } @Override public boolean executeSql(String sql) { Session session = null; try { session = getSession(); session.beginTransaction(); SQLQuery queryObject = session.createSQLQuery(sql); int rs = queryObject.executeUpdate(); session.getTransaction().commit(); if(rs<=0){ return false; } } catch (RuntimeException re) { session.getTransaction().rollback(); throw re; } finally { if (session != null) { if (session.isOpen()) { // 关闭session session.close(); } } } return true; } }
UserDao实例化泛型,并定义特殊的方法
public class UserDao extends BaseDao<User> { public UserDao(Class clazz) { super(clazz); } public List<User> findAllMen(){ return findByProperty("sex", "男"); } public List<User> findAllWomen(){ return findByProperty("sex","女"); } }
DaoFactory采用单例模式,并包含有所有对于实体的操作对象
public class DaoFactory { private static DaoFactory m_Instance = new DaoFactory(); private UserDao userDao=null; private DaoFactory(){} public static DaoFactory getNewInstance(){ return m_Instance; } public UserDao getUserDao(){ if(userDao==null){ userDao=new UserDao(User.class); } return userDao; } }
客户端调用
public static void main(String[]args){ DaoFactory dao=DaoFactory.getNewInstance(); UserDao user=dao.getUserDao(); List<User> list=user.findAllMen(); for(int i=0;i<list.size();i++){ System.out.println(list.get(i).getName()); } }
对于hibernate的配置,可以参考http://blog.csdn.net/jiuqiyuliang/article/details/39380465