Xdoclet 介绍

Xdoclet 基本概念

我们可以在 java 代码中使用类似于 javadoc 的注释,来表达更多的内容。这些额外的注释,通过使用 xdoclet 工具,我们可以将它们转换为我们需要的各种配置文件。先看一个简单的例子:

比如有一个 java 文件的源代码如下:

   productType跟自己是一对多的关系:这种情况比较特殊,set和many-to-one对应的均为外键

package com.babasport.VO;

import java.util.Set;


/**
 * @hibernate.class 
 * table="productType"
 * @author sunqitang
 *
 */
public class ProductType {
	/**  商品类别Id  **/
	private Integer productTypeId;
	/**  商品类别名称  **/
	private String productTypeName;
	/**  商品类别google搜索内容  **/
	private String productTypeGoogle;
	/**  商品类别备注  **/
	private String productTypeNote;
	/**  商品类别的子类别  **/
	private Set<ProductType> subProductTypes;
	/**  商品类别的父类   **/
	private ProductType parentProductType;
	/**
	 * @hibernate.many-to-one 
	 * column="parentProductType" 
	 * class="com.babasport.VO.ProductType"
	 * @return
	 */
//这里也为目标对象的Id
	public ProductType getParentProductType() {
		return parentProductType;
	}
	public void setParentProductType(ProductType parentProductType) {
		this.parentProductType = parentProductType;
	}
	/**
	 * @hibernate.set 
	 * cascade="all" 
	 * inverse="true" 
	 * lazy="true"
	 * @hibernate.key column="parentProductType"
	 * @hibernate.one-to-many class="com.babasport.VO.ProductType"
	 * @return
	 */
//这里的key的column为目标对象的Id
	public Set<ProductType> getSubProductTypes() {
		return subProductTypes;
	}
	public void setSubProductTypes(Set<ProductType> subProductTypes) {
		this.subProductTypes = subProductTypes;
	}
	/**
	 * @hibernate.id 
	 * generator-class="native" 
	 * @return
	 */
	public Integer getProductTypeId() {
		return productTypeId;
	}
	public void setProductTypeId(Integer productTypeId) {
		this.productTypeId = productTypeId;
	}
	/**
	 * @hibernate.property 
	 * not-null="true" 
	 * length="16" 
	 * @return
	 */
	public String getProductTypeName() {
		return productTypeName;
	}
	public void setProductTypeName(String productTypeName) {
		this.productTypeName = productTypeName;
	}
	/**
	 * @hibernate.property 
	 * length="100"
	 * @return
	 */
	public String getProductTypeGoogle() {
		return productTypeGoogle;
	}
	public void setProductTypeGoogle(String productTypeGoogle) {
		this.productTypeGoogle = productTypeGoogle;
	}
	/**
	 * @hibernate.property 
	 * length="100"
	 * @return
	 */
	public String getProductTypeNote() {
		return productTypeNote;
	}
	public void setProductTypeNote(String productTypeNote) {
		this.productTypeNote = productTypeNote;
	}
	
	
}
 

 

通过 xdoclet ,我们可以得到关于这个类的 Hibernate 映射文件,如下:

 

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="VO.Producttype" table="producttype" catalog="test">
        <id name="id" type="java.lang.String">
            <column name="id" length="10" />
            <generator class="native" />
        </id>
        <many-to-one name="producttype" class="VO.Producttype" fetch="select">
            <column name="sub" length="10" />
        </many-to-one>
        <set name="producttypes" inverse="true">
            <key>
                <column name="sub" length="10" />
            </key>
            <one-to-many class="VO.Producttype" />
        </set>
    </class>
</hibernate-mapping>
 

另个例子:Type和user是一对多的关系:set对应的 key为userId,many-to-one对应的为typeID

type.java

package VO;

import java.util.Set;

/**
 * @hibernate.class table="Type"
 * @author sunqitang
 *
 */
public class Type {
	private Integer typeId;
	private Integer typeName;
	private Set<User> users;
	/**
	 * @hibernate.id generator-class="native"
	 * @return
	 */
	public Integer getTypeId() {
		return typeId;
	}
	public void setTypeId(Integer typeId) {
		this.typeId = typeId;
	}
	/**
	 * @hibernate.property 
	 * @return
	 */
	public Integer getTypeName() {
		return typeName;
	}
	public void setTypeName(Integer typeName) {
		this.typeName = typeName;
	}
	/**
	 * @hibernate.set 
	 * cascade="all"
	 * inverse="true"
	 * @hibernate.key column="userId"
	 * @hibernate.one-to-many class="VO.User" 
	 * @return
	 */
	public Set<User> getUsers() {
		return users;
	}
	public void setUsers(Set<User> users) {
		this.users = users;
	}
}

 user.java

package VO;
/**
 * @hibernate.class table="User"
 * @author sunqitang
 *
 */
public class User {
	private Integer userId;
	private String  userName;
	private Type type;
	/**
	 * @hibernate.id
	 * generator-class="native"
	 * @return
	 */
	public Integer getUserId() {
		return userId;
	}
	public void setUserId(Integer userId) {
		this.userId = userId;
	}
	/**
	 * @hibernate.property 
	 * @return
	 */
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	/**
	 * @hibernate.many-to-one column="typeId" class="VO.Type"
	 * @return
	 */
	public Type getType() {
		return type;
	}
	public void setType(Type type) {
		this.type = type;
	}
}

 生成的hbm为:

type.hbm.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
  <class table="Type" name="VO.Type">
    <id name="typeId">
      <generator class="native"/>
    </id>
    <property name="typeName"/>
    <set inverse="true" cascade="all" name="users">
      <key column="userId"/>
      <one-to-many class="VO.User"/>
    </set>
  </class>
</hibernate-mapping>

 user.hbm.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
  <class table="User" name="VO.User">
    <id name="userId">
      <generator class="native"/>
    </id>
    <property name="userName"/>
    <many-to-one column="typeId" name="type" class="VO.Type"/>
  </class>
</hibernate-mapping>
 

 

使用xdoclet来生成hbm文件。

 
<?xml version="1.0" encoding="UTF-8"?>
<project name="OA系统构建脚本" default="生成hibernate映射文件" basedir=".">

   	<property name="src.dir" value="${basedir}/src"/>
   	<property name="build.dir" value="${basedir}/bin"/>
	<property name="webapp.dir" value="${basedir}/src/webapp"/>
	<property name="xdoclet.home" value="D:\\JAVA电子资源\\组件\\Xdoclet\\xdoclet-plugins-1.0.3\\xdoclet-plugins-1.0.3"/>

   	<!-- Build classpath -->
   	<path id="xdoclet.task.classpath">
      	<fileset dir="${xdoclet.home}/lib">
         	<include name="**/*.jar"/>
      	</fileset>
      	<fileset dir="${xdoclet.home}/plugins">
         	<include name="**/*.jar"/>
      	</fileset>
   	</path>
	<taskdef 
		name="xdoclet"
		classname="org.xdoclet.ant.XDocletTask"
		classpathref="xdoclet.task.classpath"
	/>
	
	<target name="生成hibernate映射文件">
		<xdoclet>
			<fileset dir="${src.dir}/com/babasport/VO">
				<include name="**/*.java"/>
			</fileset>
			<component 
				classname="org.xdoclet.plugin.hibernate.HibernateMappingPlugin"
				version="3.0"
				destdir="${src.dir}"
			/>
		</xdoclet>
	</target>
</project>

你可能感兴趣的:(xml,Hibernate,.net,ant,MyEclipse)