<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Atea - Hero's Grave

    面向?qū)ο螅_源,框架,敏捷,云計算,NoSQL,商業(yè)智能,編程思想。

      BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
      40 隨筆 :: 0 文章 :: 28 評論 :: 0 Trackbacks
    XDoclet是一款開源的代碼自動生成引擎,支持很多框架的代碼自動生成。
    而XDoclet2作為Maven2的插件,支持Hibernate3的pojo->xml的代碼自動生成。配合ant與Hibernate Tool API,可以實(shí)現(xiàn)pojo<->xml<->db schema的相互轉(zhuǎn)化。具體關(guān)系為:
     生成處理  實(shí)用工具
     pojo->xml  XDoclet2
     xml->pojo
     Hibernate Tool API
     pojo+xml -> db schema  Hibernate Tool API
     db schema -> pojo+xml
     可用myeclipse,本文不涉及

    下面通過ANT腳本例子,來展示如何完成以上的轉(zhuǎn)換。
     1 <?xml version="1.0" encoding="utf-8"?>
     2 <project name="xdoclet_example">
     3 
     4     <!--xdoclet的jar包所在目錄-->
     5     <property name="xdoclet.plugin.install.dir" value="./lib/xdoclet" />
     6     <!--待處理項(xiàng)目的src目錄-->
     7     <property name="src.dir" value="./src" />
     8     <!--待處理項(xiàng)目的根目錄-->
     9     <property name="prj.dir" value="./" />
    10 
    11     <!--include進(jìn)來xdoclet所有jar包-->
    12     <path id="xdoclet.task.classpath" description="xdoclet的所有jar">
    13         <fileset dir="${xdoclet.plugin.install.dir}">
    14             <include name="**/*.jar" />
    15         </fileset>
    16     </path>
    17     <!--include進(jìn)來待處理項(xiàng)目的所有jar包,意在使用hibernate相關(guān)的jar包-->
    18     <path id="prj.jar" description="項(xiàng)目目錄下的所有jar">
    19         <fileset dir="${prj.dir}">
    20             <include name="**/*.jar" />
    21         </fileset>
    22     </path>
    23     <!--
    24         繼承上2個目錄,同時需要把pojo編譯好的class文件包含進(jìn)來。
    25         這里定義需要留意,很容易掉入到ant的bug陷阱里去。
    26         詳細(xì)可參照http://m.tkk7.com/boluobn/articles/136197.html
    27     -->
    28     <path id="runtime.path" description="SchemaExport用的jar和class文件">
    29         <path refid="xdoclet.task.classpath" />
    30         <path refid="prj.jar" />
    31         <pathelement location="${prj.dir}/build/classes" />
    32     </path>
    33 
    34     <!--
    35         xdoclet只在判斷未存在hbm.xml的情況下才會生成,已存在的話則不會自動覆蓋。
    36         所以定義一個清空已存在的hbm.xml文件任務(wù)。
    37     -->
    38     <target name="clean" description="清空已有的hbm.xml">
    39         <echo message="清空已有的hbm.xml" />
    40         <delete dir="src">
    41             <include name="**/po/*.hbm.xml" />
    42         </delete>
    43     </target>
    44 
    45     <!--根據(jù)pojo生成hbm.xml,注意一下pojo的路徑即可-->
    46     <target name="xdoclet2hbm" description="根據(jù)pojo生成hbm.xml" depends="clean">
    47         <echo message="根據(jù)pojo生成hbm.xml" />
    48         <taskdef name="xdoclet" classname="org.xdoclet.ant.XDocletTask" classpathref="xdoclet.task.classpath" />
    49         <xdoclet encoding="utf-8">
    50             <fileset dir="${src.dir}">
    51                 <include name="**/po/*.java" />
    52             </fileset>
    53             <component classname="org.xdoclet.plugin.hibernate.HibernateMappingPlugin" destdir="${basedir}/${src.dir}" version="3.0" />
    54         </xdoclet>
    55     </target>
    56 
    57     <!--根據(jù)hibernate.cfg.xml文件生成pojo-->
    58     <target name="hbm2java" description="根據(jù)hbm.xml文件生成pojo">
    59         <echo message="根據(jù)hbm.xml文件生成pojo" />
    60         <taskdef name="hbm2java" classname="org.hibernate.tool.ant.HibernateToolTask" classpathref="runtime.path" />
    61         <hbm2java destdir="${src.dir}">
    62             <configuration configurationfile="${src.dir}/hibernate.cfg.xml" />
    63             <hbm2java />
    64         </hbm2java>
    65     </target>
    66 
    67     <!--
    68         生成數(shù)據(jù)庫環(huán)境的任務(wù)。
    69         這個可能并不常用,使用org.hibernate.tool.hbm2ddl.SchemaExport編寫一個JUnit test可以更方便的連搭環(huán)境帶造數(shù)據(jù)。
    70     -->
    71     <target name="SchemaExport" description="根據(jù)hibernate.cfg.xml生成數(shù)據(jù)庫環(huán)境">
    72         <echo message="根據(jù)hibernate.cfg.xml生成數(shù)據(jù)庫環(huán)境" />
    73         <taskdef name="schemaexport" classname="org.hibernate.tool.hbm2ddl.SchemaExportTask" classpathref="runtime.path" />
    74         <schemaexport config="${src.dir}/hibernate.cfg.xml" quiet="false" text="false" drop="false" delimiter=";" output="schema-export.sql" />
    75     </target>
    76 
    77 </project>
    腳本很詳細(xì),看注釋即可。

    在使用xdoclet2hbm前,需要在pojo上設(shè)定javadoc注釋,以告訴xdoclet如何進(jìn)行解析。
    one2one & one2many的例子:
    Child.java
     1 package po;
     2 
     3 /**
     4  * @hibernate.class table="child"
     5  */
     6 @SuppressWarnings("serial")
     7 public class Child implements java.io.Serializable {
     8 
     9     //xdoclet注釋有寫在getter上與寫在field上2種方法。
    10     //寫在field上會自動生成access="field",所以寫在getter上更常用一些。
    11 
    12     private String id;
    13     private String name;
    14     private Father father;
    15 
    16     /**
    17      * @hibernate.id generator-class="uuid.hex" column="id" length="32"
    18      */
    19     public String getId() {
    20         return this.id;
    21     }
    22 
    23     public void setId(String id) {
    24         this.id = id;
    25     }
    26 
    27     //基本支持所有的hibernate 屬性,所以想要啥設(shè)置就大膽的寫吧
    28     /**
    29      * @hibernate.property column="name" length="32" not-null="true" type="java.lang.String" lazy="true"
    30      */
    31     public String getName() {
    32         return this.name;
    33     }
    34 
    35     public void setName(String name) {
    36         this.name = name;
    37     }
    38 
    39     /**
    40      * @hibernate.many-to-one column="father_id" not-null="true"
    41      */
    42     public Father getFather() {
    43         return this.father;
    44     }
    45 
    46     public void setFather(Father father) {
    47         this.father = father;
    48     }
    49 
    50 }

    Father.java
     1 package po;
     2 
     3 import java.util.HashSet;
     4 import java.util.Set;
     5 
     6 /**
     7  * @hibernate.class table="father"
     8  */
     9 @SuppressWarnings("serial")
    10 public class Father implements java.io.Serializable {
    11 
    12     private String id;
    13 
    14     private String name;
    15 
    16     private Integer age;
    17 
    18     private Set<Child> children = new HashSet<Child>(0);
    19 
    20     /**
    21      * @hibernate.id generator-class="uuid.hex" column="id" length="32"
    22      */
    23     public String getId() {
    24         return this.id;
    25     }
    26 
    27     public void setId(String id) {
    28         this.id = id;
    29     }
    30 
    31     /**
    32      * @hibernate.property column="name" length="32" not-null="true" type="java.lang.String"
    33      */
    34     public String getName() {
    35         return this.name;
    36     }
    37 
    38     public void setName(String name) {
    39         this.name = name;
    40     }
    41 
    42     /**
    43      * @hibernate.property column="age" length="32" type="java.lang.Integer"
    44      */
    45     public Integer getAge() {
    46         return age;
    47     }
    48 
    49     public void setAge(Integer age) {
    50         this.age = age;
    51     }
    52 
    53     /**
    54      * @hibernate.set table="child"
    55      * @hibernate.key column="father_id"
    56      * @hibernate.one-to-many class="po.Child"
    57      */
    58     public Set<Child> getChildren() {
    59         return children;
    60     }
    61 
    62     public void setChildren(Set<Child> children) {
    63         this.children = children;
    64     }
    65 }

    many2many的例子:
    Student.java
     1 package po;
     2 
     3 import java.util.HashSet;
     4 import java.util.Set;
     5 
     6 /**
     7  * @hibernate.class table="student"
     8  */
     9 @SuppressWarnings("serial")
    10 public class Student implements java.io.Serializable {
    11 
    12     private String id;
    13     private String name;
    14     private Set<Teacher> teachers = new HashSet<Teacher>(0);
    15 
    16     /**
    17      * @hibernate.id generator-class="uuid.hex" column="id" length="32"
    18      */
    19     public String getId() {
    20         return this.id;
    21     }
    22 
    23     public void setId(String id) {
    24         this.id = id;
    25     }
    26 
    27     /**
    28      * @hibernate.property column="name" length="32" not-null="true" type="java.lang.String"
    29      */
    30     public String getName() {
    31         return this.name;
    32     }
    33 
    34     public void setName(String name) {
    35         this.name = name;
    36     }
    37 
    38     /**
    39      * @hibernate.set table="student_teacher_relation"
    40      * @hibernate.key column="student_id"
    41      * @hibernate.many-to-many class="po.Teacher" column="teacher_id"
    42      */
    43     public Set<Teacher> getTeachers() {
    44         return teachers;
    45     }
    46 
    47     public void setTeachers(Set<Teacher> teachers) {
    48         this.teachers = teachers;
    49     }
    50 }

    Teacher.java
     1 package po;
     2 
     3 import java.util.HashSet;
     4 import java.util.Set;
     5 
     6 /**
     7  * @hibernate.class table="teacher"
     8  */
     9 @SuppressWarnings("serial")
    10 public class Teacher implements java.io.Serializable {
    11 
    12     private String id;
    13     private String name;
    14     private Set<Student> students = new HashSet<Student>(0);
    15 
    16     /**
    17      * @hibernate.id generator-class="uuid.hex" column="id" length="32"
    18      */
    19     public String getId() {
    20         return this.id;
    21     }
    22 
    23     public void setId(String id) {
    24         this.id = id;
    25     }
    26 
    27     /**
    28      * @hibernate.property column="name" length="32" not-null="true" type="java.lang.String"
    29      */
    30     public String getName() {
    31         return this.name;
    32     }
    33 
    34     public void setName(String name) {
    35         this.name = name;
    36     }
    37 
    38     /**
    39      * @hibernate.set table="student_teacher_relation"
    40      * @hibernate.key column="teacher_id"
    41      * @hibernate.many-to-many class="po.Student" column="student_id"
    42      */
    43     public Set<Student> getStudents() {
    44         return students;
    45     }
    46 
    47     public void setStudents(Set<Student> students) {
    48         this.students = students;
    49     }
    50 
    51 }

    總結(jié):
    1.本文沒有涉及hibernate annnotation模式,需要者請自行查閱相關(guān)資料。
    2.XDoclet2沒有XDoclet1代好找,推薦一個網(wǎng)址 Jar Search Engine
    3.本文未能涉及在開發(fā)中,程序運(yùn)轉(zhuǎn)時遇到DB schema結(jié)構(gòu)變化而進(jìn)行的動態(tài)自動轉(zhuǎn)換技術(shù)。如有高人知曉,請不吝賜教。
    posted on 2009-08-01 22:35 Atea 閱讀(1114) 評論(2)  編輯  收藏 所屬分類: HibernateAnt

    評論

    # re: 使用XDoclet2+Ant實(shí)現(xiàn)Hibernate3的pojo-xml-db代碼相互轉(zhuǎn)換[未登錄] 2010-08-11 23:38 lixl
    請問 pojo轉(zhuǎn)換.hbm.xml 的腳本如何執(zhí)行呢?請指教 詳解  回復(fù)  更多評論
      

    # re: 使用XDoclet2+Ant實(shí)現(xiàn)Hibernate3的pojo-xml-db代碼相互轉(zhuǎn)換[未登錄] 2010-08-17 20:27 Atea
    @lixl
    1、注意一下pojo的路徑
    把這句話改成你自己的:
    <include name="**/po/*.java" />
    2、在po里寫javadoc
    3、執(zhí)行ant xdoclet2hbm

    PS:
    一年之后,反觀xml配置方式已不如annotation來得方便
    只在一些有特殊要求的項(xiàng)目中用

      回復(fù)  更多評論
      

    主站蜘蛛池模板: 亚洲精品无码一区二区| 免费无码肉片在线观看| 亚洲中文字幕乱码熟女在线| 狠狠亚洲婷婷综合色香五月排名| 青青青国产在线观看免费网站| 波霸在线精品视频免费观看| 亚洲AV无码AV日韩AV网站| 亚洲第一区视频在线观看| 亚洲日本va中文字幕久久| 国产又黄又爽又猛的免费视频播放| 免费观看激色视频网站(性色)| 18禁在线无遮挡免费观看网站| 一级毛片不卡免费看老司机| 亚洲AV无码国产剧情| 亚洲一区二区三区精品视频| 亚洲图片一区二区| 久久亚洲综合色一区二区三区 | 亚洲高清中文字幕综合网| 亚洲色图综合在线| 国产免费爽爽视频免费可以看| 成人免费在线观看网站| 噼里啪啦免费观看高清动漫4| 久久永久免费人妻精品下载| 国产偷伦视频免费观看| 999zyz**站免费毛片| 久久性生大片免费观看性| 免费一级毛suv好看的国产网站 | 最新猫咪www免费人成| 国产免费女女脚奴视频网| 99视频免费观看| 四虎影视在线影院在线观看免费视频 | 亚洲爆乳精品无码一区二区| 亚洲三级视频在线观看| 亚洲国产成人精品无码区在线网站| 91精品国产亚洲爽啪在线观看| 亚洲人成网站在线播放影院在线| 亚洲午夜福利717| 亚洲国产AV无码专区亚洲AV| 亚洲第一极品精品无码久久| 久久精品国产精品亚洲艾 | 国产成人免费AV在线播放|