【hibernate】idea利用maven搭建hibernate环境(创建hibernate配置文件(包括cfg和hbm))


在开始学习hibernate的时候,使用eclipse通过引入jar包的方式创建,又要导jar包又要导约束非常的麻烦。
今天我们利用idea和maven来创建一个hibernate框架。

1.准备

1、idea。
2、mysql

2. 步骤

2-1 在idea中创建一个mavne项目。

【hibernate】idea利用maven搭建hibernate环境(创建hibernate配置文件(包括cfg和hbm))
【hibernate】idea利用maven搭建hibernate环境(创建hibernate配置文件(包括cfg和hbm))
【hibernate】idea利用maven搭建hibernate环境(创建hibernate配置文件(包括cfg和hbm))

2-2 引入jar包

在创建好的项目中的pom文件中,引入hibernate和mysql的jar。
我们可以在maven仓库(https://mvnrepository.com/ )查询最近的hibernate引用的jar。

 <dependencies>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.3.2.Final</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.15</version>
        </dependency>
       
    </dependencies>

2-3 利用idea插件,创建hibernate配置文件(包括cfg和hbm)

2-3-1 添加hibernate.cfg.xml 文件

打开idea中的project structure (ctrl + alt + shift +s)
【hibernate】idea利用maven搭建hibernate环境(创建hibernate配置文件(包括cfg和hbm))
点击加号,添加hibernate
【hibernate】idea利用maven搭建hibernate环境(创建hibernate配置文件(包括cfg和hbm))

【hibernate】idea利用maven搭建hibernate环境(创建hibernate配置文件(包括cfg和hbm))
到此添加成功,点击应用即可。

2-3-2 自动生成配置文件 *.hbm.xml 文件

连接mysql数据库
【hibernate】idea利用maven搭建hibernate环境(创建hibernate配置文件(包括cfg和hbm))
【hibernate】idea利用maven搭建hibernate环境(创建hibernate配置文件(包括cfg和hbm))
下载myqsl驱动
【hibernate】idea利用maven搭建hibernate环境(创建hibernate配置文件(包括cfg和hbm))
【hibernate】idea利用maven搭建hibernate环境(创建hibernate配置文件(包括cfg和hbm))

测试连接成功,既可以打开了。
【hibernate】idea利用maven搭建hibernate环境(创建hibernate配置文件(包括cfg和hbm))
自动生成配置文件和实体。
【hibernate】idea利用maven搭建hibernate环境(创建hibernate配置文件(包括cfg和hbm))
【hibernate】idea利用maven搭建hibernate环境(创建hibernate配置文件(包括cfg和hbm))
【hibernate】idea利用maven搭建hibernate环境(创建hibernate配置文件(包括cfg和hbm))
【hibernate】idea利用maven搭建hibernate环境(创建hibernate配置文件(包括cfg和hbm))
【hibernate】idea利用maven搭建hibernate环境(创建hibernate配置文件(包括cfg和hbm))
【hibernate】idea利用maven搭建hibernate环境(创建hibernate配置文件(包括cfg和hbm))
【hibernate】idea利用maven搭建hibernate环境(创建hibernate配置文件(包括cfg和hbm))
【hibernate】idea利用maven搭建hibernate环境(创建hibernate配置文件(包括cfg和hbm))
到此,配置文件已经生成完毕。下面我们 进入开发测试。

2-3 配置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/pinyougoudb</property>
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.username">root</property>
    <property name="connection.password">root</property>  
    
    <!--自动生成的实体和hbm.xml 文件位置。-->
      <mapping resource="entity/TbAddressEntity.hbm.xml"/>
      <mapping class="entity.TbAddressEntity"/>
      <mapping class="entity.TbAreasEntity"/>
      <mapping resource="entity/TbAreasEntity.hbm.xml"/>
      <mapping class="entity.TbBrandEntity"/>
      <mapping resource="entity/TbBrandEntity.hbm.xml"/>
      <mapping class="entity.TbCitiesEntity"/>
      <mapping resource="entity/TbCitiesEntity.hbm.xml"/>
      <mapping class="entity.TbContentEntity"/>
      <mapping resource="entity/TbContentEntity.hbm.xml"/>
      <mapping resource="entity/TbContentCategoryEntity.hbm.xml"/>
      <mapping class="entity.TbContentCategoryEntity"/>
      <mapping resource="entity/TbFreightTemplateEntity.hbm.xml"/>
      <mapping class="entity.TbFreightTemplateEntity"/>
      <mapping resource="entity/TbGoodsEntity.hbm.xml"/>
      <mapping class="entity.TbGoodsEntity"/>
      <mapping class="entity.TbGoodsDescEntity"/>
      <mapping resource="entity/TbGoodsDescEntity.hbm.xml"/>
      <mapping resource="entity/TbItemEntity.hbm.xml"/>
      <mapping class="entity.TbItemEntity"/>
      <mapping class="entity.TbItemCatEntity"/>
      <mapping resource="entity/TbItemCatEntity.hbm.xml"/>
      <mapping class="entity.TbOrderEntity"/>
      <mapping resource="entity/TbOrderEntity.hbm.xml"/>
      <mapping resource="entity/TbOrderItemEntity.hbm.xml"/>
      <mapping class="entity.TbOrderItemEntity"/>
      <mapping class="entity.TbPayLogEntity"/>
      <mapping resource="entity/TbPayLogEntity.hbm.xml"/>
      <mapping class="entity.TbProvincesEntity"/>
      <mapping resource="entity/TbProvincesEntity.hbm.xml"/>
      <mapping resource="entity/TbSeckillGoodsEntity.hbm.xml"/>
      <mapping class="entity.TbSeckillGoodsEntity"/>
      <mapping class="entity.TbSeckillOrderEntity"/>
      <mapping resource="entity/TbSeckillOrderEntity.hbm.xml"/>
      <mapping resource="entity/TbSellerEntity.hbm.xml"/>
      <mapping class="entity.TbSellerEntity"/>
      <mapping resource="entity/TbSpecificationEntity.hbm.xml"/>
      <mapping class="entity.TbSpecificationEntity"/>
      <mapping class="entity.TbSpecificationOptionEntity"/>
      <mapping resource="entity/TbSpecificationOptionEntity.hbm.xml"/>
      <mapping resource="entity/TbTypeTemplateEntity.hbm.xml"/>
      <mapping class="entity.TbTypeTemplateEntity"/>
      <mapping class="entity.TbUserEntity"/>
      <mapping resource="entity/TbUserEntity.hbm.xml"/>
      <!-- <property name="connection.username"/> -->
    <!-- <property name="connection.password"/> -->

    <!-- DB schema will be updated if needed -->
    <!-- <property name="hbm2ddl.auto">update</property> -->
  </session-factory>
</hibernate-configuration>

3 测试

向数据库表中插入一下数据。

package testmain;

import entity.TbBrandEntity;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;

public class test {

    @Test
    public void fun1() {

        Configuration conf = new Configuration().configure();

        SessionFactory sessionFactory = conf.buildSessionFactory();

        Session session = sessionFactory.openSession();

        Transaction tx = session.beginTransaction();
/******************************************************/
        TbBrandEntity brand=new TbBrandEntity();
        brand.setFirstChar("d");
//        brand.setId((long) 231);
        brand.setName("大牌");
        session.save(brand);
/******************************************************/
        tx.commit();
        session.close();
        sessionFactory.close();
        System.out.println(12312312);

    }
    
}

运行测试方法。
报错信息

C:\tools\Java\jdk1.8.0_201\bin\java.exe -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:C:\tools\JetBrains\IntelliJ IDEA 2018.3.4\lib\idea_rt.jar=1660:C:\tools\JetBrains\IntelliJ IDEA 2018.3.4\bin" -Dfile.encoding=UTF-8 -classpath "C:\tools\JetBrains\IntelliJ IDEA 2018.3.4\lib\idea_rt.jar;C:\tools\JetBrains\IntelliJ IDEA 2018.3.4\plugins\junit\lib\junit-rt.jar;C:\tools\JetBrains\IntelliJ IDEA 2018.3.4\plugins\junit\lib\junit5-rt.jar;C:\tools\Java\jdk1.8.0_201\jre\lib\charsets.jar;C:\tools\Java\jdk1.8.0_201\jre\lib\deploy.jar;C:\tools\Java\jdk1.8.0_201\jre\lib\ext\access-bridge-64.jar;C:\tools\Java\jdk1.8.0_201\jre\lib\ext\cldrdata.jar;C:\tools\Java\jdk1.8.0_201\jre\lib\ext\dnsns.jar;C:\tools\Java\jdk1.8.0_201\jre\lib\ext\jaccess.jar;C:\tools\Java\jdk1.8.0_201\jre\lib\ext\jfxrt.jar;C:\tools\Java\jdk1.8.0_201\jre\lib\ext\localedata.jar;C:\tools\Java\jdk1.8.0_201\jre\lib\ext\nashorn.jar;C:\tools\Java\jdk1.8.0_201\jre\lib\ext\sunec.jar;C:\tools\Java\jdk1.8.0_201\jre\lib\ext\sunjce_provider.jar;C:\tools\Java\jdk1.8.0_201\jre\lib\ext\sunmscapi.jar;C:\tools\Java\jdk1.8.0_201\jre\lib\ext\sunpkcs11.jar;C:\tools\Java\jdk1.8.0_201\jre\lib\ext\zipfs.jar;C:\tools\Java\jdk1.8.0_201\jre\lib\javaws.jar;C:\tools\Java\jdk1.8.0_201\jre\lib\jce.jar;C:\tools\Java\jdk1.8.0_201\jre\lib\jfr.jar;C:\tools\Java\jdk1.8.0_201\jre\lib\jfxswt.jar;C:\tools\Java\jdk1.8.0_201\jre\lib\jsse.jar;C:\tools\Java\jdk1.8.0_201\jre\lib\management-agent.jar;C:\tools\Java\jdk1.8.0_201\jre\lib\plugin.jar;C:\tools\Java\jdk1.8.0_201\jre\lib\resources.jar;C:\tools\Java\jdk1.8.0_201\jre\lib\rt.jar;D:\hibernatedemo1231232\target\classes;C:\tools\maven\apache-maven-3.6.0Reponsitory\org\hibernate\hibernate-core\5.3.2.Final\hibernate-core-5.3.2.Final.jar;C:\tools\maven\apache-maven-3.6.0Reponsitory\org\jboss\logging\jboss-logging\3.3.2.Final\jboss-logging-3.3.2.Final.jar;C:\tools\maven\apache-maven-3.6.0Reponsitory\javax\persistence\javax.persistence-api\2.2\javax.persistence-api-2.2.jar;C:\tools\maven\apache-maven-3.6.0Reponsitory\org\javassist\javassist\3.22.0-GA\javassist-3.22.0-GA.jar;C:\tools\maven\apache-maven-3.6.0Reponsitory\net\bytebuddy\byte-buddy\1.8.12\byte-buddy-1.8.12.jar;C:\tools\maven\apache-maven-3.6.0Reponsitory\antlr\antlr\2.7.7\antlr-2.7.7.jar;C:\tools\maven\apache-maven-3.6.0Reponsitory\org\jboss\spec\javax\transaction\jboss-transaction-api_1.2_spec\1.1.1.Final\jboss-transaction-api_1.2_spec-1.1.1.Final.jar;C:\tools\maven\apache-maven-3.6.0Reponsitory\org\jboss\jandex\2.0.5.Final\jandex-2.0.5.Final.jar;C:\tools\maven\apache-maven-3.6.0Reponsitory\com\fasterxml\classmate\1.3.4\classmate-1.3.4.jar;C:\tools\maven\apache-maven-3.6.0Reponsitory\javax\activation\javax.activation-api\1.2.0\javax.activation-api-1.2.0.jar;C:\tools\maven\apache-maven-3.6.0Reponsitory\dom4j\dom4j\1.6.1\dom4j-1.6.1.jar;C:\tools\maven\apache-maven-3.6.0Reponsitory\org\hibernate\common\hibernate-commons-annotations\5.0.4.Final\hibernate-commons-annotations-5.0.4.Final.jar;C:\tools\maven\apache-maven-3.6.0Reponsitory\mysql\mysql-connector-java\8.0.15\mysql-connector-java-8.0.15.jar;C:\tools\maven\apache-maven-3.6.0Reponsitory\com\google\protobuf\protobuf-java\3.6.1\protobuf-java-3.6.1.jar;C:\tools\maven\apache-maven-3.6.0Reponsitory\junit\junit\4.12\junit-4.12.jar;C:\tools\maven\apache-maven-3.6.0Reponsitory\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit4 testmain.test,fun1
May 14, 2019 10:34:22 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.3.2.Final}
May 14, 2019 10:34:22 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
May 14, 2019 10:34:22 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.4.Final}
**************************************提示没有找到配置文件***********************************
org.hibernate.boot.MappingNotFoundException: Mapping (RESOURCE) not found : entity/TbAddressEntity.hbm.xml : origin(entity/TbAddressEntity.hbm.xml)

com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
**********************************************************************

Process finished with exit code -1

这是因为entity包中的所有的xml 文件都没有在build的时候,放入到targe文件夹中个,我们需要在pom配置文件中,让其扫描xml文件。
在pom文件中增加如下代码

 <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>

此时运行再次报错。这是因为设置mysql数据库方言。

Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set

我们需要在hibernate.cfg.xml配置数据库连接的时候,再次配置上数据库方言。

 <property name="connection.url">jdbc:mysql://localhost:3306/pinyougoudb</property>
    <property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
    <property name="connection.username">root</property>
    <property name="connection.password">root</property>
    <-- 数据库方言 -->
    <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>

再次运行,发现又报错了,

Caused by: java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.

这是因为数据库时区错误,我们应该在数据库连接后面,设置数据库连接。

<property name="connection.url">jdbc:mysql://localhost:3306/pinyougoudb?serverTimezone=UTC</property>

此时运行成功,我们查看数据库,看数据是否插入成功。

补充:

执行Hibernate的时候,报错说Mapping文件找不到。检查后发现路径没有错,使用idea+maven创建的项目。

maven默认的只编译加载resources目录下的配置文件,我把文件放在了java目录下,所以需要在pom.xml中添加如下配置(eclipse中可能不会出现这种情况)