spring+hibernate自动生成数据库表结构

现在越来越多的java项目采用java EE开发,spring+hibernate+stuts或者spring+hibernate+jsp的模式越来越常见,于是,更多的是spring来整合和管理hibernate,而不是hibernate独立配置。在项目开发过程中,很多童鞋可能都会经常遇到进行项目移植的情况。这样数据库移植的问题就成了一个令人头疼的问题。也许你说可以从数据库导出表结构,然后再建立新的数据库,而且博主以前也是这么做的,但是当你实施起来的时候,往往会出现种种问题,比如数据类型不匹配,导出数据结构时候类型出差错等等一系列很头疼问题。
其实,当你已经逆向工程生成hibernate持久化类以后,完全可以利用hibernate将持久化类再转回到相应的数据库表。但是特别注意,hibernate只能给你生成表,不能生成数据库。此话的意思在于提醒各位博友,在利用hibernate自动生成数据库表之前,必须先建立相应的空的数据库。当然,如果你已经拥有了数据库,并且数据库中已经有表结构了,那么hibernate可以对这些表执行更新操作。
    下面就介绍这些操作,其实非常的简单,就是对applicationContext.xml(spring的配置文档)中的sessionFactory(Bean)的配置问题。sessionFactory的具体介绍参见http://blog.csdn.net/dreamrealised/article/details/9126389
前期工作需要spring整合hibernate,需要在applicationContext文档中集成hibernate,即添加sessionFactory的bean,并且将hibernate的持久化对象写入applicationContext文档。如下:

 
 
[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
  6.     http://www.springframework.org/schema/aop  
  7.     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"  
  8.     default-lazy-init="true" default-autowire="byName">  
  9.   
  10.     <bean id="propertyConfigurer"  
  11.         class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  12.         <property name="locations">  
  13.             <list>  
  14.                 <value>WEB-INF/jdbc.properties</value>  
  15.             </list>  
  16.         </property>  
  17.     </bean>  
  18.   
  19.     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"  
  20.         destroy-method="close">  
  21.         <property name="driverClassName" value="${jdbc.driverClassName}" />  
  22.         <property name="url" value="${jdbc.url}" />  
  23.         <property name="username" value="${jdbc.username}" />  
  24.         <property name="password" value="${jdbc.password}" />  
  25.         <property name="poolPreparedStatements" value="true" />  
  26.         <property name="defaultAutoCommit" value="true" />  
  27.     </bean>  
  28.   
  29.     <bean id="sessionFactory"  
  30.         class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">  
  31.         <property name="hibernateProperties">  
  32.             <value> hibernate.dialect=${hibernate.dialect}  
  33.                 hibernate.query.substitutions=true  
  34.                 hibernate.show_sql=true  
  35.                 hibernate.hbm2ddl.auto=update  
  36.             </value>  
  37.         </property>  
  38.         <property name="annotatedClasses">  
  39.             <list>  
  40.                 <value>nju.software.xkxt.data.dataobject.UserDO</value>  
  41.         </property>  
  42.     </bean>  
  43. </beans>  
这样只需要配置sessionFactory,在hibernateProperties中设置hibernate.hbm2dll.auto设置为update,如下。这样,sessionFactory启动的时候会去检查schema是否一致,如果不一致会做scheme更新。在该项目中,由于具有持久化类UserDO,则当sessionFactory启动的时候,如果项目数据库中没有user表,则在数据库中添加user表,如果存在user表,则就更新user表结构,使user表跟UserDO持久化类定义的结构相同。

 
 
[html] view plain copy
  1. <bean id="sessionFactory"  
  2.     class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">  
  3.     <property name="hibernateProperties">  
  4.         <value>   
  5.             hibernate.dialect=${hibernate.dialect}  
  6.             hibernate.query.substitutions=true  
  7.             hibernate.show_sql=true  
  8.             hibernate.hbm2ddl.auto=update  
  9.         </value>  
  10.     </property>  
  11.     <property name="annotatedClasses">  
  12.         <list>  
  13.             <value>nju.software.xkxt.data.dataobject.UserDO</value>  
  14.         </list>  
  15.     </property>  
  16. </bean>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值