hibernate自动建表

hibernate配置文件hibernate.cfg.xml

[java]  view plain  copy
  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>  

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

[java]  view plain  copy
  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 = ;  
  13.             SessionFactory sf  = ;  
  14.             Session s = ;  
  15.             Transaction ts= ;  
  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 != )  
  33.                 {  
  34.                     ts.rollback();  
  35.                 }  
  36.                 e.printStackTrace();  
  37.             } finally{  
  38.                 if(s != ){  
  39.                     s.close();  
  40.                 }  
  41.             }  
  42.               
  43.         }  
  44. }  

运行结果:

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值