1. 在能能夠對hibernate 初始化後,就能夠經過SessionFactory來獲取一個Session來對Object來操做,不用再進行編寫SQL語句來經過jdbc的dao來對數據庫來操做。例如:
java
import java.sql.DriverManager; import java.sql.Connection; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class Demo{ private static String DBURL="jdbc:mysql://localhost:3306/test"; private static String DBUSER="root"; private static String DBPASSWORD="123456"; private static String DBDRIVER="org.gjt.mm.mysql.Driver"; private Connection conn=null; public Demo(){ try{ Class.forName(DBDRIVER); this.conn=DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD); }catch(Exception e){ e.printStackTrace(); } } public static void main(String args[]){ String sql = "select * from user ;"; PreparedStatement pst = conn.prepareStatement(sql); ResultSet re = pstat.executeQuery(); } }
使用JDBC來操做數據庫,無非就是經過數據庫的url和數據庫的用戶密碼,加上數據庫對應的JDBC驅動獲取一個Connection鏈接來鏈接到數據庫,而後再經過Connection來獲取一個PreparedStatement,經過PreparedStatement來獲取一個ResultSet結果集mysql
2. 如何使用Hibernate的話,就是經過一個工具類,來把SessionFactory封裝成單例模式,而後能夠經過這個工具類獲取對SessionFactory的引用來獲取Session.sql
import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistry; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; public class HiberanteUtil { private static SessionFactory factory; static{ try{ Configuration cfg = new Configuration().configure(); StandardServiceRegistry registry = new StandardServiceRegistryBuilder() .applySettings(cfg.getProperties()).build(); factory = cfg.buildSessionFactory(registry); }catch (Throwable ex) { System.err.println("Failed to create sessionFactory object." + ex); throw new ExceptionInInitializerError(ex); } } public SessionFactory getSessionFactory( ){ return this.factory; } }
3.編寫一個DAO的操做類數據庫
package cn.tian.dao; import java.io.Serializable; import java.util.ArrayList; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import cn.tian.vo.User; public class UserDaoImpl implements UserDao{ private SessionFactory factory; public RoleDaoImpl(SessionFactory sessionFactory){ this.factory = sessionFactory; } /* 數據庫添加一行記錄 */ @Override public Serializable insert(User user) { // TODO Auto-generated method stub //獲取Session Session session = factory.openSession(); //Hiberante事務管理 Transaction tx = null; Serializable serializable = null; try { //開啓一個新的事務 tx = session.beginTransaction(); //將對象變成持久狀態,至關對insert into 操做 serializable = session.save(user); //事務提交 tx.commit(); } catch (Exception e) { if (tx!=null) tx.rollback(); log.debug("UserDao: can't insert "+user); } finally { //關閉session會話,釋放系統資源 session.close(); } return serializable; } //更新操做 @Override public Role update(User user) { // TODO Auto-generated method stub Session session = factory.openSession(); Transaction tx = null; try{ tx = session.beginTransaction(); session.update(user); tx.commit(); }catch(Exception e){ if(tx!=null) tx.rollback(); log.debug("UserDao: can't update "+user); } finally{ session.close(); } return user; } //刪除操做 @Override public Role delete(User user) { // TODO Auto-generated method stub Session session = factory.openSession(); Transaction tx = null; try{ tx = session.beginTransaction(); session.delete(user); tx.commit(); } catch(Exception e){ if(tx!=null) tx.rollback(); log.debug("UserDao: can't delete "+user); } finally{ session.close(); } return user; } //查詢,獲取所有記錄 @Override public List<User> getList() { // TODO Auto-generated method stub String hql = "from User u"; List<User> list = null; Session session = factory.openSession(); Transaction tx = null; try{ tx = session.beginTransaction(); Query query = session.createQuery(hql); //在這裏須要特別注意list的泛型轉換,由於hibernate返回的都是Object類型的,須要轉換爲對應的Class list = query.list(); tx.commit(); }catch(Exception e){ if(tx!=null) tx.rollback(); log.debug("UserDao: can't select from user"); }finally{ session.close(); } return list; } //只查詢一條記錄 @Override public User get(Long id) { // TODO Auto-generated method stub Session session = factory.openSession(); User user = null; try{ //由於返回的是Object,因此強制轉換爲對應的Class user = (Role)session.get(User.class,id); }catch(Exception e ){ log.debug("UserDao: can't find "+id); } finally{ session.close(); } return user; } }
與之對比,在Hibernate對數據庫的添加刪除修改的時候,比JDBC少了SQL的硬編碼,但須要注意其返回的類型轉換
session