我用的是MyEclipse,有些插件有,但還是要說一下,用Eclipse也是一樣的,先做一些前期工作,上面文章我們說過如何下載Hibernate插件,下載過來後我們解壓得到如圖:
將第二個ZIP文件安裝在Eclipse即可
這裏要說一下,安裝是下面兩個插件我這裏安裝出問題了,只安裝了下面兩個,然後建工程文件的時候不能點完成,建不了文件,後來我直接把如圖文件下的兩個目錄文件下的文件複製到了Eclipse的對應的文件名稱裏面就OK了。然後就TMD可以了。
接下來認識一下目錄文件如下圖
按圖上的順序建的文件
還有要導入JAR包與對應的數據庫JAR驅動
如圖
放在對應的lib目錄就行,因爲我這裏是WEB工程目錄,java工程也是翻放在lib目錄下,接下來就是右鍵-Build path加入所有JAR包到工程
好了,準備工作完成開始配置
首先我們建一個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>
<!-- 配置連接數據庫的基本信息 -->
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hibtest</property>
<!-- 配置hibernatenate 的基本信息 -->
<!-- hibernate 所使用的數據庫方言 -->
<property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<!-- 執行操作時是否在控制檯打印SQL -->
<property name="show_sql">true</property>
<!-- 是否對SQL進行格式化 -->
<property name="format_sql">true</property>
<!-- 指定自動生成數據表的策略 -->
<property name="hbm2ddl.auto">update</property>
<!-- 設置Hibernate 的事務隔離級別-->
<property name="connection.isolation">2</property>
<!-- 指定關聯的.hbm.xml文件 -->
<mapping resource="com/hib/helloworld/News.hbm.xml"/>
</session-factory>
</hibernate-configuration>
我這裏的數據庫方言用的是MySql5.1之後的,要不會出一些語法錯誤
接下來複制
http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd
左鍵-windos-preferences-XML-XML Catalog
然後選擇User Specified Entries點擊Add後如下圖
選擇圖片裏的.dtd文件與錄入上面的Http字符串即可,
這樣hibernate配置完成。
下面新建News.java類,代碼如下
package com.hib.helloworld;
import java.sql.Date;
public class News {
private Integer id;
private String title;
private String author;
private Date date;
public News() {
}
public News(String title, String author, Date date) {
this.title = title;
this.author = author;
this.date = date;
}
@Override
public String toString() {
return "News [id=" + id + ", title=" + title + ", author=" + author + ", date=" + date + "]";
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
然後新建News.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">
<!-- Generated 2017-7-17 17:38:39 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.hib.helloworld.News" table="NEWS">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<!-- 指定主鍵的生成方式,native:使用數據庫本地的方式-->
<generator class="native" />
</id>
<property name="title" type="java.lang.String">
<column name="TITLE" />
</property>
<property name="author" type="java.lang.String">
<column name="AUTHOR" />
</property>
<property name="date" type="java.sql.Date">
<column name="DATE" />
</property>
</class>
</hibernate-mapping>
然後新建測試類HibTest.java,代碼如下
package test;
import java.sql.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
import com.hib.helloworld.News;
public class HibTest {
public void init(){
}
public void destore(){
}
@Test
public void test() {
//1.創建一個SessionFactory 對像
SessionFactory sessionFactory = null;
//1).創建Configuration對象:對應hibernate的基本配置信息和對象關係映射信息
Configuration configuration = new Configuration().configure();
//2).創建一個ServiceRegistry 對象:hibernate 4.x新添加的對象
//hibernate的任何配置和服務都需要在該對象中註冊後纔能有效
StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder().configure().build();
//3).創建會話工廠對象
sessionFactory = configuration.buildSessionFactory(standardRegistry);
//2.創建一個Session對象
Session session = sessionFactory.openSession();
//3.開啓事物
Transaction transaction = session.beginTransaction();
//4.執行保存操作
News news = new News("Java","ATGUGI",new Date(new java.util.Date().getTime()));
session.save(news);
//5.提交事物
transaction.commit();
//6.關閉Session
session.close();
//7.關閉SessionFactory
sessionFactory.close();
}
}
在數據庫建一個對應的datebase即可
運行可以看到數據庫中自建了一張表,而且還有數據。