最新的luna插件下載
安裝好有,在新建菜單裡應該有:
將下載好的hibernate中的/lib/required包裡的所有jar包都添加進去,連接數據庫的驅動也要添加進去.
xml裡寫上數據庫信息和一些設置
javaTest
123456
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/sample
org.hibernate.dialect.MySQLInnoDBDialect
true
true
update
5.新建 News 類 , get set , 構造函數等…
package com.qhn;
import java.sql.Date;
public class News {
private Integer id;
private String title;
private String author;
private 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;
}
@Override
public String toString() {
return "News [id=" + id + ", title=" + title + ", author=" + author
+ ", date=" + date + "]";
}
public News(String title, String author, Date date) {
super();
this.title = title;
this.author = author;
this.date = date;
}
public News() {
}
}
6.用插件生成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 May 14, 2015 8:33:47 PM by Hibernate Tools 3.4.0.CR1 --> <hibernate-mapping> <class name="com.qhn.News" table="NEWS"> <id name="id" type="java.lang.Integer"> <column name="ID" /> <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>
7. 建立 測試類 測試
package com.qhn;
import static org.junit.Assert.*;
import java.sql.Date;
import org.hibernate.*;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.Test;
@SuppressWarnings("deprecation")
public class HibernateTest {
@Test
public void test() {
//1. SessionFectory
SessionFactory sessionFactory = null;
Configuration configuration = new Configuration().configure();
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
.applySettings(configuration.getProperties())
.buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
//2. Sesson
Session session = sessionFactory.openSession();
//3. 開啟事務
Transaction transaction = session.beginTransaction();
//4. 執行保存操作
News news = new News("java","HI",new Date(new java.util.Date().getTime()));
session.save(news);
//5. 提交事物
transaction.commit();
//6. 關閉Session
session.close();
//7. 關閉SessionFectory
sessionFactory.close();
}
}
運行後可以在對應的數據庫中查到已經新建了對於的表,並且插入了數據.
最後整個工程的文件結構如下:
├── bin
│ ├── com
│ │ └── qhn
│ │ ├── HibernateTest.class
│ │ ├── News.class
│ │ └── News.hbm.xml
│ └── hibernate.cfg.xml
├── lib
│ ├── antlr-2.7.7.jar
│ ├── dom4j-1.6.1.jar
│ ├── hibernate-commons-annotations-4.0.5.Final.jar
│ ├── hibernate-core-4.3.9.Final.jar
│ ├── hibernate-jpa-2.1-api-1.0.0.Final.jar
│ ├── jandex-1.1.0.Final.jar
│ ├── javassist-3.18.1-GA.jar
│ ├── jboss-logging-3.1.3.GA.jar
│ ├── jboss-logging-annotations-1.2.0.Beta1.jar
│ ├── jboss-transaction-api_1.2_spec-1.0.0.Final.jar
│ └── mysql-connector-java-5.1.34-bin.jar
└── src
├── com
│ └── qhn
│ ├── HibernateTest.java
│ ├── News.hbm.xml
│ └── News.java
└── hibernate.cfg.xml