日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

hibernate增删改查的标准范例

發布時間:2025/7/14 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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增删改查的标准范例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。