最新的luna插件下载地址在:http://download.jboss.org/jbosstools/updates/stable/luna/
安装好有,在新建菜单里应该有:
将下载好的hibernate中的/lib/required包里的所有jar包都添加进去,连接数据库的驱动也要添加进去.
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">javaTest</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/sample</property>
<!-- 配置hibernate 基本信息 -->
<!-- 方言 -->
<property name="dialetc">org.hibernate.dialect.MySQLInnoDBDialect</property>
<!-- 打印sql -->
<property name="show_sql">true</property>
<!-- sql格式化 -->
<property name="format_sql">true</property>
<!-- 生成数据表策略 -->
<property name="hbm2ddl.auto">update</property>
<!-- 关联的hbm.xml文件 -->
<mapping resource="com/qhn/News.hbm.xml"/>
</session-factory>
</hibernate-configuration>
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() {
}
}
<?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>
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
原文地址:http://blog.csdn.net/ck_boss/article/details/45745873