Spring和hibernate集合的框架中只要在applicationContext.xml文件中配置如下:
<prop key="hibernate.hbm2ddl.auto">update</prop>
Hibernate框架中只要在hibernate.cfg.xml里加上如下代码:
<property name="hbm2ddl.auto">update</property>
以下是配置hibernate.cfg.xml的代码:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="hbm2ddl.auto">update</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/wangziq
</property>
<property name="connection.username">root</property>
<property name="connection.password">393397258</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="myeclipse.connection.profile">
com.mysql.jdbc.Driver
</property>
<property name="show_sql">true</property>
<mapping resource="com/hust/Jsason/wangziq.hbm.xml" />
</session-factory>
</hibernate-configuration>
配置文件处理完成后,具体功能实现的测试代码如下:
public class TestDemo {
@Test
public void selectStu(){
Configuration cig = new Configuration().configure();
SessionFactory sessionFactory = cig.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction t = session.beginTransaction();
wangziq w = new wangziq();
p.setName("wzq");
p.setidnumber("1024");
p.setName("abc");
p.setidnumber("1111");
session.save(w);
t.commit();
session.close();
sessionFactory.close();
}
}
测试发现信息插入成功
hibernate会根据beans类自动创建一个表,这就是hibernate自动创建表的功能。