Hibernate-V 在其他项目中加入 Hibernate

Step 1

在 pom.xml 中添加hibernate和mysql依赖

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.4.30.Final</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.25</version>
        </dependency>

Step 2

在idea的Database选择卡中添加需要连接的数据库
Hibernate-V 在其他项目中加入 Hibernate

Step 3

编写hibernate.cfg.xml,并将其放入resources文件夹中

<?xml version="1.0" encoding="utf-8"?>
<hibernate-configuration>
    <session-factory>
        <property name="dialect">org.hibernate.dialect.MySQL8Dialect</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.username">root</property>
        <property name="connection.password">19980411</property>
        <property name="show_sql">true</property>

        <mapping resource="UserEntity.hbm.xml"/>
        <mapping class="com.example.hiber.entity.UserEntity"/>
    </session-factory>
</hibernate-configuration>

Step 4

使用Persistence工具,直接根据数据库表生成xml映射文件和实体类。
Hibernate-V 在其他项目中加入 Hibernate

上一篇:Hibernate对象识别,重写hashcode和equals方法


下一篇:eclipse学习(第三章:ssh中的Hibernate)——4.Hibernate中的映射文件及映射类型