一个套用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增删改查的标准范例相关推荐

  1. java三大框架增删改查_Java_Web三大框架之Hibernate增删改查

    packagecom.msl.test;importorg.hibernate.Session;importorg.hibernate.Transaction;importcom.msl.Hibern ...

  2. Hibernate修改操作 删除操作 查询操作 增加操作 增删改查 Hibernate增删查改语句...

    我用的数据库是MySQL,实体类叫User public class User {private Integer uid;private String username;private String ...

  3. Hibernate的四种典型例子(增删改查)

    Hibernate的四种典型例子(增删改查)  //查询  // Session session=HibernateSessionFactory.getSession();  // session.b ...

  4. hibernate实现增删改查的各种方法

    1>接口(主要是增删改查的接口)BaseDao.java [java] view plain copy /** * * @author fly.zhou */ public interface  ...

  5. spring入门(11)-spring与hibernate整合完成增删改查的操作(封装HibernateTemplate模版类对象)

    今天是spring的最后一节课,这节课老师讲了spring与hibernate整合完成增删改查的操作,这是很重要的一节课,这也是第一次真正的实现spring结合Hibernate和数据库连接上,下面是 ...

  6. Hibernate 基本配置文件+基本增删改查

    文件结构 maven 依赖 <!-- hibernate 核心依赖 --> <dependency><groupId>org.hibernate</group ...

  7. 一步步实现:JPA的基本增删改查CRUD(jpa基于hibernate)

    1.创建一个JPA工程 首先,创建一个JPA工程(若不知道JPA创建或出现at least one user library must be selected等错误,请参考http://blog.cs ...

  8. Hibernate中对增删改查的小结

    Hibernate中对增删改查的小结 mysql中库表News,字段如下 id      |  int     |  auto_increment  |  primary key title   |  ...

  9. Hibernate 笔记4 实现对数据库的增删改查

    1  准备 首先在mysql数据库中建表User,并添加相关信息. user表结构如下. +-------+--------------+------+-----+---------+-------- ...

最新文章

  1. IDEAWebstorm使用
  2. Parallels Desktop 重装系统
  3. 重磅,Intel考虑收购博通
  4. 比特币ABC推出最新版本后讨论网络不兼容性
  5. java输出回文数原代码_leetcode 9 回文数
  6. LSTM如何解决梯度消失或爆炸的?
  7. 软件需求包括3个不同的层次 - 业务需求、用户需求和功能需求
  8. (五)Oracle函数 序列 约束 索引
  9. 7个C语言小程序让你快速入门程序世界
  10. leetcode350C++
  11. 数据库事务的四个隔离级别
  12. 【Redis数据库】命令学习笔记——发布订阅、事务、脚本、连接等命令汇总
  13. winpython anaconda_Windows:安装 Anaconda《 Python:基础 》
  14. jQuery实现百度导航li拖放排列效果,即时更新数据库
  15. UI设计师需要熟记的45个快捷键Windows、Mac
  16. 浏览器内核学习笔记一
  17. 编写Java程序,创建一个父类交通工具类(Vehicles),以及两个子类,分别是轿车类(Car)和卡车类(Truck)。
  18. Gradle下载慢解决方案
  19. 速读原著-UnixLinux基础(七)
  20. 移动互联网时代,不会“网络预约”的老年人应该怎么办

热门文章

  1. Protocol Buffers java
  2. Symfony3实现刷新登录时间
  3. 《图解CSS3:核心技术与案例实战》——1.1节什么是CSS3
  4. hdu 统计难题(map)
  5. 优秀交互设计的 UI 原则
  6. 企业微信提示服务商未响应请求,将无法获取用户事件回调
  7. Redux学习(2) ----- 异步和中间件
  8. Codeforces Beta Round #17 D. Notepad (数论 + 广义欧拉定理降幂)
  9. 201521123011 《java程序设计》 第9周学习总结
  10. Atitit 提升效率 界面gui方面的前后端分离与cbb体系建设 规范与推荐标准