您当前的位置: 首页 > 

xiangzhihong8

暂无认证

  • 1浏览

    0关注

    1324博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

使用schemaExport自动生成表结构

xiangzhihong8 发布时间:2015-12-29 09:27:09 ,浏览量:1

一.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
 
 
      
 
      
 

       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 );

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
 
 
 
    
 
      
 
      
 
      
 
 
 
 
 
 
 
 
 
    
 
    
 
      
 
          xxx/xxx/model/User.hbm.xml
 
      
 
    
 
    
 
      
 
          org.hibernate.dialect.MySQLDialect
 
          true
 
          true
 
      
 
    
 
 
关注
打赏
1482932726
查看更多评论
立即登录/注册

微信扫码登录

0.1016s