1、idea。
2、mysql
在创建好的项目中的pom文件中,引入hibernate和mysql的jar。
我们可以在maven仓库(https://mvnrepository.com/ )查询最近的hibernate引用的jar。
org.hibernate
hibernate-core
5.3.2.Final
mysql
mysql-connector-java
8.0.15
打开idea中的project structure (ctrl + alt + shift +s)
点击加号,添加hibernate
测试连接成功,既可以打开了。
自动生成配置文件和实体。
到此,配置文件已经生成完毕。下面我们 进入开发测试。
配置文件中大部分文件都已经自动生成,我们只需要配置一下账号密码即可。
<hibernate-configuration>
<session-factory>
<property name="connection.url">jdbc:mysql://localhost:3306/pinyougoudbproperty>
<property name="connection.driver_class">com.mysql.jdbc.Driverproperty>
<property name="connection.username">rootproperty>
<property name="connection.password">rootproperty>
<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"/>
session-factory>
hibernate-configuration>
向数据库表中插入一下数据。
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/javadirectory>
<includes>
<include>**/*.xmlinclude>
includes>
<filtering>truefiltering>
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/pinyougoudbproperty>
<property name="connection.driver_class">com.mysql.cj.jdbc.Driverproperty>
<property name="connection.username">rootproperty>
<property name="connection.password">rootproperty>
<-- 数据库方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialectproperty>
再次运行,发现又报错了,
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=UTCproperty>
此时运行成功,我们查看数据库,看数据是否插入成功。
执行Hibernate的时候,报错说Mapping文件找不到。检查后发现路径没有错,使用idea+maven创建的项目。
maven默认的只编译加载resources目录下的配置文件,我把文件放在了java目录下,所以需要在pom.xml中添加如下配置(eclipse中可能不会出现这种情况)