在學Hibernate,它的第一個程序:講一個實例化的對象直接寫入數據庫中;
首先準備好環境,jdk1.7,myeclipse 8.5,mysql,等,其中下載好hibernate的包:
下載地址:http://sourceforge.net/projects/hibernate/files/?source=navbar 包括了hibernate開發的各種包。
1、創建普通的java項目
創建user library(window->preferences->java->build path->User Libraries),加入hibernate\lib下的所有jar包以及hibernate3.jar,還有jdbc驅動,以後添加hibernate所需的jar包只需添加這個lib即可。
在這個程序中需要導入的包有:
F:\hibernate\hibernate-distribution-3.3.2.GA\lib\required下面的全部lib包;F:\hibernate\hibernate-distribution-3.3.2.GA的hibernate3.jar包;F:\hibernate\slf4j-1.5.8的slf4j-nop-1.5.8.jar包,就可以了。
2.將數據庫驅動包通過build path -->add external archives導入到工程裏面。
3.在src下創建hibernate配置文件,hibernate.cfg.xml 參考F:\hibernate\hibernate-distribution- 3.3.2.GA\documentation\manual\zh-CN\html_single\index.html;
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<!-- JDBC connection pool (use the built-in) -->
<!-- <property name="connection.pool_size">1</property> -->
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<!-- <property name="current_session_context_class">thread</property> -->
<!-- Disable the second-level cache -->
<!-- <property name="cache.provider_class">org.hibernate.first.model.Student</property> -->
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<!-- <property name="hbm2ddl.auto">update</property> -->
<mapping resource="org/hibernate/first/model/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
4.定義實體類:
package org.hibernate.first.model;
public class Student {
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
5、定義User對象的映射文件User.hbm.xml,最好放在User類的包裏
內容如下:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!-- 默認表明與類名一致,可以通過設置class的table屬性來設置表明 -->
<class name="com.hk.hibernate.User">
<!--主鍵,自動生成唯一值,必須放在最前面 -->
<id name="id">
<generator class="uuid"/>
</id>
<property name="name"/>
<property name="age"/>
<property name="birth"/>
</class>
</hibernate-mapping>
6、將映射文件User.hbm.xml加入到hibernate.cfg.xml中
在hibernate.cfg.xml中</session-factory>前加入
<!-- 加入User.hbm.xml,注意路徑 -->
<mapping resource="com/hk/hibernate/User.hbm.xml"/>(注意,這裏是“/”,不是“.”)
7、創建數據庫hibernate和表:tb_student;
8、編寫測試類:
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.first.model.Student;
public class TestStudent {
/**
* @param args
* 測試第一個Hibernate的程序
*/
public static void main(String[] args) {
Student s = new Student();
s.setId(1);
s.setName("jim");
s.setAge(20);
Configuration cfg = new Configuration();
SessionFactory sessionFactory = cfg.configure().buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
session.save(s);
session.getTransaction().commit();
session.close();
sessionFactory.close();
}
}
9、大功告成!
2013-8-9 日,增加Annotation的方法寫第一條程序:
需要增加導入的包是:
程序跟上面不同的地方是:
Teacher.java
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity //表明是一個實體類
public class Teacher {
private int id;
private String name;
private String title;
@Id //主鍵
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
******省略其他屬性的Setter和Getter方法。******
TestTeacher.java中定義Configuration對象就不能像用xml文件一樣,應如下:
Configuration cfg = new AnnotationConfiguration();
其他不變
hibernate.cfg.xml
增加一句:
<mapping class="org.hibernate.first.model.Teacher"/>注意:間隔是 點
然後,完成另一種方式的一個helloworld程序。