用appfuse2.0生成一个单独的模块

原文: http://www.blogjava.net/liuwentao253/archive/2007/08/30/141406.html
从appfuse开始学习Spring和Hibernate - (1)构建项目 http://jackycheng2007.iteye.com/blog/1512766
关于maven appfuse2 的心得体会 http://blog.csdn.net/chenhuaishi/article/details/4873442
appfuse2 根据表生成自己想生成的pojo http://luohua.iteye.com/blog/443903

mvn appfuse:gen-model是appfuse maven plugin(简称AMP,大家记住这个简称)中的
一个命令,用以从数据库反向工程生成pojo。我们可以在eclipse环境中配置这个插
件,具体配置方法类似于配置mvn jetty:run,在此不再赘述。配置好了这个插件之
后,我们还有如下几步要做才能保证该插件能正确运行:
1. 修改pom.xml中<artifactId>appfuse-maven-plugin</artifactId>下面的相关配
置,在<configuration>下加入如下语句:
<componentProperties>
    <revengfile>target/classes/hibernate.reveng.xml</revengfile>
</componentProperties>

hibernate.reveng.xml是用以定制反向工程的配置文件,基本配置appfuse已经帮我们
做好了,没有大的问题,不过大家可以根据自己的需求去修改,也可以更改到另外一
个路径
2.我们的物理表中往往存在外键关联关系,这时用这个插件反向工程的时候会报错,
我们需要按如下步骤进行修改:
a.首先cmd进入自己的项目目录
b.运行命令:mvn appfuse:copy-template
c.找到src/test/resources/appfuse/model路径下的
Ejb3PropertyGetAnnotation.ftl文件,找到
<#lt/>${pojo.generateJoinColumnsAnnotation(property)},将此句改为
<#lt/>${pojo.generateJoinColumnsAnnotation(property,cfg)}即可
---------------------------下面的修改比较完整一点
<#if ejb3><#rt/>
    <#if pojo.hasIdentifierProperty()><#rt/>
        <#if property.equals(clazz.identifierProperty)><#rt/>
        <#lt/>${pojo.generateAnnIdGenerator()}
${pojo.generateAnnColumnAnnotation(property)}
    @GeneratedValue(strategy = GenerationType.AUTO)
    @DocumentId<#rt/>
        </#if><#rt/>
    </#if><#rt/>
    <#if c2h.isManyToOne(property)><#rt/>
        <#--TODO support @OneToOne true and false-->
        <#lt/>${pojo.generateManyToOneAnnotation(property)}
    // TODO : 这里要修改,否则出错。1.导入JoinColumn注解类。2.修改JoinColumn的ID名称。
    @JoinColumn(name = "xxxxxx_id")
        <#--TODO support optional and targetEntity-->
        <#lt/>${pojo.generateCollectionAnnotation(property, cfg)}
        <#elseif c2h.isCollection(property)><#rt/>
            <#lt/>${pojo.generateCollectionAnnotation(property, cfg)}
        <#else><#rt/>
        <#lt/>${pojo.generateBasicAnnotation(property)}
        <#if !property.equals(clazz.identifierProperty)><#rt/>
        <#lt/>${pojo.generateAnnColumnAnnotation(property)}
        <#lt/>    @Field
        </#if><#rt/>
    </#if><#rt/>
</#if><#rt/>






原文
----------------------------------
使用 AppFuse2.0,你需要对 maven有一些基本的了解,比如什么叫 target、什么是 pom.xml 以及如何运行 maven,等等。下表列出了本文中用到的也是较为常用的 AppFuse 的 命令:
命令                                                                 说明

mvn eclipse:eclipse                      生成eclipse的项目的配置文件,用户可以直接把项目导入到eclipse中

mvn jetty:run-war                         打包并且发布你的应用程序到Jetty, 查看在 http://localhost:8080 

mvn appfuse:gen                         根据pojo生成dao manger action 页面及他们的test。

mvn appfuse:install                       把生成的源代码及配置文件写入到src中

mvn integration-test                      Runs UI tests in Tomcat using Cargo

mvn appfuse:full-source               Converts AppFuse basic projects to full-source with no AppFuse dependencies. Currently does not work with modular archetypes.

mvn appfuse:gen-model           Generates Java classes from database tables.


本文的示例实现对部门信息的增删查改等基本功能。用 struts 实现表示层,用 Hibernate 开发持久层,用 Spring 提供事务控制等跨模块服务,并用 Acegi 进行安全管理。本示例只用到一个域模型:department,下面是它的 UML 图。
图 1. Department UML 图



现在我们来生成一个简单的模块
首先,还是进入dos命令行
在进入myproject里,执行 mvn eclipse:eclipse
这是可以在eclipse中import导入appfuse的项目—myproject
在src.main.java.org.appfuse.model中新建一个deparment类
Department.java
package org.appfuse.model; 

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table; 

@Entity
@Table(name="department")
public class Department extends BaseObject implements Serializable {
       private static final long serialVersionUID = 831759222476769186L;
       private Long id;
       private String name;     
       private String description;      

       @Id @GeneratedValue(strategy=GenerationType.AUTO)
       public Long getId() {
              return id;
       } 

       public void setId(Long id) {
              this.id = id;
       }

       @Column(name="name",nullable=false,length=100)
       public String getName() {
              return name;
       } 

       public void setName(String name) {
              this.name = name;
       }       

       @Column(name="description",nullable=false,length=100)
       public String getDescription() {
              return description;
       } 

       public void setDescription(String description) {
              this.description = description;
       } 

       @Override
       public boolean equals(Object o) {
              // TODO Auto-generated method stub
              return false;
       }

       @Override
       public int hashCode() {
              // TODO Auto-generated method stub
              return 0;
       }

       @Override
       public String toString() {
              // TODO Auto-generated method stub
              return null;
       }
}


我们现在用appfuse提供的 appfuse:gen来生成模块

AppFuse 制作了一个代码生成工具 ,他生成的代码位于 \target\appfuse\ 目录下面generated-sources内。代码生成工具可以生成绝大部分我们需要的代码,比如 dao 类,service 类,菜单、增删改的 web 页面、配置文件、样本数据,等等。

如果你希望appfuses生成 dao 和 service 类,就在项目根目录下的pom.xml中,把genericCore属性设为false。

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>appfuse-maven-plugin</artifactId>
                <version>${appfuse.version}</version>
                <configuration>
                    <genericCore>${amp.genericCore}</genericCore>
                    <fullSource>${amp.fullSource}</fullSource>
                </configuration>
            </plugin>

Set to false if you want Java files generated for your DAOs and Managers

Set to true if you've "full-sourced" your project and changed org.appfuse to your package name

它可以帮你搞定一切。下面就让我们来运行 “mvn appfuse:gen” 生成代码。


在target\appfuse\generated-sources下会生成部门的代码

我们需要接着执行mvn appfuse:install 把生成的代码写入源程序中

接着执行mvn jetty:run-war,在 http://localhost:8080就可以看到程序了

appfuse的初始界面

猜你喜欢

转载自panyongzheng.iteye.com/blog/1886826