实体类
package com.tao.pojo;
public class Student {
private String id;
private String name;
private String pass;
public Student() {
super();
}
public Student(String name, String pass) {
super();
this.name = name;
this.pass = pass;
}
public Student(String id, String name, String pass) {
super();
this.id = id;
this.name = name;
this.pass = pass;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", pass=" + pass + "]";
}
}
映射文件
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2018-3-1 14:18:27 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="com.tao.pojo.Student" table="STUDENT">
<id name="id" type="java.lang.String">
<column name="ID" />
<generator class="uuid" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
<property name="pass" type="java.lang.String">
<column name="PASS" />
</property>
</class>
</hibernate-mapping>
配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test0228_002</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping resource="com/tao/pojo/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
测试
package com.tao.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.dialect.MySQL5Dialect;
public class TestMain {
//用hibernate框架创建表
public static void main(String[] args) {
Configuration configure = new Configuration().configure("hibernate.cfg.xml");
SessionFactory factory = configure.buildSessionFactory();
Session session = factory.openSession();
session.beginTransaction();
session.getTransaction().commit();
session.close();
factory.close();
}
}
用程序生成表,如果报的有这个错
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'type=MyISAM' at line 6
解决方案
将数据库方言改为
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>