怎么用hibernate自动创建数据库表

1.导入相关的jar包

2.实体类

  1. package demo;  
  2.   
  3. public class User {  
  4.   private int id;  
  5.   private String username;  
  6.   private String password;  
  7.   private int age;  
  8.   public int getId() {  
  9.     return id;  
  10. }  
  11. public void setId(int id) {  
  12.     this.id = id;  
  13. }  
  14. public String getUsername() {  
  15.     return username;  
  16. }  
  17. public void setUsername(String username) {  
  18.     this.username = username;  
  19. }  
  20. public String getPassword() {  
  21.     return password;  
  22. }  
  23. public void setPassword(String password) {  
  24.     this.password = password;  
  25. }  
  26. public int getAge() {  
  27.     return age;  
  28. }  
  29. public void setAge(int age) {  
  30.     this.age = age;  
  31. }  
  32. public User(){  
  33.         
  34.   }  
  35.     
  36. }  

2.配置文件

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE hibernate-configuration PUBLIC  
  3.         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
  4.         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd   
  5.   
  6. ">  
  7. <hibernate-configuration>  
  8. <session-factory>     
  9.     <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>  
  10.  <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>  
  11.  <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property>  
  12.   <property name="hibernate.connection.username">root</property>  
  13.  <property name="hibernate.connection.password"></property>  
  14.    
  15.   <property name="show_sql">true</property>  
  16.   
  17.   <property name="hbm2ddl.auto" >update</property>  
  18.     <mapping resource="demo/user.hbm.xml"/>  
  19.     
  20. </session-factory>  
  21.   
  22. </hibernate-configuration>  
这里声明一下,在这个地方跟之前配置的不同就是加了一个标签,这是自动创建数据库的关键
[html]  view plain  copy
  1. <property name="hbm2ddl.auto" >update</property>  

create: 每次加载hibernate时都会删除上一次的生成的表,然后根据你的类再重新来生成新表,这样很容易丢失数据库

 update: 第一次加载hibernate时会根据实体类自动建立表的结构,以后加载hibernate时根据 实体类自动更新数据库表

注意!敲黑板!hibernate只能自动创建数据库表不可以自动创建数据库嗷~

3.映射关系

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "hibernate-mapping-3.0.dtd" >  
  3. <hibernate-mapping>  
  4. <class name="demo.User" table="t_user">  
  5.     <id name="id" column="ID"  type="int">  
  6.         <generator class="native"/>  
  7.     </id>  
  8.     <property name="username" column="username" type="java.lang.String"  />  
  9.     <property name="password" column="password" type="java.lang.String" />  
  10.     <property name="age" column="age" type="int"/>  
  11.   </class>  
  12.   
  13. </hibernate-mapping>  

4.测试类

[java]  view plain  copy
  1. package test;  
  2.   
  3. import java.util.EnumSet;  
  4.   
  5. import org.hibernate.Session;  
  6. import org.hibernate.SessionFactory;  
  7. import org.hibernate.Transaction;  
  8. import org.hibernate.boot.Metadata;  
  9. import org.hibernate.boot.MetadataSources;  
  10. import org.hibernate.boot.registry.StandardServiceRegistry;  
  11. import org.hibernate.boot.registry.StandardServiceRegistryBuilder;  
  12. import org.hibernate.boot.spi.MetadataImplementor;  
  13. import org.hibernate.cfg.Configuration;  
  14. import org.hibernate.service.ServiceRegistry;  
  15. import org.hibernate.tool.hbm2ddl.SchemaExport;  
  16. import org.hibernate.tool.schema.TargetType;  
  17.   
  18. import demo.User;  
  19.   
  20. public class UserTest {  
  21.     public static void main(String[] args) {  
  22.           Configuration conf = new Configuration().configure();//1、读取配置文件  
  23.             <span style="color:#ff0000;"> StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().configure().build();  
  24.                 Metadata metadata = new MetadataSources(serviceRegistry).buildMetadata();  
  25.                 SchemaExport schemaExport = new SchemaExport();  
  26.             schemaExport.create(EnumSet.of(TargetType.DATABASE), metadata)}</span>  
[java]  view plain  copy
  1.   

重点是加红部分的代码,由于用的是hibernate5.0版本所以创新数据库表的代码改变,旧版本创建数据库的代码是

[java]  view plain  copy
  1. Configuration cfg = new Configuration().configure();    
  2.       
  3.        SchemaExport export = new SchemaExport(cfg);    
  4.           
  5.        export.create(truetrue);    

5。运行


猜你喜欢

转载自blog.csdn.net/qq_36937005/article/details/80665201