hibernate环境搭建(Hello world 配置文件版)

来源:互联网 发布:mac osx系统下载 编辑:程序博客网 时间:2024/05/21 14:53

1.下载hibernate jar包:hibernate-release-4.3.5.Final,导入必要的jar包,路径为:hibernate-release-4.3.5.Final\lib\required。

包含的jar包有10个。

2.建立新的java项目。

3.学习自己建立User Library: 

  (a)项目右键——build path——configure build path——add library.

  (b)选择User-library,在其中新建library,命名为hibernate。

  (c)在library中加入hibernate所需要的jar包(路径为:hibernate-release-4.3.5.Final\lib\required),hello world就够了,其他的还要加。

4.引入数据库的jdbc驱动。我用的mysql:mysql-connector-java-5.1.7-bin.jar

(a)创建数据库:create  database hibernate;

  (b)切换数据库:use hibernate;

       (c)创建Student表:create table Student(id int  primary key, name varchar(20),age int);

5.建立hibernate的配置文件hibernate.cfg.xml,强烈建议在hibernate-release-4.3.5.Final\documentation\manual\en-US\html_single路径下的帮助文档中copy。

地点:1.1.4. Hibernate configuration。 内容修改后:

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?xml version='1.0' encoding='utf-8'?>  
  2. <!DOCTYPE hibernate-configuration PUBLIC  
  3.         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
  4.         "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">  
  5.   
  6. <hibernate-configuration>  
  7.   
  8.     <session-factory>  
  9.   
  10.         <!-- Database connection settings -->  
  11.         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>  
  12.         <property name="connection.url">jdbc:mysql://localhost/hibernate</property>  
  13.         <property name="connection.username">XXX</property>  
  14.         <property name="connection.password">XXXX</property>  
  15.   
  16.         <!-- JDBC connection pool (use the built-in) -->  
  17.         <!--  
  18.         <property name="connection.pool_size">1</property> 
  19.          -->  
  20.         <!-- SQL dialect -->  
  21.         <property name="dialect">org.hibernate.dialect.MySQLDialect</property>  
  22.   
  23.         <!-- Enable Hibernate's automatic session context management -->  
  24.         <property name="current_session_context_class">thread</property>  
  25.   
  26.         <!-- Disable the second-level cache  -->  
  27.         <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>  
  28.   
  29.         <!-- Echo all executed SQL to stdout -->  
  30.         <property name="show_sql">true</property>  
  31.   
  32.         <!-- Drop and re-create the database schema on startup -->  
  33.         <!--  
  34.         <property name="hbm2ddl.auto">update</property> 
  35.         -->  
  36.         <mapping resource="com/huxing/hibernate/model/Student.hbm.xml"/>  
  37.   
  38.     </session-factory>  
  39.   
  40. </hibernate-configuration>  
建立Student类:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. public class Student {  
  2.      private int id;  
  3.      private String name;  
  4.      private int age;  
  5.     public int getId() {  
  6.         return id;  
  7.     }  
  8.     public void setId(int id) {  
  9.         this.id = id;  
  10.     }  
  11.     public String getName() {  
  12.         return name;  
  13.     }  
  14.     public void setName(String name) {  
  15.         this.name = name;  
  16.     }  
  17.     public int getAge() {  
  18.         return age;  
  19.     }  
  20.     public void setAge(int age) {  
  21.         this.age = age;  
  22.     }  
  23.        
  24. }  
建立Student的映射文件:Student.hbm.xml
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  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.huxing.hibernate.model">  
  7.   
  8.     <class name="Student" table="student">  
  9.         <id name="id" column="id">  
  10.         </id>  
  11.         <property name="name" type="string"  column="name"/>  
  12.         <property name="age"  type="int" column="age"/>  
  13.     </class>  
  14.   
  15. </hibernate-mapping>  

最后测试:

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. import org.hibernate.Session;  
  2. import org.hibernate.SessionFactory;  
  3. import org.hibernate.cfg.Configuration;  
  4.   
  5. import com.huxing.hibernate.model.Student;  
  6.   
  7. public class StudentTest {  
  8.     public static void main(String[] args) {  
  9.         Student a = new Student();  
  10.         a.setId(123);  
  11.         a.setAge(32);  
  12.         a.setName("hello hibernate!");  
  13.           
  14.         Configuration cfg = new Configuration();  
  15.         SessionFactory cf = cfg.configure().buildSessionFactory();  
  16.         Session session = cf.openSession();  
  17.         session.beginTransaction();  
  18.         session.save(a);  
  19.         session.getTransaction().commit();  
  20.         session.close();  
  21.         cf.close();  
  22.     }  
  23. }  
0 0