• 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);
                }
            }
        }
    
    }
  • 相关阅读:
    WebService到底是什么?(转)
    C# 页面抓取获取快递信息
    SQL Server ->> 条件筛选做法之 -- IN(VALUE1,VALUE2,...)与INNER JOIN STRING_SPLIT()性能对比
    SQL Server ->> MSDB.DBO.AGENT_DATETIME函数从整型转时间日期格式
    Windows ->> Windows Server 2012打开管理添加“我的电脑”桌面图标途径
    Windows ->> Windows下安装MSI程序遇到2503和2502错误
    SQL Server ->> DAC(Dedicated Administrator Connection)专用管理员连接
    SQL Server ->> 高可用与灾难恢复(HADR)技术之 -- Transaction Replication(事务复制)
    SQL Server ->> ColumnStore Index(列存储索引)
    SQL Server ->> 使用Azure Active Directory Authentication连接到Azure SQL Database
  • 原文地址:https://www.cnblogs.com/zhangbaowei/p/4853406.html
Copyright © 2020-2023  润新知