一.Hibernate原生状态
1
2
3
4
5
|
Configuration cfg =
new Configuration().configure();
SchemaExport export =
new SchemaExport(cfg);
export.create(
true , true ); |
二.Hibernate整合Spring
1.使用hibernate.cfg.xml原生配置
hibernate.cfg.xml同原生一样编写
在Spring主配置文件applicationContext中,引入hibernate.cfg.xml
使用SchemaExport生成数据库表的代码同上一致。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
Spring applicationContext.xml
<
bean id = “sessionFactory” class = “org.springframework.orm.hibernate3.LocalSessionFactoryBean” > < property name = “configLocation” value = “file:src/hibernate.cfg.xml” > </ property >
</
bean > |
2.不使用hibernate.cfg.xml,在Spring的主配置文件applicationContext.xml中配置
完全不编写hibernate.cfg.xml,全部都在applicationContext.xml中配置
1 2 3 4 5 6 7 8 9 10 11 12 13 | ClassPathResource ac = new ClassPathResource( “applicationContext.xml” ); XmlBeanFactory xbf = new XmlBeanFactory(ac); //注意: &sessionFactory ,一定要包含 & ,不加Spring返回的是Hibernate下的SessionFactoryImpl类 LocalSessionFactoryBean lsfb=(LocalSessionFactoryBean) xbf.getBean( “&sessionFactory” ); Configuration cfg=lsfb.getConfiguration(); SchemaExport export= new SchemaExport(cfg); export.create( true , false ); |
|
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/13101.html