第一次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. }  

数据库


项目目录