一、创建好的目录结构
- 如下图:
二、具体步骤
(1)创建一个项目:以创建一个web项目为例
打开IDEA,选择File > new > project ,左侧选择java,勾选Web Application + Hibernate ,同时勾选 ”Create default hibernate configuration and main class” (当然也可以不勾选,在项目创建好后再手动创建也是一样的) 。
具体操作见下图:
(2)手动添加数据库驱动包和数据库缓冲池的jar包。
- 添加下面两个包
- 添加过程
复制下载好的要添加的数据库驱动包 > 粘贴到 lib文件目录下 > 选中刚添加的 jar包 > 右键选择Add as Libtary。(添加完成)。 - 得到如图所示的目录结构
(3)连接MySql数据库(也可以是别的数据库)
如果电脑没有安装数据库可以点击下面连接安装数据库:
点击查看数据库安装步骤 - 找到Database,点击如图所示 :
- 填写Database(数据库名)、User(用户名)、Password(当前用户密码) 在前三步完成后,可以点击测试Test Connection显示成功后再点击Apply
(4)修改hibernate.cfg.xml。 - 代码如下所示:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.url">jdbc:mysql://localhost:3306/java12</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<!-- DB schema will be updated if needed -->
<property name="hbm2ddl.auto">update</property>
<property name="format_sql">true</property>
</session-factory>
</hibernate-configuration>
- 注意:下边这两行代码是自动生成的,在上边代码中不用写入。
<!--先不用写,下面两行是自动生成的自动生成的-->
<mapping resource="com/FurnituresEntity.hbm.xml"/>
<mapping class="com.FurnituresEntity"/>
(5)自动生成Hibernate的实体类以及配置文件
- 点击窗口中的Persistence
- 在Persistence中右键项目,然后点击Generate Persistence Mapping,选择By Database Schema
- 选择数据源,配置实体类包,选择要生成的实体类(其中日期类型的只能手动修改为java.util.Date),然后点击OK
(6)生成的实体类和配置文件如下:
- 实体类FurnituresEntity .java
package com;
import javax.persistence.*;
import java.util.Objects;
@Entity
@Table(name = "furnitures", schema = "java12", catalog = "")
public class FurnituresEntity {
private int id;
private String name;
private Integer price;
private Integer num;
private String dates;
private String style;
@Id
@Column(name = "id")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Basic
@Column(name = "name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Basic
@Column(name = "price")
public Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this.price = price;
}
@Basic
@Column(name = "num")
public Integer getNum() {
return num;
}
public void setNum(Integer num) {
this.num = num;
}
@Basic
@Column(name = "dates")
public String getDates() {
return dates;
}
public void setDates(String dates) {
this.dates = dates;
}
@Basic
@Column(name = "style")
public String getStyle() {
return style;
}
public void setStyle(String style) {
this.style = style;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
FurnituresEntity that = (FurnituresEntity) o;
return id == that.id &&
Objects.equals(name, that.name) &&
Objects.equals(price, that.price) &&
Objects.equals(num, that.num) &&
Objects.equals(dates, that.dates) &&
Objects.equals(style, that.style);
}
@Override
public int hashCode() {
return Objects.hash(id, name, price, num, dates, style);
}
}
- 配置文件FurnituresEntity .hbm.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.FurnituresEntity" table="furnitures" schema="java12">
<id name="id" column="id"/>
<property name="name" column="name"/>
<property name="price" column="price"/>
<property name="num" column="num"/>
<property name="dates" column="dates"/>
<property name="style" column="style"/>
</class>
</hibernate-mapping>
三、编写测试类
- Test.java
package com;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class Test {
public static void main(String[] args) {
Configuration cfg=null;
SessionFactory sf=null;
Session session=null;
Transaction ts=null;
FurnituresEntity furnituresEntity=new FurnituresEntity();
furnituresEntity.setName("鞋架");
furnituresEntity.setPrice(200);
furnituresEntity.setStyle("欧式风格");
furnituresEntity.setDates("2020-02-14");
furnituresEntity.setNum(35);
try {
cfg = new Configuration().configure();
session = cfg.buildSessionFactory().openSession();
ts = session.beginTransaction();
session.save(furnituresEntity);
//输出测试
System.out.println("11111");
ts.commit();
}
catch (Exception e)
{
if(ts!=null)
{
ts.rollback();
}
e.printStackTrace();
}
finally {
session.close();
}
}
}
- 运行结果