zoukankan      html  css  js  c++  java
  • 3、Hibernate实现简单的CRUD操作

    CRUD :

    Create 增加   Retrieve 读取(重新获得数据)  Update 更新   Delete  删除

    PersonDao的实现如下:

    package com.baowei.dao;
    
    import java.util.List;
    
    import org.hibernate.Session;
    
    import com.baowei.entity.Person;
    import com.baowei.utils.HibernateUtil;
    
    public class PersonDao {
    
        public void save(Person person) {
            Session session = null;
            try {
                session = HibernateUtil.openSession();
                session.getTransaction().begin();
                session.save(person);
                session.getTransaction().commit();
            } catch (Exception e) {
                if (session != null)
                    session.getTransaction().rollback();
                throw new RuntimeException(e);
            } finally {
                if (session != null) {
                    HibernateUtil.close(session);
                }
            }
    
        }
    
        public void update(Person person) {
            Session session = null;
            try {
                session = HibernateUtil.openSession();
                session.getTransaction().begin();
                session.update(person);
                session.getTransaction().commit();
            } catch (Exception e) {
                if (session != null)
                    session.getTransaction().rollback();
                throw new RuntimeException(e);
            } finally {
                if (session != null) {
                    HibernateUtil.close(session);
                }
            }
    
        }
    
        public void delete(Person person) {
            Session session = null;
            try {
                session = HibernateUtil.openSession();
                session.getTransaction().begin();
                session.delete(person);
                session.getTransaction().commit();
            } catch (Exception e) {
                if (session != null)
                    session.getTransaction().rollback();
                throw new RuntimeException(e);
            } finally {
                if (session != null) {
                    HibernateUtil.close(session);
                }
            }
    
        }
    
        public Person findById(int id) {
            Session session = null;
            Person person = null;
            try {
                session = HibernateUtil.openSession();
                session.getTransaction().begin();
                person = (Person) session.get(Person.class, id);
                session.getTransaction().commit();
                return person;
            } catch (Exception e) {
                throw new RuntimeException(e);
            } finally {
                if (session != null)
                    session.getTransaction().rollback();
                if (session != null) {
                    HibernateUtil.close(session);
                }
            }
        }
    
        public List findAll() {
            Session session = null;
            List persons = null;
            try {
                session = HibernateUtil.openSession();
                session.getTransaction().begin();
                persons = session.createQuery("from Person").list();
                // persons = session.createSQLQuery("select * from person").list();
                session.getTransaction().commit();
                return persons;
            } catch (Exception e) {
                if (session != null)
                    session.getTransaction().rollback();
                throw new RuntimeException(e);
            } finally {
                if (session != null) {
                    HibernateUtil.close(session);
                }
            }
        }
    
        public List page(int firstResult, int maxResults) {
            Session session = null;
            List persons = null;
            try {
                session = HibernateUtil.openSession();
                session.getTransaction().begin();
                persons = session.createQuery("from Person")
                        .setFirstResult(firstResult).setMaxResults(maxResults)
                        .list();
                // persons = session.createSQLQuery("select * from person").list();
                session.getTransaction().commit();
                return persons;
            } catch (Exception e) {
                if (session != null)
                    session.getTransaction().rollback();
                throw new RuntimeException(e);
            } finally {
                if (session != null) {
                    HibernateUtil.close(session);
                }
            }
        }
    
    }
  • 相关阅读:
    java将pdf转成base64字符串及将base64字符串反转pdf
    input校验不能以0开头的数字
    js校验密码,不能为空的8-20位非纯数字或字母的密码
    tomcat正常关闭,端口号占用解决 StandardServer.await: create[8005]:
    Eclipse中项目报Target runtime com.genuitec.runtime.generic.jee60 is not defined异常的解决
    Access restriction: The type Base64 is not accessible due to restriction on
    [操作系统] 线程和进程的简单解释
    ssh登录一段时间后断开的解决方案
    [SAMtools] 常用指令总结
    [C] 有关内存问题
  • 原文地址:https://www.cnblogs.com/zhangbaowei/p/4853406.html
Copyright © 2011-2022 走看看