当单独使用hibernate时可以利用下面的代码可以重新生成数据库表。Configuration cfg = new Configuration().configure();
SchemaExport schemaExport = new SchemaExport(cfg);
schemaExport.create(true, true);当hibernate与spring集成后,以前的hibernate.cfg.xml文件内容已经放在ApplicationContext.xml中了,也就是说hibernate.cfg.xml文件已经不存在了,那怎么利用spring重新生成数据库表呢?谢谢!
SchemaExport schemaExport = new SchemaExport(cfg);
schemaExport.create(true, true);当hibernate与spring集成后,以前的hibernate.cfg.xml文件内容已经放在ApplicationContext.xml中了,也就是说hibernate.cfg.xml文件已经不存在了,那怎么利用spring重新生成数据库表呢?谢谢!
解决方案 »
- 关于WebLogic 的问题 为什么我部署完事后 web页面的index.jsp显示404错误?希望大家帮看看 万分感谢了
- 求Hibernate开发与实战这边电子书或者链接也行能够查看内容
- java中怎么设置多条语句若执行都执行若不执行都不执行
- struts 重复提交问题
- 熟练接口,工厂模式 有这方面的材料吗?
- 请问jboss怎么配置jndi绑定一个普通对象?
- 个人总结关于MyEclipse中创建struts数据源的问题
- 如何在服务器的soap头中添加额外的属性 比如用户名密码 来判断用户的身份
- 使用不同的中间件的区别在哪里?
- JFREECHART1.0.0版本 柱图问题
- linux配置jdk
- myeclipse linux版本无法下载
<prop key="hibernate.hbm2ddl.auto">update</prop>
是否可以把Configuration对象声明为一个<bean>?对它的属性进行注入?
3人收藏此文章, 我要收藏 发表于2个月前(2012-08-18 20:16) , 已有161次阅读 共0个评论 SchemaExport生成数据库表
一.Hibernate原生状态
view sourceprint?
1 Configuration cfg = new Configuration().configure(); 2 3 SchemaExport export = new SchemaExport(cfg); 4 5 export.create(true, true);
二.Hibernate整合Spring 1.使用hibernate.cfg.xml原生配置 hibernate.cfg.xml同原生一样编写 在Spring主配置文件applicationContext中,引入hibernate.cfg.xml 使用SchemaExport生成数据库表的代码同上一致。view sourceprint?
01 Spring applicationContext.xml 02 03 <bean id="sessionFactory" 04 05 class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 06 07 <property name="configLocation" 08 09 value="file:src/hibernate.cfg.xml"> 10 11 </property> 12 13 </bean> 2.不使用hibernate.cfg.xml,在Spring的主配置文件applicationContext.xml中配置 完全不编写hibernate.cfg.xml,全部都在applicationContext.xml中配置
view sourceprint?
01 ClassPathResource ac = new ClassPathResource("applicationContext.xml"); 02 03 XmlBeanFactory xbf = new XmlBeanFactory(ac); 04 05 //注意: &sessionFactory ,一定要包含 & ,不加Spring返回的是Hibernate下的SessionFactoryImpl类 06 07 LocalSessionFactoryBean lsfb=(LocalSessionFactoryBean) xbf.getBean("&sessionFactory"); 08 09 Configuration cfg=lsfb.getConfiguration(); 10 11 SchemaExport export=new SchemaExport(cfg); 12 13 export.create(true, false); view sourceprint?01 <!-- 配置数据源--> 02 03 <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 04 05 <property name="driverClassName" value="${jdbc.driverClassName}"/> 06 07 <property name="url" value="${jdbc.url}"/> 08 09 <property name="username" value="${jdbc.username}"/> 10 11 <property name="password" value="${jdbc.password}"/> 12 13 </bean> 14 15 16 17 <!-- 配置sessionfactory,该配置替代了hibernate.cfg.xml的配置 --> 18 19 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 20 21 <property name="dataSource" ref="dataSource"></property> 22 23 <property name="mappingResources"> 24 25 <list> 26 27 <value>xxx/xxx/model/User.hbm.xml</value> 28 29 </list> 30 31 </property> 32 33 <property name="hibernateProperties"> 34 35 <props> 36 37 <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 38 39 <prop key="hibernate.show_sql">true</prop> 40 41 <prop key="hibernate.format_sql">true</prop> 42 43 </props> 44 45 </property> 46 47 </bean>