第一次hibernate

3個準備,7個步驟

三個準備:準備Jar包 準備User類 準備映射和數據庫

七個步驟:Configuration、SessionFactory、打開Session、開始一個事務、持久化操作save/update/delete/get、提交事務 、關閉Session

1.導入jar包

   orm包和JDBC

2.創建User.java類

保存在hibernate包下

[java]  view plain  copy
  1. package hibernate;  
  2.   
  3. import java.io.Serializable;  
  4. import java.util.Date;  
  5.   
  6. public class User implements Serializable {    
  7.     private long id;  
  8.     private String name;  
  9.     private Date birthday;  
  10.       
  11.     public long getId() {  
  12.         return id;  
  13.     }  
  14.   
  15.     public void setId(long id) {  
  16.         this.id = id;  
  17.     }  
  18.   
  19.     public String getName() {  
  20.         return name;  
  21.     }  
  22.   
  23.     public void setName(String name) {  
  24.         this.name = name;  
  25.     }  
  26.   
  27.     public Date getBirthday() {  
  28.         return birthday;  
  29.     }  
  30.   
  31.     public void setBirthday(Date birthday) {  
  32.         this.birthday = birthday;  
  33.     }  
  34.   
  35.     public User() {  
  36.     }  
  37. }  

2.創建hibernate映射文件 hibernate.cfg.xml

此處的mapping resource 應該寫全

hibernate/User.hbm.xml

要注意文件名的大小寫

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <hibernate-configuration>  
  3. <session-factory>     
  4.     <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>  
  5.     <property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/test</property>  
  6.     <property name="hibernate.connection.username">root</property>  
  7.     <property name="hibernate.connection.password"></property>  
  8.     <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>  
  9.     <property name="show_sql">true</property>  
  10.       <mapping resource="hibernate/User.hbm.xml"/>  
  11. </session-factory>  
  12. </hibernate-configuration>  

3.創建User類的映射文件User.hbm.xml

    這個文件和User類放在一個包下

    映射裏的column的字段名與數據庫的字段名不區分大小寫(也就是說這裏大寫數據庫小寫並不影響數據流通)

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <hibernate-mapping>  
  4.   <class name="hibernate.User" table="t_user">  
  5.     <id name="id" column="ID"  type="long">  
  6.         <generator class="increment"/>  
  7.     </id>  
  8.     <property name="name" column="NAME" />  
  9.     <property name="birthday" column="BIRTHDAY"/>  
  10.   </class>  
  11. </hibernate-mapping>  

4.添加測試類

[java]  view plain  copy
  1. package hibernate;  
  2.   
  3. import java.util.Date;  
  4.   
  5. import org.hibernate.Session;  
  6. import org.hibernate.SessionFactory;  
  7. import org.hibernate.Transaction;  
  8. import org.hibernate.cfg.Configuration;  
  9.   
  10. public class test {  
  11.   
  12.     public static void main(String[] args) {  
  13.         Configuration conf = new Configuration().configure();//1、讀取配置文件  
  14.         SessionFactory sf = conf.buildSessionFactory();// 2、創建SessionFactory  
  15.         Session session = sf.openSession();// 3、打開Session  
  16.         Transaction tx = ;  
  17.         try{  
  18.         tx = session.beginTransaction();// 4、開始一個事務  
  19.         // 5、持久化操作  
  20.         User user = new User();  
  21.         user.setName("Hibernate user");  
  22.         user.setBirthday(new Date());;  
  23.         session.save(user);  
  24.         tx.commit();// 6、 提交事務        
  25.          }catch(Exception e){  
  26.         if (!=tx){tx.rollback();}  
  27.         e.printStackTrace();        
  28.          }finally{  
  29.         session.close();// 7、關閉Session  
  30.          }  
  31. }  
  32.   
  33.   
  34. }  

數據庫


項目目錄