hibernate反向生成数据库表

第一步:添加Hibernatejar包,hibernate.cfg.xml写好数据源.

hibernate.cfg.xml:

<!DOCTYPEhibernate-configurationPUBLIC

"-//Hibernate/HibernateConfigurationDTD3.0//EN"

"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>

<propertyname="hibernate.connection.url">jdbc:mysql://localhost/test</property>

<propertyname="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>

<propertyname="hibernate.connection.username">root</property>

<propertyname="hibernate.connection.password">root</property>

<propertyname="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

<propertyname="hibernate.show_sql">true</property>

<mappingresource="com/bc/bean/HomeBusiness.hbm.xml"/>

</session-factory>

</hibernate-configuration>

第二步:编写实体类;

packagecom.bc.bean;

importjava.util.Date;

publicclassBusiness{

privateIntegeruId;

privateStringuName;

privateDatehirDate;

privateintzStoreNum;

privateintzBranchNum;

privateintzTradNum;

privateintsStoreNum;

privateintsBranchNum;

privateintsTradNum;

privateintbStoreNum;

privateintbBranchNum;

privateintbTradNum;

privateinttotalNum;

privateStringhome;

publicBusiness(){

super();

}

publicIntegergetuId(){

returnuId;

}

publicvoidsetuId(IntegeruId){

this.uId=uId;

}

publicStringgetuName(){

returnuName;

}

publicvoidsetuName(StringuName){

this.uName=uName;

}

publicDategetHirDate(){

returnhirDate;

}

publicvoidsetHirDate(DatehirDate){

this.hirDate=hirDate;

}

publicintgetzStoreNum(){

returnzStoreNum;

}

publicvoidsetzStoreNum(intzStoreNum){

this.zStoreNum=zStoreNum;

}

publicintgetzBranchNum(){

returnzBranchNum;

}

publicvoidsetzBranchNum(intzBranchNum){

this.zBranchNum=zBranchNum;

}

publicintgetzTradNum(){

returnzTradNum;

}

publicvoidsetzTradNum(intzTradNum){

this.zTradNum=zTradNum;

}

publicintgetsStoreNum(){

returnsStoreNum;

}

publicvoidsetsStoreNum(intsStoreNum){

this.sStoreNum=sStoreNum;

}

publicintgetsBranchNum(){

returnsBranchNum;

}

publicvoidsetsBranchNum(intsBranchNum){

this.sBranchNum=sBranchNum;

}

publicintgetsTradNum(){

returnsTradNum;

}

publicvoidsetsTradNum(intsTradNum){

this.sTradNum=sTradNum;

}

publicintgetbStoreNum(){

returnbStoreNum;

}

publicvoidsetbStoreNum(intbStoreNum){

this.bStoreNum=bStoreNum;

}

publicintgetbBranchNum(){

returnbBranchNum;

}

publicvoidsetbBranchNum(intbBranchNum){

this.bBranchNum=bBranchNum;

}

publicintgetbTradNum(){

returnbTradNum;

}

publicvoidsetbTradNum(intbTradNum){

this.bTradNum=bTradNum;

}

publicintgetTotalNum(){

returntotalNum;

}

publicvoidsetTotalNum(inttotalNum){

this.totalNum=totalNum;

}

publicStringgetHome(){

returnhome;

}

publicvoidsetHome(Stringhome){

this.home=home;

}

}

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

HomeBusiness.hbm.xml

<?xmlversion="1.0"?>

<!DOCTYPEhibernate-mappingPUBLIC

"-//Hibernate/HibernateMappingDTD3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mappingpackage="com.bc.bean">

<classname="HomeBusiness"table="t_HomeBusiness">

<idname="id"type="java.lang.Integer">

<columnname="id"precision="6"scale="0"/>

<generatorclass="native">

</generator>

</id>

<propertyname="name"column="name"/>

<propertyname="one"column="one"/>

<propertyname="two"column="two"/>

<propertyname="three"column="three"/>

<propertyname="four"column="four"/>

<propertyname="five"column="five"/>

<propertyname="totalNum"column="totalNum"/>

<propertyname="home"column="home"/>

<propertyname="team"column="team"/>

</class>

</hibernate-mapping>

第四步:Main执行创建

ExportDB.java

packagecom.test.bean.test;

importorg.hibernate.cfg.Configuration;

importorg.hibernate.tool.hbm2ddl.SchemaExport;

publicclassExportDB{

publicstaticvoidmain(String[]args){

//读取hibernate.cfg.xml文件

Configurationcfg=newConfiguration().configure();

SchemaExportexport=newSchemaExport(cfg);

export.create(true,true);

}

}

按照以上步骤,即可反向生成!

相关推荐