日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/9 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【hibernate】idea利用maven搭建hibernate环境(创建hibernate配置文件(包括cfg和hbm)) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • 1.準備
  • 2. 步驟
    • 2-1 在idea中創建一個mavne項目。
    • 2-2 引入jar包
    • 2-3 利用idea插件,創建hibernate配置文件(包括cfg和hbm)
      • 2-3-1 添加hibernate.cfg.xml 文件
      • 2-3-2 自動生成配置文件 *.hbm.xml 文件
    • 2-3 配置hibernate.cfg.xml 文件
  • 3 測試
  • 補充:

在開始學習hibernate的時候,使用eclipse通過引入jar包的方式創建,又要導jar包又要導約束非常的麻煩。
今天我們利用idea和maven來創建一個hibernate框架。

1.準備

1、idea。
2、mysql

2. 步驟

2-1 在idea中創建一個mavne項目。



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


到此添加成功,點擊應用即可。

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

連接mysql數據庫


下載myqsl驅動

測試連接成功,既可以打開了。

自動生成配置文件和實體。








到此,配置文件已經生成完畢。下面我們 進入開發測試。

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 {@Testpublic 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 '?D1ú±ê×?ê±??' 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中可能不會出現這種情況)

總結

以上是生活随笔為你收集整理的【hibernate】idea利用maven搭建hibernate环境(创建hibernate配置文件(包括cfg和hbm))的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。