使用xdocLet自动生成: 类的映射文件/ hibernate配置文件


1.用户类User对应组织机构中的类Person:使用了一对一唯一外键双向关联映射
2.访问控制列表ACL与User,Role,Module所关联:而Module使用了一对多双向关联映射
3.用户User和角色Role相互关联:user和Role使用了多对多双向映射
4.在模型类中加上@注解
5.运行Ant脚本:build.xml


<?xml version="1.0" encoding="GBK"?>
<project name="OA系统构建脚本" default="生成Hibernate配置文件" basedir=".">

   	<property name="src.dir" value="${basedir}/"/>
   	<property name="build.dir" value="${basedir}/bin"/>
	<property name="xdoclet.home" value="D:/代码库/BJSXT/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/wlh/oa/model">
				<include name="**/*.java"/>
			</fileset>			
			<component
				classname="org.xdoclet.plugin.hibernate.HibernateConfigPlugin"
				destdir="${src.dir}"
				version="3.0"
				hbm2ddlauto="update"
				jdbcurl="jdbc:mysql://127.0.0.1/oa"
				jdbcdriver="com.mysql.jdbc.Driver"
				jdbcusername="root"
				jdbcpassword="bjsxt"
				dialect="org.hibernate.dialect.MySQLDialect"
				showsql="true"
			/>
		</xdoclet>
	</target>
	<target name="生成hibernate映射文件">
		<xdoclet>
			<fileset dir="${src.dir}/com/wlh/oa/model">
				<include name="**/*.java"/>
			</fileset>
			<component 
				classname="org.xdoclet.plugin.hibernate.HibernateMappingPlugin"
				version="3.0"
				destdir="${src.dir}"
			/>
		</xdoclet>
	</target>
</project>







package com.wlh.oa.model;

import java.util.Date;

/**
 * 
 * @author Administrator
 * @hibernate.class table="T_User"
 */
public class User {
	
	/**
	 * @hibernate.id
	 * 		generator-class="native"
	 */
	private int id;
	
	/**
	 * @hibernate.property 	
	 * 		unique="true"
	 * 		not-null="true"
	 */
	private String username;
	
	/**
	 * @hibernate.property
	 * 		not-null="true"
	 */
	private String password;
	
	/**
	 * @hibernate.property
	 */
	private Date createTime;
	
	/**
	 * @hibernate.property
	 */
	private Date expireTime;
	
	/**
	 * @hibernate.many-to-one unique="true"
	 */
	private Person person;
	
	}



package com.wlh.oa.model;

/**
 * 
 * @author Administrator
 * @hibernate.class table="T_Role"
 */
public class Role {
	
	/**
	 * @hibernate.id
	 * 		generator-class="native"
	 */
	private int id;
	
	/**
	 * @hibernate.property
	 */
	private String name;
	}


package com.wlh.oa.model;

/**
 * 
 * @author Administrator
 * @hibernate.class table="T_UsersRoles"
 */
public class UsersRoles {
	
	/**
	 * @hibernate.id generator-class="native"
	 */
	private int id;
	
	/**
	 * @hibernate.many-to-one
	 */
	private Role role;
	
	/**
	 * @hibernate.many-to-one
	 */
	private User user;
	
	/**
	 * @hibernate.property
	 */
	private int orderNo;
	
	}

package com.wlh.oa.model;

import java.util.Set;


/**
 * 
 * @author Administrator
 * @hibernate.class table="T_Module"
 */
public class Module {
	
	/**
	 * @hibernate.id
	 * 		generator-class="native"
	 */
	private int id;
	
	/**
	 * @hibernate.property
	 */
	private String name;
	
	/**
	 * @hibernate.property
	 */
	private String url;
	
	/**
	 * @hibernate.property
	 */
	private int orderNo;
	
	/**
	 * @hibernate.property
	 */
	private String sn;
	
	/**
	 * @hibernate.many-to-one column="pid"
	 */
	private Module parent;
	
	/**
	 * @hibernate.set lazy="extra" inverse="true"
	 * @hibernate.key column="pid"
	 * @hibernate.one-to-many class="com.bjsxt.oa.model.Module"
	 */
	private Set children;
	
	}


package com.wlh.oa.model;

/**
 * 
 * @author Administrator
 * @hibernate.class table="T_ACL"
 */
public class ACL {
	
	public static final String TYPE_ROLE = "Role";
	public static final String TYPE_USER = "User";
	
	/**
	 * @hibernate.id generator-class="native"
	 */
	private int id;
	
	/**
	 * @hibernate.property
	 */
	private String principalType;
	
	/**
	 * @hibernate.property
	 */
	private int principalId;
	
	/**
	 * @hibernate.property
	 */
	private int moduleId;
	
	/**
	 * @hibernate.property
	 */
	private int aclState;
	
	/**
	 * @hibernate.property
	 */
	private int aclTriState;
	
	/**
	 * ACL的状态为继承(即无效,判断的时候应该判断其所属角色的授权)
	 */
	public static final int ACL_TRI_STATE_EXTENDS = 0xFFFFFFFF;
	
	/**
	 * ACL的状态为不继承(即有效,判断的时候,直接根据aclState判断授权)
	 */
	public static final int ACL_TRI_STATE_UNEXTENDS = 0;
	
	/**
	 * 授权允许
	 */
	public static final int ACL_YES = 1;
	
	/**
	 * 授权不允许
	 */
	public static final int ACL_NO = 0;
	
	/**
	 * 授权不确定
	 */
	public static final int ACL_NEUTRAL = -1;
	
		
	public void setPermission(int permission,boolean yes){
		int temp = 1;
		temp = temp << permission;
		if(yes){
			aclState |= temp;
		}else{
			aclState &= ~temp;
		}
	}
	
	public int getPermission(int permission){
		if(aclTriState == ACL_TRI_STATE_EXTENDS){
			return ACL_NEUTRAL;
		}
		
		int temp = 1;
		temp = temp << permission;
		temp &= aclState;
		if(temp != 0){
			return ACL_YES;
		}
		return ACL_NO;
	}
	
	/**
	 * 设置ACL的继承状态
	 * @param yes true标识继承,false表示不继承
	 */
	public void setExtends(boolean yes){
		if(yes){
			aclTriState = ACL_TRI_STATE_EXTENDS;
		}else{
			aclTriState = ACL_TRI_STATE_UNEXTENDS;
		}
	}
}


你可能感兴趣的:(xml,Hibernate,ant,脚本)