利用Hibernate映射文件生oracle或者mysql数据库

(一)利用hibernate影射文件在oracle中生成对应的数据库和表

我用的是hibernate和Spring配置生成的,在Spring的配置文件配置如下:

[xhtml]  view plain copy
  1. <!-- 配置sessionFactory -->  
  2.     <bean id="sessionFactory"  
  3.         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
  4.         <property name="dataSource">  
  5.             <ref bean="dataSource" />  
  6.         </property>  
  7.         <property name="mappingDirectoryLocations">  
  8.             <list>  
  9.                 <value>classpath:/standing/common/pojo/hbm/</value>  
  10.             </list>  
  11.         </property>  
  12.         <property name="hibernateProperties">  
  13.             <props>  
  14.                 <prop key="hibernate.dialect">  
  15.                     org.hibernate.dialect.OracleDialect  
  16.                 </prop>  
  17.                 <prop key="hibernate.jdbc.batch_size">25</prop>  
  18.                 <prop key="hibernate.show_sql">true</prop>  
  19.                 <prop key="hibernate.hbm2ddl.auto">validate</prop>  
  20.             </props>  
  21.         </property>  
  22.     </bean>  

 

启动项目时即可在oracle中生成数据库。

(二)利用hibernate影射文件在mysql中生成对应的数据库和表

①同样利用hibernate和Spring配置生成,在session-factory中的配置如下:

[xhtml]  view plain copy
  1. <session-factory>  
  2.     <property name="dialect">org.hibernate.dialect.MySQLDialect</property>  
  3.     <property name="connection.url">  
  4.         jdbc:mysql://localhost:3306/xdatabasename       </property>  
  5.     <property name="connection.username">root</property>  
  6.     <property name="connection.password">root</property>  
  7.     <property name="connection.driver_class">  
  8.         com.mysql.jdbc.Driver  
  9.     </property>  
  10.     <property name="myeclipse.connection.profile">  
  11.         myDriver  
  12.     </property>  
  13.     <property name="jdbc.use_scrollable_resultset">false</property>   
  14.  <property name="hibernate.connection.pool.size">20</property>  
  15.      <property name="hibernate.show_sql">true</property>  
  16.      <property name="jdbc.fetch_size">50</property>   
  17.      <property name="jdbc.batch_size">25</property>   
  18.     <property name="format_sql">true</property>  
  19.     <property name="show_sql">true</property>  
  20.     <mapping resource="XX/po/X.hbm.xml" />  
  21.   
  22. </session-factory>  

②需要写一个main方法,代码如下:

[java]  view plain copy
  1. package xX.test;  
  2.   
  3. import org.hibernate.cfg.Configuration;  
  4. import org.hibernate.tool.hbm2ddl.SchemaExport;  
  5.   
  6. public class CreateDatabase {  
  7.   
  8.     public static void main(String[] args) {  
  9.         Configuration cfg = new Configuration().configure();  
  10.         SchemaExport schemaExport= new SchemaExport(cfg);  
  11.         schemaExport.create(falsetrue);  
  12.     }  
  13.   
  14. }  

注意:需要先在数据库中建一个名字是xdatabasename的空数据库

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值