hibernate增删改查的标准范例
生活随笔
收集整理的這篇文章主要介紹了
hibernate增删改查的标准范例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
??? 一個套用hibernate框架編寫的增刪改查小范例,此處分享一下,經過多次修改,從代碼規范和后期維護,以及簡潔程度上說:算是很標準的書寫格式;
package www.csdn.net.bookhome.daoimpl;import java.util.List;import org.hibernate.Session; import org.hibernate.Transaction;import www.csdn.net.bookhome.dao.AdminDao; import www.csdn.net.bookhome.dao.BaseHibernateDao; import www.csdn.net.bookhome.domain.Admin; import www.csdn.net.bookhome.utils.HibernateSessionFactory;public class AdminDaoImpl extends BaseHibernateDao implements AdminDao {public void deleteObject(Admin entity) {Transaction tx = null;try {Session session = getSession();tx = session.beginTransaction();session.delete(entity);tx.commit();} catch (Exception e) {tx.rollback();throw new RuntimeException("刪除所有錯誤"+e);} finally {HibernateSessionFactory.closeSession();}}public void deleteObjectById(Integer id) {Transaction tx = null;try {Session session = getSession();tx = session.beginTransaction();session.save(id);tx.commit();} catch (Exception e) {tx.rollback();throw new RuntimeException("根據id錯誤"+e);} finally {HibernateSessionFactory.closeSession();}}public List getAllObjects(Class entityClass) {try {return getSession().createQuery("from Admin").list();} catch (Exception e) {throw new RuntimeException("查找錯誤"+e);} finally {HibernateSessionFactory.closeSession();}}public Admin getObjectById(Class className, Integer id) {try {return (Admin) getSession().get(className, id);} catch (Exception e) {throw new RuntimeException("根據id查找錯誤"+e);} finally {HibernateSessionFactory.closeSession();}}public List getObjects(Class clazz, int from, int size, String orderName) {try {return getSession().createQuery("from Admin").setFirstResult((from-1)*size).setMaxResults(size).list();} catch (Exception e) {throw new RuntimeException("分頁查詢錯誤"+e);} finally {HibernateSessionFactory.closeSession();}}public Admin loadObjectById(Class className, Integer id) {try {return (Admin) getSession().load(className, id);} catch (Exception e) {throw new RuntimeException("load查詢錯誤"+e);} finally {HibernateSessionFactory.closeSession();}}public void saveObject(Admin entity) {Transaction tx = null;try {Session session = getSession();tx = session.beginTransaction();session.save(entity);tx.commit();} catch (Exception e) {tx.rollback();throw new RuntimeException("保存錯誤"+e);} finally {HibernateSessionFactory.closeSession();}}public void updateObject(Admin entity) {Transaction tx = null;try {Session session = getSession();tx = session.beginTransaction();session.update(entity);tx.commit();} catch (Exception e) {tx.rollback();throw new RuntimeException("更新錯誤"+e);} finally {HibernateSessionFactory.closeSession();}}public Admin getAllObjects(String name) {try { return (Admin) getSession().createQuery("from Admin a where a.adminName = :name").setParameter("name", name).uniqueResult();} catch (Exception e) {throw new RuntimeException("根據用戶查詢有錯誤"+e);} finally {HibernateSessionFactory.closeSession();}}public Admin login(Admin entity) {try {return (Admin) getSession().createQuery("from Admin a where a.adminName = :name and a.adminPassword = :pass ").setString("name",entity.getAdminName()).setString("pass", entity.getAdminPassword()).uniqueResult();} catch (Exception e) {throw new RuntimeException("登錄錯誤"+e);} finally {HibernateSessionFactory.closeSession();}}}
?
轉載于:https://www.cnblogs.com/yangkai-cn/archive/2013/01/09/4016989.html
總結
以上是生活随笔為你收集整理的hibernate增删改查的标准范例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【WP7】对象序列化
- 下一篇: 页面无刷新ajax上传文件--模拟ifr