【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。
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);}}運行測試方法。
報錯信息
這是因為entity包中的所有的xml 文件都沒有在build的時候,放入到targe文件夾中個,我們需要在pom配置文件中,讓其掃描xml文件。
在pom文件中增加如下代碼
此時運行再次報錯。這是因為設置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))的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php文件怎么制定编码格式,php文件编
- 下一篇: 一条未发出的短信