hibernate自动建表的例子

使用hibernate自动建表时需要注意的是hibernate不会去创建数据库,所以数据库的创建要手动去完成。创建数据库后就可以通过实体类由hibernate自动去创建数据库表了。

hibernate配置文件hibernate.cfg.xml

  1. <!-- 
  2.   ~ Hibernate, Relational Persistence for Idiomatic Java 
  3.   ~ 
  4.   ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later. 
  5.   ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. 
  6.   --> 
  7. <!DOCTYPE hibernate-configuration PUBLIC 
  8.     "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
  9.     "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"
  10.  
  11. <hibernate-configuration> 
  12.     <session-factory> 
  13.         <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 
  14.         <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 
  15.         <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/student</property> 
  16.         <property name="hibernate.connection.username">root</property> 
  17.         <property name="hibernate.connection.password"></property> 
  18.         <property name="hbm2ddl.auto">update</property> 
  19.                 <property name="myeclipse.connection.profile">com.mysql.jdbc.Driver</property> 
  20.         <property name="show_sql">true</property> 
  21.         <mapping resource="com/student/Item.hbm.xml"/> 
  22.     </session-factory> 
  23. </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>

然后我们试着插如一条数据

  1. package com.student; 
  2.  
  3. import org.hibernate.HibernateException; 
  4. import org.hibernate.Session; 
  5. import org.hibernate.SessionFactory; 
  6. import org.hibernate.Transaction; 
  7. import org.hibernate.cfg.Configuration; 
  8.  
  9. public class TestDemo { 
  10.         public static void main(String[] args){ 
  11.              
  12.             Configuration cfg = null
  13.             SessionFactory sf  = null
  14.             Session s = null
  15.             Transaction ts= null
  16.              
  17.             Student student = new Student(); 
  18.             student.setUsername("zhangmo"); 
  19.             student.setPassword("123456789"); 
  20.             student.setAge(20); 
  21.              
  22.             try
  23.                 cfg = new Configuration().configure(); 
  24.                 sf = cfg.buildSessionFactory(); 
  25.                 s = sf.openSession(); 
  26.                 ts = s.beginTransaction(); 
  27.                  
  28.                 s.save(student); 
  29.                 ts.commit(); 
  30.             } catch (HibernateException e) { 
  31.                 // TODO Auto-generated catch block 
  32.                 if(ts != null
  33.                 { 
  34.                     ts.rollback(); 
  35.                 } 
  36.                 e.printStackTrace(); 
  37.             } finally
  38.                 if(s != null){ 
  39.                     s.close(); 
  40.                 } 
  41.             } 
  42.              
  43.         } 
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的表。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值