各個軟體可在各自的官網中尋找,這裡為了友善放上自己百度雲的連結,
在學習hibernate的過程中,發現因為各種軟體版本的問題,一直都很難找到一個完全符合自己的教程,
最後在檢視多個教程之後,本人在win8.1上配置運作成功,
給自己也給别人留下一個參考,這裡給出本人參考較多的一個教程:http://blog.csdn.net/aboy123/article/details/10085635 ,
感謝這位部落客的分享,讓我少走許多彎路@aboy123 ,這位部落客的教程中的版本與這裡的并不一樣,本人在配置的時候基本采用的是新的版本了,
并且對其中的一些代碼進行了修改,
另:在下面hibernate的壓縮包中有hibernate的一些參考代碼和執行個體(遇到問題可參考裡面的寫法)
eclipse版本:eclipse-jee-mars-R-win32-x86_64(http://pan.baidu.com/s/1hqiyahe)
hibernate版本:hibernate-release-4.3.10.Final (http://pan.baidu.com/s/1qWDVL5U)
mysql版本:mysql-installer-community-5.6.26.0 (http://pan.baidu.com/s/1lYaJW)
mysql的jdbc驅動版本:mysql-connector-java-5.1.36(http://pan.baidu.com/s/15AQZW)
1、先将上述軟體下載下傳,解壓,隻有mysql需要點選安裝
2、運作eclipse,建立Java Project,将hibernate lib/required目錄下所有的jar包和mysql的jdbc驅動中的mysql-connector-java-5.1.36-bin.jar 加入lib目錄
3、編寫hibernate.cfg.xml檔案,将該檔案放置到src目錄下,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>
<!-- mysql資料庫驅動 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- mysql資料庫名稱 -->
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_first</property>
<!-- 資料庫的登陸使用者名 -->
<property name="hibernate.connection.username">root</property>
<!-- 資料庫的登陸密碼 -->
<property name="hibernate.connection.password"></property>
<!-- 方言:為每一種資料庫提供擴充卡,友善轉換 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping resource="com/example/hibernate/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
這裡資料庫的登陸使用者名和密碼為自己mysql的使用者名和密碼,本人的使用者名root,無密碼,在運作程式之前要保證mysql中已經建立了hibernate_first這一資料庫
4、在src添加包,本人添加的包名為:com.example.hibernate.(注意與代碼應用一緻即可)
5、在com.example.hibernate中添加User類,User類代碼如下:
package com.example.hibernate;
import java.util.Date;
public class User {
private String id;
private String username;
private String password;
private Date createTime;
private Date expireTime;
public String getId() {
return id;
}
public void setId(String 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 Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getExpireTime() {
return expireTime;
}
public void setExpireTime(Date expireTime) {
this.expireTime = expireTime;
}
}
6、編寫User.hbm.xml,完成實體類映射,将其放置到com.example.hibernate中,即與User類同一目錄下,User.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>
<class name="com.example.hibernate.User">
<id name="id">
<generator class="uuid"/>
</id>
<property name="username"/>
<property name="password"/>
<property name="createTime"/>
<property name="expireTime"/>
</class>
</hibernate-mapping>
其中的property标簽是将要生成是資料庫表中的字段,在這裡不用關心各個字段是什麼類型的。因為Hibernate會根據上面的實體類中屬性的類型來決定将來表中字段的類型,可以要可以注意到,這裡User.hbm.xml的位置與第3個步驟中hibernate.cfg.xml檔案中的<mapping resource="com/example/hibernate/User.hbm.xml"/>是要保持一緻的。
7、生成表:編寫工具類ExoprtDB.java,将hbm生成ddl(與User類同一目錄),ExoprtDB.java内容如下:
package com.example.hibernate;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class ExoprtDB {
public static void main(String[] args) {
//預設讀取hibernate.cfg.xml檔案
Configuration cfr = new Configuration().configure();
SchemaExport export = new SchemaExport(cfr);
export.create(true, true);
}
}
8、到這裡就可以生成User表了,但是如果直接運作ExoprtDB.java檔案是不能生成User表的。因為在mysql資料中還沒有建立資料庫Hibernate-first。是以在mysql控制台中通過create database hibernate-first; use hibernate-first;之後再執行ExoprtDB.java檔案就可以生成表了。
9、最後是向表中添加資料的例子,編寫Client類(與User類同一目錄),Client.java檔案内容如下:
package com.example.hibernate;
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class Client {
public static void main(String[] args) {
//讀取配置檔案
Configuration cfg = new Configuration().configure();
SessionFactory factory = cfg.buildSessionFactory();
Session session = null;
try{
session = factory.openSession();
//開啟事務
session.beginTransaction();
User user = new User();
user.setUsername("username");
user.setPassword("123");
user.setCreateTime(new Date());
user.setExpireTime(new Date());
session.save(user);
//送出事務
session.getTransaction().commit();
}catch(Exception e){
e.printStackTrace();
//復原事務
session.getTransaction().rollback();
}finally{
if(session != null){
if(session.isOpen()){
//關閉session
session.close();
}
}
}
}
}