主要是hibernate的更新速度已经超出了我的现象,今天测试schemaExport的时候直接懵逼了,为啥子不能创建表了呢,还报了很多错误,查看文档知道hibernate5的schemaExport方法跟4的有一定差别。不扯淡了….
直接进入正题!hibernate4的方法我就不累述了,网上特别多。。。。我用的是5.2.6版本的hibernate, mysql5.7。
1、首先把需要的包导进来(很多然后Build Path…….)
2、配置hibernate.cfg.xml
<!-- ~ Hibernate, Relational Persistence for Idiomatic Java ~ ~ License:
GNU Lesser General Public License (LGPL), version 2.1 or later. ~ See the
lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. -->
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 数据库的链接 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">数据库密码</property>
<!--hibernate可选项 -->
<property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name="format_sql">true</property>
<property name="show_sql">true</property>
<!--hbm文件 -->
<mapping resource="com/siggy/pojo/Score.hbm.xml"/>
</session-factory>
</hibernate-configuration>
``
3、接着开始配置个Scor.japackage com.siggy.pojo;
public class Score {
private int Id;
private String stuId;//学生编号
private String subjectId;//学科编号
private String result;//成绩
public int getId() {
return Id;
}
public void setId(int id) {
Id = id;
}
public String getStuId() {
return stuId;
}
public void setStuId(String stuId) {
this.stuId = stuId;
}
public String getSubjectId() {
return subjectId;
}
public void setSubjectId(String subjectId) {
this.subjectId = subjectId;
}
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
}
4、配置Score.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.siggy.pojo.Score" table="score">
<id name="id" >
<!-- 生成策略 -->
<generator class="native"></generator>
</id>
<!-- 类属性 -->
<property name="Id" />
<property name="stuId" />
<property name="subjectId" />
<property name="result" />
</class>
</hibernate-mapping>
5、编写
package com.siggy.test;
import java.util.EnumSet;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.tool.schema.TargetType;
import org.junit.Test;
public class Hibernate {
@Test
public void testCreateDB(){
//注意这里的写法跟hibernate4.x的写法有很大的不同
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().configure().build();
Metadata metadata = new MetadataSources(serviceRegistry).buildMetadata();
SchemaExport schemaExport = new SchemaExport();
schemaExport.create(EnumSet.of(TargetType.DATABASE), metadata);
}
}
总结:经过查找发现是hibernate里的dialect和Mysql的版本不匹配,SQL语句里的type=InnoDB使用在MySQL5.0之前,5.0之后就要是使用engine=InnoDB。