1.本文介绍的是:使用MyEclipse自带的xdoclet来在java类中写xdoclet 标记来创建映射.具体如下: package cn.dao.db; private Long id;//标识 private Date workDate;//日期 private String weather;//天气 private String content; //日志内容(Clob) private String state; //日志状态 private Long orgId; //机构id private Long userId;//用户id private Date createDate;//创建日期 private byte[] image; //图片 public static final String WORKNOTE_BLANK = "00"; //未填写 public static final String WORKNOTE_FULL = "11"; //已填写 * @hibernate.id generator-class="sequence" column="BS" * @hibernate.meta attribute="field-description" value="标识" * @hibernate.generator-param name="sequence" value="SEQ_GW"
public void setId(Long id) { /** * @hibernate.meta attribute="field-description" value="工作日期" public Date getWorkDate(){ /** * @hibernate.property column="weather" not-null="false" length="24" * @hibernate.meta attribute="field-description" value="天气" */ public String getWeather() { /** * @hibernate.property column="content" not-null="false" type="text" * @hibernate.meta attribute="field-description" value="内容" */ public String getContent() { public void setContent(String content) { /** * @hibernate.property column="state" not-null="false" length="2" * @hibernate.meta attribute="field-description" value="状态" */ public String getState() { public void setState(String state) { /** * @hibernate.property column="orgId" type="long" * @hibernate.meta attribute="field-description" value="机构id" */ public Long getOrgId() { * @hibernate.property column="userId" type="long" * @hibernate.meta attribute="field-description" value="用户id" */ public Long getUserId() { /** * @hibernate.property column="createDate" not-null="false" type="timestamp" * @hibernate.meta attribute="field-description" value="创建日期" */ public Date getCreateDate() { * @hibernate.property column="image" type="blob" not-null="false" * @hibernate.meta attribute="field-description" value="图片" */ public byte[] getImage() { public void setImage(byte[] image) { } 3.右键在你的工程的名字点击弹出->MyEclipse->run xdoclet ->没有错误便生成具体类和数据库的.xml映射文件了利用这些映射文件来创建后台数据库的表: 1.先把以上的所有的类的映射xml文件加入到hibernate.cfg.xml: 如: <mapping resource="cn/dao/db/WorkNote.hbm.xml" /> 2.在HibernateSessionFactory.java 类中加入一段代码,运行这个类:HibernateSessionFactory 便生成数据库表: impimp public static void main(String[] args) { 或者改为: public static void main(String[] args) { //第一个参数:表示sql是否输出到控制台;第二个参数为true 启动hibernate时会自动检查数据库,如果缺少表,则自动建表;
=============================== 只要在hibernate.cfg.xml添加这句话,就可以自动生成数据表 |
|