SchemaExport生成数据库表
一.Hibernate原生状态
1 |
Configuration cfg = new Configuration().configure(); |
3 |
SchemaExport export = new SchemaExport(cfg); |
5 |
export.create( true , true ); |
二.Hibernate整合Spring
1.使用hibernate.cfg.xml原生配置
hibernate.cfg.xml同原生一样编写
在Spring主配置文件applicationContext中,引入hibernate.cfg.xml
使用SchemaExport生成数据库表的代码同上一致。
01 |
Spring applicationContext.xml |
03 |
< bean id = "sessionFactory" |
05 |
class = "org.springframework.orm.hibernate3.LocalSessionFactoryBean" > |
07 |
< property name = "configLocation" |
09 |
value = "file:src/hibernate.cfg.xml" > |
2.不使用hibernate.cfg.xml,在Spring的主配置文件applicationContext.xml中配置
完全不编写hibernate.cfg.xml,全部都在applicationContext.xml中配置
01 |
ClassPathResource ac = new ClassPathResource( "applicationContext.xml" ); |
03 |
XmlBeanFactory xbf = new XmlBeanFactory(ac); |
07 |
LocalSessionFactoryBean lsfb=(LocalSessionFactoryBean) xbf.getBean( "&sessionFactory" ); |
09 |
Configuration cfg=lsfb.getConfiguration(); |
11 |
SchemaExport export= new SchemaExport(cfg); |
13 |
export.create( true , false ); |
03 |
< bean id = "dataSource" class = "org.springframework.jdbc.datasource.DriverManagerDataSource" > |
05 |
< property name = "driverClassName" value = "${jdbc.driverClassName}" /> |
07 |
< property name = "url" value = "${jdbc.url}" /> |
09 |
< property name = "username" value = "${jdbc.username}" /> |
11 |
< property name = "password" value = "${jdbc.password}" /> |
19 |
< bean id = "sessionFactory" class = "org.springframework.orm.hibernate3.LocalSessionFactoryBean" > |
21 |
< property name = "dataSource" ref = "dataSource" ></ property > |
23 |
< property name = "mappingResources" > |
27 |
< value >xxx/xxx/model/User.hbm.xml</ value > |
33 |
< property name = "hibernateProperties" > |
37 |
< prop key = "hibernate.dialect" >org.hibernate.dialect.MySQLDialect</ prop > |
39 |
< prop key = "hibernate.show_sql" >true</ prop > |
41 |
< prop key = "hibernate.format_sql" >true</ prop > |
|