Hibernate正向工程实例

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); 

	}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值