--------------------------------------------- 多看文档,对身体好 ------------------------------------------------------------------------------
事先准备
Hibernate-3.6.3(hibernate-distribution-3.6.3.Final-dist.zip)的压缩包 地址: www.hibernate.org
slf4j-1.6.1(slf4j-1.6.1.zip)的压缩包 地址:http://www.slf4j.org/download.html ----我在CSDN也上传了一份
我建立的是一个JavaProject。
1.新建一个Java工程
2.导入hibernate所需要的各种包
hibernate3.jar------------------------------Hibernate核心包
antlr-2.7.6.jar-------------------------------Hibernate必须的包
commons-collections-3.1.jar--------------------------Hibernate必须的包
dom4j-1.6.1.jar--------------------------Hibernate必须的包
javassist-3.12.0.GA.jar--------------------------Hibernate必须的包
jta-1.1.jar--------------------------Hibernate必须的包
slf4j-api-1.6.1.jar--------------------------Hibernate必须的包(slf4j的api包)
hibernate-jpa-2.0-api-1.0.0.Final.jar--Jpa需要的包(不要可能会报错)
slf4j-nop-1.6.1.jar-------------------------slf4j的实现包
(嗯,以上的差不多了,如果需要annotation的话,还需要下载annotation的包---这里不解释了==谷歌+电驴)
3.查文档创建我的第一个Hibernate实例——HelloWord(文档的原例)
3.1接下来我们创建一个类,用来代表那些我们希望储存在数据库里的 event,这是一个具有一些属性的简单 JavaBean 类:
package org . hibernate . tutorial . domain ; import java . util . Date ; public class Event { private Long id ; private String title ; private Date date ; public Event () {} public Long getId () { return id ; } private void setId ( Long id ) { this . id = id ; } public Date getDate () { return date ; } public void setDate ( Date date ) { this . date = date ; } public String getTitle () { return title ; } public void setTitle ( String title ) { this . title = title ; } }
把这个文件保存到 src/main/java/org/hibernate/tutorial/domain
目录下。
3.2映射文件
下面是一个完整的映射文件----------名字可以自己设置*.hbm.xml一般都是这个格式
<?xml version="1.0"?> < ! DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" > < hibernate-mapping package = "org.hibernate.tutorial.domain" > [...] </ hibernate-mapping >
< hibernate-mapping package = "org.hibernate.tutorial.domain" > < class name = "Event" table = "EVENTS" > < id name = "id" column = "EVENT_ID" > < generator class = "native" /> </ id > < property name = "date" type = "timestamp" column = "EVENT_DATE" /> < property name = "title" /> </ class > </ hibernate-mapping >
当实体类的属性名和数据库表的字段名一样的时候,我们就可以省去column=“column_name” 可以直接这么写
< property name = "title" />
name属性表表示实体的属性名字 column表示字段名字
3.3Hibernate的配置文件
下面是一个配置文件的示例hibernate.cfg.xml(名字最好是这个,免得需改,或者出一些没必要的错误)----在src目录下面建立
<?xml version='1.0' encoding='utf-8'?> < ! DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd" > <!--需要注意的是这里的和前面的映射文件不一致,
最好是copy文档--> < hibernate-configuration > < session-factory > <!-- Database connection settings --> < property name = "connection.driver_class" > org.hsqldb.jdbcDriver </ property > < property name = "connection.url" > jdbc:hsqldb:hsql://localhost </ property > < property name = "connection.username"> sa </ property > < property name = "connection.password" ></ property >
<!--以上都是一些数据库的配置,相信大家都不陌生--> <!-- JDBC connection pool (use the built-in) --> < property name = "connection.pool_size" > 1 </ property > <!--这里是Hibernate数据库连接池的设置,可以不用--> <!-- SQL dialect --> < property name = "dialect" > org.hibernate.dialect.HSQLDialect </ property >
<!--这里是Hibernate的方言,每种数据库的都不一样,应为本人比较喜欢MySQL
所以用org.hibernate.dialect.MySQLDialect这句-->
<!-- 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 >
<!--是否显示SQL语句--> <!-- Drop and re-create the database schema on startup --> < property name = "hbm2ddl.auto" > update </ property >
<!--SQL DDL语句的类型--> < mapping resource = "org/hibernate/tutorial/domain/Event.hbm.xml" />
<!--mapping 很重要, 就是映射文件的路径名--> </ session-factory > </ hibernate-configuration >
3.4测试Hibernate--Helloworld
User user = new User();
user.setId(25);
user.setUsername("xwl617");
user.setEmail("xwl@xwl.com");
user.setLocation("CS");
user.setPassword("123456");
user.setQuestion("asdsa?");
user.setAnswer("asa");
//添加属性
Configuration cfg = new Configuration();
SessionFactory sf=cfg.configure().buildSessionFactory();
Session session = sf.openSession();//得到session此处的session为org.hibernate.Session包下面的;
session.beginTransaction();//开始传送
session.save(user);//保存对象
session.getTransaction().commit();//提交对象
session.close();//关闭资源
sf.close();
----------------------------------多看文档,此处的SessionFactory最好是弄成单例模式,免得以后总是加载---------------------------------
前面是原理分析,和注意事项
下面是我的列子
-src
-package:com.xwl.hibernate.entity
-class:User.java
-xml:User.hbm.xml
-package:com.xwl.hibernate.main
-class:Test.java
-xml:hibernate.cfg.xml
User.java
package com.xwl.hibernate.entity;
public class User {
private int id;
private String username;
private String password;
private String email;
private String location;
private String question;
private String answer;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
public String getAnswer() {
return answer;
}
public void setAnswer(String answer) {
this.answer = answer;
}
}
User.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.xwl.hibernate.entity">
<class name="User" table="user">
<id name="id" column="Id"></id>
<property name="username" column="_name">
</property>
<property name="password"></property>
<property name="email"></property>
<property name="location"></property>
<property name="question"></property>
<property name="answer"></property>
</class>
</hibernate-mapping>
Test.java
package com.xwl.hibernate.main;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.xwl.hibernate.entity.User;
public class Test {
public static void main(String[] args) {
User user = new User();
user.setId(25);
user.setUsername("xwl617");
user.setEmail("xwl@xwl.com");
user.setLocation("CS");
user.setPassword("123456");
user.setQuestion("asdsa?");
user.setAnswer("asa");
Configuration cfg = new Configuration();
SessionFactory sf=cfg.configure().buildSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
session.save(user);
session.getTransaction().commit();
System.out.println("commit");
session.close();
sf.close();
}
}
hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/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/test</property>
<property name="connection.username">root</property>
<property name="connection.password">123</property>
<!-- JDBC connection pool (use the built-in) --><!-- database pool -->
<!-- <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="com/xwl/hibernate/entity/User.hbm.xml" />
</session-factory>
</hibernate-configuration>