Hibernate开发第一个例子
?
1、首先下载以下文件:slf4j-1.5.8? hibernate-distribution-3.3.2.GA-dist
2、在myEclipse中建立hibernate 的User Libraries 库:
?? Window—Prefrerences—Java—Build Path—User Libraries
3、新建java项目 并引入自定义hibernate库以及操作mysql的驱动程序
?? |-hibernate3.jar
?? |-lib\required(全部)
?? |-slf4j-1.5.8\slf4j-1.5.8\slf4j-nop-1.5.8.jar
4、建立Student.java
package org.kevin.hibernate.model;
public class Student {
?? private int id;
?? private String name;
?? private String description;
?? public Student() {
?? }
?? 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 String getDescription() {
????? return description;
?? }
?? public void setDescription(String description) {
????? this.description = description;
?? }
}
5、建立模型的映射文件:
Student.hbm.xml
<?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 package="org.kevin.hibernate.model">
??? <class name="Student" table="Student">
?????? <id name="id" column="id">
?????????? <generator class="native" />
?????? </id>
?????? <property name="name" />
?????? <property name="description" />
??? </class>
</hibernate-mapping>
?
6、建立hibernate配置文件:hibernate.cfg.xml
<?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/hibernate</property>
??????? <property name="connection.username">root</property>
??????? <property name="connection.password">123456</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.cache.NoCacheProvider</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/kevin/hibernate/model/Student.hbm.xml"/>
?
??? </session-factory>
?
</hibernate-configuration>
7、建立测试文件
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.kevin.hibernate.model.Student;
?
public class StudentTest {
?
???????? public static void main(String[] args) {
?????????????????? Student s = new Student();
?????????????????? s.setId(2);
?????????????????? s.setName("kevin");
?????????????????? s.setDescription("description");
?
?????????????????? Configuration cfg = new Configuration();
?????????????????? SessionFactory sf = cfg.configure().buildSessionFactory();
?????????????????? Session session = sf.openSession();
?????????????????? session.beginTransaction();
?????????????????? session.save(s);
?????????????????? session.getTransaction().commit();
?????????????????? session.close();
???????? }
}
以上步骤在documentation中有,可参考!