hibernate4.2.4之环境搭建与测试实例

1.下载hibernate-release-4.2.4.Final.zip 从点击打开链接( http://www.hibernate.org/downloads)下载

      

2.在eclipse中新建一个java project工程,名为hibernate_first,在hibernate_first中新建一个ilb文件夹,

     解压下载的hibernate-release-4.2.4.Final.zip,打开之后有几个文件夹 lib---jar库   documentation--文档说明  projetc---关于hibernate的配置/例子等等之类的

    进入到hibernate-release-4.2.4.Final\lib\required\ 目录下 将8个必须的jar包拷贝到 hibernate_first\lib\ 下面 并且将8个jar包单击右键选中--Build Path--Add to Build Path(即添加为工程引用)

   同时将mysql-connector-java-5.1.25-bin.jar 添加lib包下

   


3.编写配置文件 进入到hibernate-release-4.2.4.Final\project\etc\ 目录下 将hibernate.cfg.xml文件拷贝到项目hibernate_first\src\ 目录下

   将hibernate.cfg.xml文件修改为如下;

[html]  view plain copy
  1. <!DOCTYPE hibernate-configuration PUBLIC  
  2.     "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
  3.     "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">  
  4.   
  5. <hibernate-configuration>  
  6.     <session-factory>  
  7.         <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>  
  8.         <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>  
  9.         <property name="hibernate.connection.username">root</property>  
  10.         <property name="hibernate.connection.password">root</property>  
  11.           
  12.         <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>  
  13.         <property name="hbm2ddl.auto">create</property>  
  14.         <property name="hibernate.show_sql">true</property>  
  15.           
  16.         <mapping resource="com/undergrowth/hibernate/domain/Student.hbm.xml" />  
  17.           
  18.     </session-factory>  
  19. </hibernate-configuration>  

简单解释一下上面的hibernate.cfg.xml文件  

 


 connection.driver_class/connection.url/connection.username/connection.password  是用于mysql连接的驱动/资源定位符/登陆账号/登陆密码

hibernate.dialect是hibernate用于兼容多种数据库而做的,告诉hibernate使用的是哪一种数据库

hibernate.show_sql是在进行相应的hibernate操作时,会在控制台输出相应的sql语句

hbm2ddl.auto是在hibernate第一次进行操作时 都会自己创建一个表

mapping resource用于指明对象与表的映射关系 这里指的是Student类和学生表的一个映射文件  下面也会介绍到

上面的这些属性其实不用记 在hibernate-release-4.2.4.Final\project\etc\hibernate.properties文件中都可以找到

 

 

 

4.编写对象与对象的映射文件 引用上面的配置  建立Student.java类 如下

   

[java]  view plain copy
  1. package com.undergrowth;  
  2.   
  3. import java.util.Date;  
  4.   
  5. public class Student {  
  6.     private int id;    
  7.     public int getId() {  
  8.         return id;  
  9.     }  
  10.     public void setId(int id) {  
  11.         this.id = id;  
  12.     }  
  13.     public String getName() {  
  14.         return name;  
  15.     }  
  16.     public void setName(String name) {  
  17.         this.name = name;  
  18.     }  
  19.     public Date getBirthday() {  
  20.         return birthday;  
  21.     }  
  22.     public void setBirthday(Date birthday) {  
  23.         this.birthday = birthday;  
  24.     }  
  25.     public String getMajor() {  
  26.         return major;  
  27.     }  
  28.     public void setMajor(String major) {  
  29.         this.major = major;  
  30.     }  
  31.     private String name;  
  32.     private Date birthday;  
  33.     private String major;  
  34.     public Student(String name, Date birthday, String major) {  
  35.         this.name = name;  
  36.         this.birthday = birthday;  
  37.         this.major = major;  
  38.     }  
  39.       
  40.     public Student(){}  //用于给hibernate的反射使用  
  41.     @Override  
  42.     public String toString() {  
  43.         return "Student [id=" + id + ", name=" + name + ", birthday="  
  44.                 + birthday + ", major=" + major + "]";  
  45.     }  
  46.       
  47.       
  48. }  


 

 

编写Student的ORM映射文件Student.hbm.xml  hibernate自带的模板很多 这里引用hibernate-release-4.2.4.Final\project\documentation\src\main\docbook\quickstart\tutorials\basic\src\test\java\org\hibernate\tutorial\hbm\ 目录下的Event.hbm.xml
 
Student.hbm.xml

 

[html] view plain copy
  1. <?xml version="1.0"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC  
  3.         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  4.         "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">  
  5.   
  6. <hibernate-mapping package="com.undergrowth">  
  7.   
  8.     <class name="Student" table="student">  
  9.         <id name="id">  
  10.             <generator class="native"/>  
  11.         </id>  
  12.         <property name="name"/>  
  13.         <property name="birthday"/>  
  14.         <property name="major"/>  
  15.     </class>  
  16.   
  17. </hibernate-mapping>  

 

 

5.测试
StudentDaoInter.java

 

[java] view plain copy
  1. package com.undergrowth.inter;  
  2.   
  3. import com.undergrowth.Student;  
  4.   
  5. public interface StudentDaoInter {  
  6.       public void saveStudent(Student student);  
  7.       public void updateStudent(Student student);  
  8.       public void deleteStudent(Student student);  
  9.       public Student findIdStudent(int id);  
  10.       public Student findNameStudent(String name);  
  11. }  


 

 

StudentDaoImp.java
[java] view plain copy
  1. package com.undergrowth.imple;  
  2.   
  3. import com.undergrowth.Student;  
  4. import com.undergrowth.hibernate.utils.HibernateUtils;  
  5. import com.undergrowth.inter.StudentDaoInter;  
  6.   
  7. public class StudentDaoImp implements StudentDaoInter {  
  8.   
  9.     @Override  
  10.     public void saveStudent(Student student) {  
  11.         // TODO Auto-generated method stub  
  12.         HibernateUtils.add(student);  
  13.     }  
  14.   
  15.     @Override  
  16.     public void updateStudent(Student student) {  
  17.         // TODO Auto-generated method stub  
  18.         HibernateUtils.update(student);  
  19.     }  
  20.   
  21.     @Override  
  22.     public void deleteStudent(Student student) {  
  23.         // TODO Auto-generated method stub  
  24.         HibernateUtils.delete(student);  
  25.     }  
  26.   
  27.     @Override  
  28.     public Student findIdStudent(int id) {  
  29.         // TODO Auto-generated method stub  
  30.         return (Student) HibernateUtils.get(Student.class, id);  
  31.     }  
  32.   
  33.     @Override  
  34.     public Student findNameStudent(String name) {  
  35.         // TODO Auto-generated method stub  
  36.         return (Student) HibernateUtils.get(name);  
  37.     }  
  38.   
  39. }  


 

 

HibernateUtils.java
[java] view plain copy
  1. package com.undergrowth.hibernate.utils;  
  2.   
  3. import java.io.Serializable;  
  4.   
  5.   
  6.   
  7. import org.hibernate.HibernateException;  
  8. import org.hibernate.Query;  
  9. import org.hibernate.Session;  
  10. import org.hibernate.SessionFactory;  
  11. import org.hibernate.Transaction;  
  12. import org.hibernate.cfg.Configuration;  
  13.   
  14. public class HibernateUtils {  
  15.     private static SessionFactory sf;  
  16.       
  17.     static{  
  18.         Configuration cfg=new Configuration();  
  19.         cfg.configure();  
  20.         sf= cfg.buildSessionFactory();  
  21.     }  
  22.       
  23.     public static Session getSession()  
  24.     {  
  25.         return sf.openSession();  
  26.     }  
  27.       
  28.     public static void add(Object entity)  
  29.     {  
  30.         Session session=null;  
  31.         Transaction tx=null;  
  32.         try {  
  33.             session=HibernateUtils.getSession();  
  34.             tx=session.beginTransaction();  
  35.             session.save(entity);  
  36.             tx.commit();  
  37.         } catch (HibernateException e) {  
  38.             throw e;  
  39.         }finally{  
  40.             if(session!=null)  
  41.                 session.close();  
  42.         }  
  43.     }  
  44.       
  45.     public static void delete(Object entity)  
  46.     {  
  47.         Session session=null;  
  48.         Transaction tx=null;  
  49.         try {  
  50.             session=HibernateUtils.getSession();  
  51.             tx=session.beginTransaction();  
  52.             session.delete(entity);  
  53.             tx.commit();  
  54.         } catch (HibernateException e) {  
  55.             throw e;  
  56.         }finally{  
  57.             if(session!=null)  
  58.                 session.close();  
  59.         }  
  60.     }  
  61.       
  62.     public static void update(Object entity)  
  63.     {  
  64.         Session session=null;  
  65.         Transaction tx=null;  
  66.         try {  
  67.             session=HibernateUtils.getSession();  
  68.             tx=session.beginTransaction();  
  69.             session.update(entity);  
  70.             tx.commit();  
  71.         } catch (HibernateException e) {  
  72.             throw e;  
  73.         }finally{  
  74.             if(session!=null)  
  75.                 session.close();  
  76.         }  
  77.     }  
  78.       
  79.       
  80.     public static Object get(Class clazz,Serializable id)  
  81.     {  
  82.         Session session=null;  
  83.         try {  
  84.             session=HibernateUtils.getSession();  
  85.             Object obj=session.get(clazz, id);  
  86.             return obj;  
  87.         } catch (HibernateException e) {  
  88.             throw e;  
  89.         }finally{  
  90.             if(session!=null)  
  91.                 session.close();  
  92.         }  
  93.     }  
  94.       
  95.     public static Object get(String name)  
  96.     {  
  97.         Session session=null;  
  98.         try {  
  99.             session=HibernateUtils.getSession();  
  100.             Query query=session.createQuery("from Student as student where name=:name");  
  101.             query.setParameter("name", name);  
  102.             Object obj=query.uniqueResult();  
  103.             return obj;  
  104.         } catch (HibernateException e) {  
  105.             throw e;  
  106.         }finally{  
  107.             if(session!=null)  
  108.                 session.close();  
  109.         }  
  110.     }  
  111. }  


 

 

HibernateTest.java
[java] view plain copy
  1. package com;  
  2.   
  3. import java.util.Date;  
  4.   
  5. import com.undergrowth.Student;  
  6. import com.undergrowth.imple.StudentDaoImp;  
  7.   
  8. public class HibernateTest {  
  9.   
  10.     /** 
  11.      * @param args 
  12.      */  
  13.     public static void main(String[] args) {  
  14.         // TODO Auto-generated method stub  
  15.         StudentDaoImp imp=new StudentDaoImp();  
  16.         //添加  
  17.         Student student=new Student("under"new Date(), "计算机");  
  18.         System.out.println(student);  
  19.         imp.saveStudent(student);  
  20.         //查询  
  21.         student=imp.findNameStudent("under");  
  22.         System.out.println(student);  
  23.         //修改  
  24.         student.setMajor("电信");  
  25.         imp.updateStudent(student);  
  26.         //查询  
  27.         student=imp.findNameStudent("under");  
  28.         System.out.println(student);  
  29.         //删除  
  30.         imp.deleteStudent(student);  
  31.           
  32.     }  
  33.   
  34. }  


 

打印的控制台信息

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值