星空网 > 软件开发 > Java

Hibernate生成数据库表

首先创建实体类

import java.util.Date;public class ProductionEntity {  public Integer getId() {    return id;  }  public void setId(Integer id) {    this.id = id;  }  public String getOrigin() {    return origin;  }  public void setOrigin(String origin) {    this.origin = origin;  }  public Double getPrice() {    return price;  }  public void setPrice(Double price) {    this.price = price;  }  public Date getDate() {    return date;  }  public void setDate(Date date) {    this.date = date;  }  private Integer id;  private String origin;  private Double price;  private Date date;}

然后Eclipse中在实体类上右键New-Other-Hibernate-Hibernate

Hibernate生成数据库表

Hibernate生成数据库表

点击Finish 即可生成ProductionEntity.hbm.

打开ProductionEntity.hbm.

有了hbm.

<??><!DOCTYPE hibernate-configuration PUBLIC    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration>  <session-factory>    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>    <property name="hibernate.connection.password">12345678</property>    <property name="hibernate.connection.url">jdbc:mysql://localhost/test</property>    <property name="hibernate.connection.username">root</property>    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>    <property name="hibernate.show_sql">true</property>     <property name="hibernate.hbm2ddl.auto">update</property>     <property name="hibernate.current_session_context_class">thread</property>     <mapping resource="com/Mageric/entity/ExchangeEntity.hbm./>   </session-factory></hibernate-configuration>

有了实体类和对应的Hibernate配置文件后还需要一个ExportDB类 来生成数据库

import org.hibernate.boot.MetadataSources;import org.hibernate.boot.registry.StandardServiceRegistryBuilder;import org.hibernate.boot.spi.MetadataImplementor;import org.hibernate.service.ServiceRegistry;import org.hibernate.tool.hbm2ddl.SchemaExport;public class ExportDB {  /**   * @param args   */  public static void main(String[] args) {    ServiceRegistry serviceRegistry = (ServiceRegistry) new StandardServiceRegistryBuilder().configure().build();    MetadataImplementor metadataImplementor = (MetadataImplementor) new MetadataSources(serviceRegistry)        .buildMetadata();    SchemaExport export = new SchemaExport(serviceRegistry, metadataImplementor);    export.create(true, true);  }}

Hibernate 5.X的代码跟之前的有些出入 旧的方法已经不再推荐使用了

有了上面两个类和配置文件之后就可以运行ExportDB.java 如无意外就会在数据库中生成对应的类了

Hibernate生成数据库表

生成之后的数据库表

Hibernate生成数据库表




原标题:Hibernate生成数据库表

关键词:Hibernate

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

小包快跑国际寄件:https://www.goluckyvip.com/tag/102425.html
国际小包价格:https://www.goluckyvip.com/tag/102426.html
ems国际小包价格:https://www.goluckyvip.com/tag/102427.html
国际小包收费:https://www.goluckyvip.com/tag/102428.html
国际小包价格表:https://www.goluckyvip.com/tag/102429.html
社交媒体:https://www.goluckyvip.com/tag/10243.html
无锡旅游景点竹海 - 无锡的竹海:https://www.vstour.cn/a/363178.html
5月贾汪好玩的地方 贾汪哪有好玩的地方:https://www.vstour.cn/a/363179.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流