hibernate通过配置自动生成数据库表

Spring和hibernate集合的框架中只要在applicationContext.xml文件中配置如下:

update

Hibernate框架中只要在hibernate.cfg.xml里加上如下代码:

update

以下是配置hibernate.cfg.xml的代码:

    
    
    
    
    
    
    update    
        
        org.hibernate.dialect.MySQLDialect    
        
        
        jdbc:mysql://localhost:3306/wangziq    
        
    root    
    393397258    
        
        com.mysql.jdbc.Driver    
        
        
        com.mysql.jdbc.Driver    
        
    true    
        
    
    
    

配置文件处理完成后,具体功能实现的测试代码如下:

public class TestDemo {    
    @Test    
        public void selectStu(){    
            Configuration  cig = new Configuration().configure();    
            SessionFactory sessionFactory = cig.buildSessionFactory();    
             Session session = sessionFactory.openSession();    
              Transaction t = session.beginTransaction();    
             wangziq w = new wangziq();    
             p.setName("wzq");           
             p.setidnumber("1024");
             p.setName("abc");           
             p.setidnumber("1111");
              session.save(w);             
              t.commit();    
              session.close();    
              sessionFactory.close();    
        }    
}


测试发现信息插入成功

hibernate通过配置自动生成数据库表_第1张图片

hibernate会根据beans类自动创建一个表,这就是hibernate自动创建表的功能。

你可能感兴趣的:(hibernate通过配置自动生成数据库表)