IDEA配置Hibernate以及自动生成Hibernate映射文件以及实体类

一、创建好的目录结构

  • 如下图:

在这里插入图片描述
二、具体步骤
(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();
        }

    }
}

  • 运行结果
    在这里插入图片描述
  • 1
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
IDEA 项目找到 Hibernate 映射文件的步骤如下: 1. 打开项目,找到 Hibernate配置文件 `hibernate.cfg.xml`,该文件通常位于项目的`src/main/resources`目录下。 2. 打开 `hibernate.cfg.xml` 文件,查看 `mapping` 标签是否包含了映射文件配置,例如: ```xml <mapping class="com.example.entity.User"/> <mapping class="com.example.entity.Order"/> ``` 上面的配置表示,Hibernate 使用注解方式映射了 `com.example.entity` 包下的 `User` 和 `Order` 两个实体类。 3. 如果 Hibernate 使用的是 XML 映射文件,那么可以在 `mapping` 标签使用 `resource` 属性指定映射文件的路径,例如: ```xml <mapping resource="com/example/entity/User.hbm.xml"/> <mapping resource="com/example/entity/Order.hbm.xml"/> ``` 上面的配置表示,Hibernate 使用 XML 映射文件映射 `com.example.entity` 包下的 `User` 和 `Order` 两个实体类。 4. 如果无法在 `hibernate.cfg.xml` 文件找到映射文件配置,那么可以在项目搜索包含实体类文件,例如,在 IDEA 可以使用 `Ctrl+Shift+N` 快捷键打开搜索框,然后输入实体类的名称,查找包含该实体类文件,通常映射文件的名称和实体类的名称相同,只是后缀名不同,例如 `User.java` 对应的映射文件名为 `User.hbm.xml` 或 `User.orm.xml` 等。 总之,根据项目的实际情况,可以选择不同的方式来查找和管理 Hibernate映射文件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值