如何让hibernate的xml反向生成表

 我来答
就烦条0o
2016-07-11 · 知道合伙人软件行家
就烦条0o
知道合伙人软件行家
采纳数:33315 获赞数:46487
从事多年系统运维,喜欢编写各种小程序和脚本。

向TA提问 私信TA
展开全部
第一步:添加Hibernate jar包,hibernate.cfg.xml写好数据源.

hibernate.cfg.xml:

<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.url">jdbc:MySQL://localhost/test</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>

<mapping resource="com/bc/bean/HomeBusiness.hbm.xml"/>
</session-factory>
</hibernate-configuration>

第二步:编写实体类;

package com.bc.bean;

import java.util.Date;

public class Business {
private Integer uId;
private String uName;
private Date hirDate;
private int zStoreNum;
private int zBranchNum;
private int zTradNum;
private int sStoreNum;
private int sBranchNum;
private int sTradNum;
private int bStoreNum;
private int bBranchNum;
private int bTradNum;
private int totalNum;
private String home;
public Business() {
super();
}
public Integer getuId() {
return uId;
}
public void setuId(Integer uId) {
this.uId = uId;
}
public String getuName() {
return uName;
}
public void setuName(String uName) {
this.uName = uName;
}
public Date getHirDate() {
return hirDate;
}
public void setHirDate(Date hirDate) {
this.hirDate = hirDate;
}
public int getzStoreNum() {
return zStoreNum;
}
public void setzStoreNum(int zStoreNum) {
this.zStoreNum = zStoreNum;
}
public int getzBranchNum() {
return zBranchNum;
}
public void setzBranchNum(int zBranchNum) {
this.zBranchNum = zBranchNum;
}
public int getzTradNum() {
return zTradNum;
}
public void setzTradNum(int zTradNum) {
this.zTradNum = zTradNum;
}
public int getsStoreNum() {
return sStoreNum;
}
public void setsStoreNum(int sStoreNum) {
this.sStoreNum = sStoreNum;
}
public int getsBranchNum() {
return sBranchNum;
}
public void setsBranchNum(int sBranchNum) {
this.sBranchNum = sBranchNum;
}
public int getsTradNum() {
return sTradNum;
}
public void setsTradNum(int sTradNum) {
this.sTradNum = sTradNum;
}
public int getbStoreNum() {
return bStoreNum;
}
public void setbStoreNum(int bStoreNum) {
this.bStoreNum = bStoreNum;
}
public int getbBranchNum() {
return bBranchNum;
}
public void setbBranchNum(int bBranchNum) {
this.bBranchNum = bBranchNum;
}
public int getbTradNum() {
return bTradNum;
}
public void setbTradNum(int bTradNum) {
this.bTradNum = bTradNum;
}
public int getTotalNum() {
return totalNum;
}
public void setTotalNum(int totalNum) {
this.totalNum = totalNum;
}
public String getHome() {
return home;
}
public void setHome(String home) {
this.home = home;
}

}

第三步:编写实体类映射文件;

HomeBusiness.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.bc.bean">
<class name="HomeBusiness" table="t_HomeBusiness">
<id name="id" type="java.lang.Integer">
<column name="id" precision="6" scale="0" />
<generator class="native">
</generator>
</id>
<property name="name" column="name"/>
<property name="one" column="one"/>
<property name="two" column="two"/>
<property name="three" column="three"/>
<property name="four" column="four"/>
<property name="five" column="five"/>
<property name="totalNum" column="totalNum"/>
<property name="home" column="home"/>
<property name="team" column="team"/>
</class>
</hibernate-mapping>

第四步:Main执行创建

ExportDB.java

package com.test.bean.test;

import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

public class ExportDB {

public static void main(String[] args) {
//读取hibernate.cfg.xml文件
Configuration cfg = new Configuration().configure();
SchemaExport export = new SchemaExport(cfg);
export.create(true, true);
}
}

按照以上步骤,即可反向生成!
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式