Hibernate 环境配置和依赖添加(使用java web和普通javaSE工程)
1.Hibernate依赖包的添加
File---->Project Structure,按照如图所示操作,导入所依赖的jar包。
2.生成hibernate.hbm.xml的配置文件
(1)点击File,弹出的菜单中点击Project Structure;
(2)点击左侧的Modules,再点击“+”号,再在弹出的菜单中选择Hibernate;
(3)在这时,项目中多出了一个Hibernate,点击Hibernate,再点击“+”号,选择hibernate.hbm.xml;
(4)弹出的窗口中选择Hibernate的版本,然后点击OK;
(5)这时项目架子中多出了一个名为hibernate.hbm.xml的配置文件;
3.配置数据库
(1)点击左下角按钮,使窗口样式如图所示;
(2)选择数据库;
(3)配置数据库后测试连接是否成功,若成功后点击确定;
4.生成Hibernate的实体类以及配置文件
(1)点击窗口中的Persistence;
(2)选择数据源,配置实体类包,选择要生成的实体类(其中日期类型的只能手动修改为java.util.Date),然后点击OK
(2)点击ok,后会生成如下内容
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/storereviews?serverTimezone=UTC&useSSL=false</property> <property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property> <property name="connection.username">root</property> <property name="connection.password">plj888888</property> <!-- DB schema will be updated if needed --> <!-- <property name="hbm2ddl.auto">update</property> --> <mapping resource="ProductClass/Huaweip20Taobao.hbm.xml"/> <mapping resource="ProductClass/Iphonexrtaobao.hbm.xml"/> <mapping resource="ProductClass/Totalcommentxiaomi8.hbm.xml"/> <mapping resource="ProductClass/Xiaomi8Taobao.hbm.xml"/> <mapping resource="ProductClass/Fenciresult.hbm.xml"/> <mapping resource="ProductClass/Jufaanalyze.hbm.xml"/> <mapping resource="ProductClass/Qinggananalyze.hbm.xml"/> <mapping resource="ProductClass/Productfeature.hbm.xml"/> <mapping resource="ProductClass/Opinionword.hbm.xml"/> <mapping resource="ProductClass/Featurewordtree.hbm.xml"/> <mapping resource="ProductClass/Featureandfreq.hbm.xml"/> <mapping resource="ProductClass/Advword.hbm.xml"/> <mapping resource="ProductClass/Objectiveword.hbm.xml"/> <mapping resource="ProductClass/Xiaomi8Taobaonew.hbm.xml"/> <mapping resource="ProductClass/Problemfreq.hbm.xml"/> <mapping resource="ProductClass/Huaweip20Taobaonew.hbm.xml"/> <mapping resource="ProductClass/Iphonexrtaobaonew.hbm.xml"/> <mapping resource="ProductClass/WordsParagraphsEntity.hbm.xml"/> <mapping class="ProductClass.WordsParagraphsEntity"/> </session-factory> </hibernate-configuration>