1.首先(F:\javasoft_chajian\)hibernate-release-4.2.4.Final\lib\required的jar包全部复制到项目的lib目录里面,同时bulid path加入路径中
2.在安装hibernatetools-Update-4.1.1.Final_2013-12-08_01-06-33-B605插件的基础上,在src下建立hibernate.cfg.xml文件,如:
一路next就行
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">csc</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql:///hibernate5</property>
<!-- 配置hibernate的基本信息 -->
<!-- hibernate 所使用的数据库方言 -->
<property name="dialect">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>
<!-- 指定关联的xxx.hbm.xml文件 -->
<mapping resource="cn/edu/sdut/hibernate/helloworld/News.hbm.xml"/>
</session-factory>
</hibernate-configuration>
注:数据库方言在F:\javasoft_chajian\hibernate-release-4.2.4.Final\project\etc下面的hibernate.properties里面能够找到。
注:如果hibernate.cfg.xml里面的http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd没有关联就不能提示相关信息。
关联方法:
点击add
将http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd复制到key里面key type选uri,路径是F:\LearnBook\javaweb\资料\开源框架jar包\SSH 框架\hibernate-release-4.2.4.Final\project\hibernate-core\src\main\resources\org\hibernate下面的hibernate-configuration-3.0.dtd
3.建立持久化类
package cn.edu.sdut.hibernate.helloworld;
import java.sql.Date;
public class News {
private Integer id;
private String title;
private String author;
private Date date;
public Date getDate() { return date;}
public void setDate(Date date) {this.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 News() {
super();
}
public News(String title, String author, Date date) {
super();
this.title = title;
this.author = author;
this.date = date;
}
@Override
public String toString() {
return "News [id=" + id + ", title=" + title + ", author=" + author + ", date=" + date + "]";
}
}
4.建立类关系映射文件(相应的此文件与相应的持久化类在同一个包中)
1).
一路next就行
xxxx.hbm.xml如: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 2016-10-26 10:37:48 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="cn.edu.sdut.hibernate.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>
5.编写访问数据库的代码
import java.sql.Date;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.Test;
import cn.edu.sdut.hibernate.helloworld.News;
public class HibernateTest {
@Test
public void test(){
//1.创建一个sessionFactory对象
SessionFactory sessionFactory = null;
try {
//1).创建configuration对象,configuration指的是hibernate的基本信息及对象关系映射信息
Configuration configuration = new Configuration().configure();
//2).创建serviceRegistry对象,任何Hibernate 的任何配置和服务都要在serviceRegistry配置之后才生效
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
//3)。创建一个sessionFactory对象
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
//2.创建一个session对象
Session session = sessionFactory.openSession();
//3.开启事务
Transaction transaction = session.beginTransaction();
//4.执行保存操作
News news = new News("java", "csc", new Date(new java.util.Date().getTime()));
session.save(news);
// News news = (News) session.get(News.class, 1);
// System.out.println(news);
// news.setAuthor("pppp");
// News news2 = (News) session.get(News.class, 1);
// System.out.println(news2);
//5.提交事务
transaction.commit();
//6.关闭session
session.close();
//7.关闭sessionFactory
sessionFactory.close();
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
小结:mysql数据库中只有hibernate5数据库,但是一开始没有NEWS表,这是根据hibernate生成的。