changNet 2012-07-20
appfuse中的插件可以自动根据POJO生成业务层代码,可以省去自己去配置的麻烦。但是默认的template难免会与项目的实际需求有所差异,因为需要调整AMP的模版。
要调整AMP的模版,需要先知道它在哪里:
使用命令:mvnappfuse:copy-templates
导出AMP模版至test/resources/appfuse目录。此时可以自己去查找需要修改的模版来进行修改。
OK。极大提高生产率。
1、生成项目 mvn archetype:generate -B -DarchetypeGroupId=org.appfuse.archetypes -DarchetypeArtifactId=appfuse-basic-struts-archetype -DarchetypeVersion=2.1.0 -DgroupId=com.launch.mycar.passport -DartifactId=passport -DarchetypeRepository=http://oss.sonatype.org/content/repositories/appfuse
备注 DgroupId=com.launch.mycar.passport 主包名 DartifactId=passport 项目名
参照 http://appfuse.org/display/APF/AppFuse+QuickStart
2、需要把各自的文件放到下面的目录
jdbc.properties (D:\passport\src\main\resources)
hibernate.reveng.xml(D:\passport\src\test\resources)
hibernate.cfg.xml (D:\passport\src\main\resources)把 appfuse 文件放到D:\passport\src\test\resources里面
3、下载源代码
mvn appfuse:full-source
4、根绝数据库新建的表 生成model
mvn appfuse:gen-model
8 生成service dao action
mvn appfuse:gen -Dentity=OperateLog
9 生成eclipse 项目
mvn eclipse:eclipse
jdbc.properties
jdbc.driverClassName=${jdbc.driverClassName} jdbc.url=${jdbc.url} jdbc.username=${jdbc.username} jdbc.password=${jdbc.password} hibernate.dialect=${hibernate.dialect} hibernatetool.metadatadialect=org.hibernate.cfg.reveng.dialect.OracleMetaDataDialect # Needed by Hibernate3 Maven Plugin defined in pom.xml hibernate.connection.username=${jdbc.username} hibernate.connection.password=${jdbc.password} hibernate.connection.url=${jdbc.url} hibernate.connection.driver_class=${jdbc.driverClassName}
hibernate.reveng.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-reverse-engineering SYSTEM "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd" > <hibernate-reverse-engineering> <schema-selection match-schema="PASSPORT"/> <type-mapping> <!-- jdbc-type is name fom java.sql.Types --> <sql-type jdbc-type="VARCHAR" length='1' hibernate-type="java.lang.String"/> <!-- length, scale and precision can be used to specify the mapping precisly --> <sql-type jdbc-type="NUMERIC" precision='1' hibernate-type="java.lang.Integer"/> <!-- the type-mappings are ordered. This mapping will be consulted last, thus overriden by the previous one if precision=1 for the column --> <sql-type jdbc-type="BIGINT" hibernate-type="java.lang.Integer"/> <sql-type jdbc-type="INTEGER" hibernate-type="java.lang.Integer"/> <sql-type jdbc-type="NUMERIC" hibernate-type="java.lang.Integer"/> </type-mapping> <!-- BIN$ is recycle bin tables in Oracle --> <table-filter match-name="BIN$.*" exclude="true"/> <!-- Exclude AppFuse tables from all catalogs/schemas --> <table-filter match-name="manager_user" exclude="true"/> <table-filter match-name="role" exclude="true"/> <table-filter match-name="manager_user_role" exclude="true"/> </hibernate-reverse-engineering>
模板参见下篇日志
附件:
1>AppFuse has a Maven Plugin that allows you to generate all the code in these tutorials. However, we recommend you complete these tutorials before using it so you're familiar with all the code that's being generated.
One of the nice features of AppFuse 2.x is you no longer need to create a DAO or Manager for type-safe CRUD functionality. You merely need to create Spring bean definitions. This means you should be able to easily develop a front-end without worrying about writing code for the backend (until you need customized functionality of course).
<2>What the full-source plugin does:
Exports all sources from Subversion into your project. It reads the dao.framework and web.framework properties to determine what you need.
Removeswarpathpluginfrompom.xml.
Calculatesdependenciesbyreadingpom.xmlfilesformthevariousAppFusemodules.Itreplacesyourdependencieswiththesenewones.TheorderofthedependenciesaddedisalphabeticalbasedongroupId.
ReadspropertiesfromtherootAppFusepom.xmlandaddstheonesthatdon'texisttoyourproject.
Renames packages to match your project's groupId.Ifyouwanttocustomizethecodegenerationtemplates(writteninFreeMarker),youcancopythem
intoyourprojectusingthefollowingcommand:
appfuse:copy-templates<3>
改成:
<sql-typejdbc-type="NUMERIC"hibernate-type="java.lang.Integer"/>
//那么生成的javabean 字段也是IntegerGenerates Java classes from existing database tables. If you want to customize the reverse engineering strategy,
youcanmodifythedefaulthibernate.reveng.xmlandputitinsrc/test/resources.
You can also override the location by specifying the "revengfile" property in the <configuration>. For example:<configuration>
<componentProperties>
<revengfile>path/to/hibernate.reveng.xml</revengfile>
</componentProperties>
<genericCore>${amp.genericCore}</genericCore>
<fullSource>${amp.fullSource}</fullSource>
</configuration>Oracle Users: If you create a custom hibernate.reveng.xml, make sure to capitalize the table names
in
<schema-selection match-schema="MY_SCHEMA_NAME"/>.You'll also need to add the following line to your jdbc.properties file:
hibernatetool.metadatadialect=org.hibernate.cfg.reveng.dialect.OracleMetaDataDialect
<4>
appfuse:copy-templates:CopiesFreeMarkertemplatesforCRUDgenerationintosrc/test/resources/appfuse.
Thesetemplatescanbecustomizedtofityourneeds.Youcandeletethetemplatesyoudon'tcustomizeand
the default templates will be used instead.<5>在DAO用ibatis时,我遇到的问题
执行:mvnappfuse:full-source
[ERROR]175002:svn:PROPFINDrequestfailedon'/svn/appfuse~svn/tags/APPFUSE_2.1.0/data/ibatis/src'
解决:
<groupId>org.appfuse.plugins</groupId>
<artifactId>appfuse-maven-plugin</artifactId>
<version>${appfuse.version}</version>
<configuration>
<genericCore>${amp.genericCore}</genericCore>
<fullSource>${amp.fullSource}</fullSource>
<trunk>https://svn.java.net/svn/appfuse~svn/</trunk>
</configuration>
即添加:
<trunk>https://svn.java.net/svn/appfuse~svn/</trunk>保留上面配置执行: mvn appfuse:copy-templates
svn:PROPFINDrequestfailedon'/svn/appfuse~svn/tags/APPFUSE_2.1.0/plugins/appfuse-maven-plugin/src/main/resources/appfuse/dao'
解决:
删除<trunk>https://svn.java.net/svn/appfuse~svn/</trunk><6>环境:ibatis+oracle
[INFO]Preparingappfuse:gen-model
[INFO][native2ascii:native2ascii{execution:native2ascii-utf8}]
[INFO][native2ascii:native2ascii{execution:native2ascii-8859_1}]
[INFO][resources:resources{execution:default-resources}]
[INFO]Using'UTF-8'encodingtocopyfilteredresources.
[INFO]Copying42resources
[INFO]Copying16resources
[INFO]Copying8resources
[INFO]Copying16resources
[INFO]Copying16resources
[WARNING]POMfor'org.apache.ibatis:ibatis-sqlmap:pom:${ibatis.version}:compile'i
Itsdependencies(ifany)willNOTbeavailabletothecurrentbuild.
[INFO][appfuse:gen-model{execution:default-cli}]
[INFO]Writing'hibernate.reveng.xml'totarget\test-classes\hibernate.reveng.xml\\可见model的产生需要借助
hibernate.reveng.xml<7>
HibernateException:Couldnotparseconfiguration:file:/D:/mavenTest/MyProj/src/main/resources/hibernate.cfg.xml
Causedby:org.dom4j.DocumentException:Connectiontimedout:connectNestedexception:Connectiontimedout:connect
atorg.dom4j.io.SAXReader.read(SAXReader.java:484)
atorg.hibernate.cfg.Configuration.doConfigure(Configuration.java:1518)
解决:
<!--NOTE:Ifyou'reworkingoffline,youmighthavetochangetheDOCTYPEtothefollowing:
<!DOCTYPEhibernate-configurationPUBLIC"-//Hibernate/HibernateConfigurationDTD3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">-->
《8》
[INFO] Request to merge when 'filtering' is not identical解决:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.6</version></plugin>//2.8好像有问题
<plugin><9>
mvnappfuse:gen-Dentiry=Person会生产Dao,service,..外,还会生产相应的测试代码.所以改变template要在这步
操作之前进行<9>
[INFO] Unable to find resource 'org.codehaus.mojo:mojo-sandbox:pom:2-SNAPSHOT' in repository appfuse-snapshots (http://oss.sonatype.org/content/reposi
tories/appfuse-snapshots)
[INFO]------------------------------------------------------------------------
原因: 自己不小心去掉了appfuse-maven-plugin <plugin> 的配置<10.
[ERROR]175002:svn:PROPFINDrequestfailedon'/svn/appfuse~svn/!svn/vcc/default'
[ERROR]175002:svn:cannotreadHTTPstatusline
[INFO]------------------------------------------------------------------------
原因: 可能是网络问题, 过分吧钟在试《9》
按官方说明来:用ibatis时,把hibernate3-maven-plugin换成sql-maven-plugin出现生成xxxSQL.xml文件错误
如:<resultproperty="userName"column="userName"/>
hibernate3-maven-plugin后:<resultproperty="userName"column="USER_NAME"/>正确
<10>
appfuse代码的自动生成,jpa注解,hibernate3-maven-plugin视乎不能少;
hibernate.reveng.xml映射类型也不能少
<11>
appfuse,oracle,ibatis时,生成xxxSQL.xml日期映射不正确.
如:<parameterproperty="birthdate"jdbcType="varchar"javaType="date"/>
解决:\MyProj\src\test\resources\appfuse\model\Ejb3PropertyGetAnnotation.ftl
注释:
<#-- ${pojo.generateBasicAnnotation(property)} --><12>
ERRORfreemarker.runtime-
Methodpublicjava.lang.Stringorg.hibernate.tool.hbm2x.pojo.EntityPOJOClass.generateJoinColumnsAnnotation
change${pojo.generateJoinColumnsAnnotation(property)}to${pojo.generateJoinColumnsAnnotation(property,cfg)}inEjb3PropertyGetAnnotation.ftl
tohaveaccesstoftlfilesyouhavetogettheywiththeappropriategoalappfuse:copy-templates
> mvn appfuse:gen-model with a db that as a table with a FK doesn't work《13>
oracle,务必要在jdbc.properties加:
hibernatetool.metadatadialect=org.hibernate.cfg.reveng.dialect.OracleMetaDataDialect
否则:hibernate.reveng.xml设置的属性无法生效