hibernate增删改查的标准范例
一个套用hibernate框架编写的增删改查小范例,此处分享一下,经过多次修改,从代码规范和后期维护,以及简洁程度上说:算是很标准的书写格式;[java]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();}}p补充:软件开发 , Java ,
上一个:简单的java版http代理
下一个:Java生产图片验证码
- 更多JAVA疑问解答:
- java怎么在线读取ftp服务器上的文件内容
- 关于程序员的职业规划
- HTML和JSP矛盾吗?
- java小程序如何打包?
- java怎么split路径文件名?
- jsp+javaBean中Column 'ordersPrice' specified twice的错误
- Java TCP/IP Socket网络编程系列
- 大家来讨论一下我到底该用什么好?Swing 还是 JavaFX
- 关于Hibernate实体自身多对一的抓取问题
- 关于apache2+tomcat群集出现的问题
- spring 获取上下文问题
- SSH 导入导出excel 谁有这块的资料吗?
- Ext TreePanel 刷新问题
- springmvc 加载一个jsp页面执行多个方法 报404
- checkbox数组action怎么向页面传值