使用hibernate自动建表时需要注意的是hibernate不会去创建数据库,所以数据库的创建要手动去完成。创建数据库后就可以通过实体类由hibernate自动去创建数据库表了。
hibernate配置文件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="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
- <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
- <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/student</property>
- <property name="hibernate.connection.username">root</property>
- <property name="hibernate.connection.password"></property>
- <property name="hbm2ddl.auto">update</property>
- <property name="myeclipse.connection.profile">com.mysql.jdbc.Driver</property>
- <property name="show_sql">true</property>
- <mapping resource="com/student/Item.hbm.xml"/>
- </session-factory>
- </hibernate-configuration>
<!--
~ 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="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/student</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>
<property name="hbm2ddl.auto">update</property>
<property name="myeclipse.connection.profile">com.mysql.jdbc.Driver</property>
<property name="show_sql">true</property>
<mapping resource="com/student/Item.hbm.xml"/>
</session-factory>
</hibernate-configuration>
然后我们试着插如一条数据
- package com.student;
- import org.hibernate.HibernateException;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.Transaction;
- import org.hibernate.cfg.Configuration;
- public class TestDemo {
- public static void main(String[] args){
- Configuration cfg = null;
- SessionFactory sf = null;
- Session s = null;
- Transaction ts= null;
- Student student = new Student();
- student.setUsername("zhangmo");
- student.setPassword("123456789");
- student.setAge(20);
- try {
- cfg = new Configuration().configure();
- sf = cfg.buildSessionFactory();
- s = sf.openSession();
- ts = s.beginTransaction();
- s.save(student);
- ts.commit();
- } catch (HibernateException e) {
- // TODO Auto-generated catch block
- if(ts != null)
- {
- ts.rollback();
- }
- e.printStackTrace();
- } finally{
- if(s != null){
- s.close();
- }
- }
- }
- }
package com.student;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class TestDemo {
public static void main(String[] args){
Configuration cfg = null;
SessionFactory sf = null;
Session s = null;
Transaction ts= null;
Student student = new Student();
student.setUsername("zhangmo");
student.setPassword("123456789");
student.setAge(20);
try {
cfg = new Configuration().configure();
sf = cfg.buildSessionFactory();
s = sf.openSession();
ts = s.beginTransaction();
s.save(student);
ts.commit();
} catch (HibernateException e) {
// TODO Auto-generated catch block
if(ts != null)
{
ts.rollback();
}
e.printStackTrace();
} finally{
if(s != null){
s.close();
}
}
}
}
运行结果:
hibernate自动的根据类名创建了一个student的表。