MyEclipse的版本是8.5,新建一个实体类Person
import java.io.Serializable;
/**
* @hibernate.mapping default-laze="false"
* @hibernate.class table="PERSON"
* @hibernate.comment 员工
*/
public class Person implements Serializable {
private static final long serialVersionUID = 2583508512288182544L;
private long id;
private String name;
private Integer age;
/**
* @hibernate.id generator-class="native"
* @hibernate.column name="ID" comment="标识"
*/
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
/**
* @hibernate.property type="string" length="50" not-null="true"
* @hibernate.column name="PNAME" comment="姓名"
*/
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
/**
* @hibernate.property type="integer" not-null="true"
* @hibernate.column name="PAGE" comment="年龄"
*/
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
myeclipst项目名称右击myeclipse,点击run xdoclet,如果没有进行myclipse xdoclet配置,需要先进行配置才能执行上述操作。具体配置方法为,项目选中右击点properties选myclipse—>xdoclet选add standard在弹出来的对话框中选standard hibernate.
运行完xdoclet之后将会生成hibernate映射文件,然后在hibernate.cfg.xml中配置好数据库连接设置hibernate.hbm2ddl.auto为create
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/test
</property>
<property name="connection.username">root</property>
<property name="connection.password">1234</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="myeclipse.connection.profile">mysql</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<mapping resource="org/hibernate/pojo/Person.hbm.xml" />
</session-factory>
</hibernate-configuration>
最后编写一个工具类DBExport
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class DBExport {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Configuration cfg = new Configuration().configure("hibernate.cfg.xml");
SchemaExport export = new SchemaExport(cfg);
export.create(true,true);
}
}