(03)Hibernate實現CRUD的案例

來源:互聯網
上載者:User

標籤:hibernate


IEmployeeDao.java

package com.rk.hibernate.b_crud;import java.io.Serializable;import java.util.List;import com.rk.hibernate.a_hello.Employee;public interface IEmployeeDao{Employee findById(Serializable id);void save(Employee emp);void update(Employee emp);void delete(Serializable id);List<Employee> findAll();List<Employee> findByName(String empName);List<Employee> page(int index,int count);}

EmployeeDaoImpl.java

package com.rk.hibernate.b_crud;import java.io.Serializable;import java.util.List;import org.hibernate.Query;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import com.rk.hibernate.a_hello.Employee;import com.rk.hibernate.utils.HibernateUtils;public class EmployeeDaoImpl implements IEmployeeDao{@Overridepublic Employee findById(Serializable id){Session session = null;Transaction tx = null;try{// 擷取Sessionsession = HibernateUtils.getSession();// 開啟事務tx = session.beginTransaction();// 主鍵查詢Employee obj = (Employee) session.get(Employee.class, id);return obj;}catch (Exception e){throw new RuntimeException(e);}finally{tx.commit();session.close();}}@Overridepublic void save(Employee emp){Session session = null;Transaction tx = null;try{session = HibernateUtils.getSession();tx = session.beginTransaction();// 執行儲存操作session.save(emp);}catch (Exception e){throw new RuntimeException(e);}finally{tx.commit();session.close();}}@Overridepublic void update(Employee emp){Session session = null;Transaction tx = null;try{session = HibernateUtils.getSession();tx = session.beginTransaction();session.update(emp);}catch (Exception e){throw new RuntimeException(e);}finally{tx.commit();session.close();}}@Overridepublic void delete(Serializable id){Session session = null;Transaction tx = null;try{session = HibernateUtils.getSession();tx = session.beginTransaction();// 先根據id查詢對象,再判斷刪除Object obj = session.get(Employee.class, id);if (obj != null) session.delete(obj);}catch (Exception e){throw new RuntimeException(e);}finally{tx.commit();session.close();}}@Overridepublic List<Employee> findAll(){Session session = null;Transaction tx = null;try{session = HibernateUtils.getSession();tx = session.beginTransaction();// HQL查詢Query q = session.createQuery("from Employee");List<Employee> list = q.list();return list;}catch (Exception e){throw new RuntimeException(e);}finally{tx.commit();session.close();}}@Overridepublic List<Employee> findByName(String empName){Session session = null;Transaction tx = null;try{session = HibernateUtils.getSession();tx = session.beginTransaction();Query q = session.createQuery("from Employee where empName=?");// 注意:參數索引從0開始q.setParameter(0, empName);// 執行查詢List<Employee> list = q.list();return list;}catch (Exception e){throw new RuntimeException(e);}finally{tx.commit();session.close();}}@Overridepublic List<Employee> page(int index, int count){Session session = null;Transaction tx = null;try{session = HibernateUtils.getSession();tx = session.beginTransaction();Query q = session.createQuery("from Employee");// 設定分頁參數q.setFirstResult(index); // 查詢的起始行q.setMaxResults(count); // 查詢返回的行數List<Employee> list = q.list();return list;}catch (Exception e){throw new RuntimeException(e);}finally{tx.commit();session.close();}}}


HibernateUtils.java

package com.rk.hibernate.utils;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;public class HibernateUtils{private static SessionFactory sessionFactory;static{sessionFactory = new Configuration().configure().buildSessionFactory();}public static Session getSession(){return sessionFactory.openSession();}}







(03)Hibernate實現CRUD的案例

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.