1.先建一个maven工程,在pom.xml中配置依赖
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- 添加Hibernate依赖 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.2.4.Final</version>
</dependency>
<!-- mysql数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.32</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
2.打开视图(先要下载hibernate-release-5.2.10.Final.zip,版本看软件)
选择window,show view,other。

这个工具打开方式是选择window,perspective,customize perspective......

3.自动生成代码
右键空白点击Add Configuration
选择我们的项目
new一个数据库连接,选择使用的数据库,写个名字
下一步
配置文件
new一个配置文件
下一步
4.生成映射文件
点击 Hibernate Code Generation Configurations....
创建
文件的生成规则,点击Next
下一步
再点击Exporters
生成的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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/guestbook?useUnicode=true&characterEncoding=UTF8</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- 是否显示sql语句 -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hibernate.current_session_context_class">thread</property>
<mapping resource="com/ex/demo/Guestbook.hbm.xml" />
</session-factory>
</hibernate-configuration>
5.测试插入一条数据
public class App
{
public static void main( String[] args )
{
// 1. 加载Hibernate配置文件。默认查找 资源目录下的 hibernate.cfg.xml
Configuration config = new Configuration();
config.configure();
// 拿到 session 的工厂对象,用于去创建 session 会话
SessionFactory sessionFactory = config.buildSessionFactory();
// 获取到会话对象
Session session = sessionFactory.getCurrentSession();
// 2. 启动事务
Transaction tx = session.beginTransaction();
// Java中创建对象
Goflow gb = new Goflow();
gb.setName("hj");
gb.setPhone("13552111");
gb.setEmail("hj@163.com");
gb.setTitle("nihao");
gb.setContent("wsand");
gb.setCreatedTime(new Date());
//
// 利用 会话对象, 保存到数据库中
session.save(gb);
// 3. 提交到数据库中去
tx.commit();
sessionFactory.close();
}
}