【java教程】实践MDA之AndroMDA教程:[第五节] 工程配置
当完成 MagicDraw 建模以后,接下来的事情就是生成代码了,但是此时生成的代码都是按照默认的设置进行的,我们往往需要对工程的配置进行修改和调整来满足各自的需求。
核心配置文件
工程中的 [root]\mda\conf\andromda.xml 文件是此代码生成工具的核心配置文件。它由多个namespace组成,每个namaspace下面包含了每个组件的配置信息。namespace包括default、spring、hibernate、java、bpm4struts等,其中default表示一些默认的全局的属性。在实际的工作中,我们需要根据实际情况对该配置文件进行修改。关于该文件可配置的详细内容,请参考官方网站:http://www.andromda.org 上文档。
举几个通常可能会修改的地方:
1、在default名字空间下面添加
<property name="defaultMultiplicity">0</property>
的属性,这样设计的持久对象属性默认全部可以为空。
2、在Spring名字空间下可以添加下面几个属性:
<property name="hibernateHbm2DDLAuto">update</property>
<property name="hibernateEnableCache">true</property>
<property name="applicationContext">carManagement- applicationContext.xml</property>
<property name="applicationContextDataSource">carManagement-applicationContext- dataSource.xml</property>
<property name="applicationContextLocalDataSource">carManagement- applicationContext- localDataSource.xml</property>
<property name="beanRefFactory">carManagementBeanRefFactory.xml</property>
<property name="beanRefFactoryId">carManagementBeanRefFactory</property>
hibernateHbm2DDLAuto属性表示自动更新数据库表结构,例如程序在执行的过程中,数据库还没有数据表,那么此时hibernate将会根据持久对象自动的构建数据库表;
hibernateEnableCache属性表示将使用hibernate的缓存功能,用来提高运行效率。
applicationContext、applicationContextDataSource、applicationContextLocalDataSource、beanRefFactory、beanRefFactoryId等几个属性表示自动生成的Spring配置文件的名称,具体每个配置文件的作用是什么,看看生成后的源代码就会明白。
3、在bpm4struts名字空间下面添加下面两个属性:
<property name="pageEncoding">gb2312</property>
<property name="mergeLocation">${maven.conf.dir}/cartridge/custom</property>
第一个属性表示生成的页面的默认编码为gb2312;第二个属性只有在需要配置生成模板的情况下才使用,如何修改模板在本节的最后部分会介绍。
数据库信息配置
在工程根目录下的存在一个 project.properties 文件,它用来配置数据库的信息。例如下面是对Mysql数据库的配置信息:
--------------------------------------------------------------------
# set this to true will deploy the war exploded
deployExploded=true
# The datasource for the application
# dataSource.name=DefaultDS
# dataSource=java:/${dataSource.name}
# Properties for management of the database schema,
# ignore if you setup/drop your schema manually
dataSource.driver.jar=D:/Tomcat 5.0/common/lib/mysql-connector-java-3.1.10-bin.jar
dataSource.driver.class=com.mysql.jdbc.Driver
dataSource.url=jdbc:mysql://localhost:3306/carsmanagement
dataSource.user=root
dataSource.password=mysql
dataSource.sql.init=core/target/schema-create.sql
dataSource.sql.drop=core/target/schema-drop.sql
dataSource.sql.load=core/target/db/create-dummy-load.sql
# What schema related goals should do when an error occurs.
dataSource.sql.onError=continue
# Change this to generate to the correct MDA database mappings
# For MySql use: MySQL
# For Hypersonic use: HypersonicSql
# For Oracle9i use: Oracle9i
sql.mappings=MySQL
# For MySql use: org.hibernate.dialect.MySQLDialect
# For Hypersonic use: org.hibernate.dialect.HSQLDialect
# For Oracle9i use: org.hibernate.dialect.Oracle9Dialect
hibernate.db.dialect=org.hibernate.dialect.MySQLDialect
--------------------------------------------------------------------
对于该配置文件,有非常重要的一点需要说明:如果设置了dataSource.name和dataSource两个属性,那么代码生成工具自动生成的Spring配置文件将使用这个数据源名称,而不使用dataSource.driver.jar、dataSource.driver.class、dataSource.url、dataSource.user、dataSource.password属性;如果没有设置dataSource.name和dataSource两个属性,那么代码生成工具自动生成的Spring配置文件将使用dataSource.driver.jar、dataSource.driver.class、dataSource.url、dataSource.user、dataSource.password属性。
如何修改模板
代码生成工具使用了Velocity模板来生成java文件、jsp文件以及相关的配置文件等各种资源。当我们觉得它生成的格式不满足我们的要求时,我们可以修改模板实现,通过我们会修改JSP模板、配置文件模板等。具体步骤如下:
1、编辑/mda/conf/andromda.xml文件,在bpm4struts的名字空间下面的属性列表中添加下面两个属性:
<!-- add by widen -->
<property name="pageEncoding">gb2312</property>
<property name="mergeLocation">${maven.conf.dir}/cartridge/custom</property>
其中mergeLocation属性表示自定义的模板文件的存放位置。
2、如果需要在web.xml文件中生成其他内容,则需要编辑/mda/conf/mappings/WebMergeMappings.xml文件,例如我们需要加入和关于编码的filter,则需要在该文件的filter merge- point的地方添加如下代码:
<filter>
<filter-name>encodingFilter</filter-name>
<filter- class>org.springframework.web.filter.CharacterEncodingFilter</filter- class>
<init-param>
<param-name>encoding</param-name>
<param-value>gb2312</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
然后在filter-mapping merge-point的地方添加如下代码:
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
在我的CarManagement示例工程里面已经对模板的基本修改做好了设置,因此如果你获取到了我的CarManagement示例工程文件夹接下来的几步将帮助你更加快速的进行模板修改。如果需要示例工程,可以用 email (delight.wjk@gmail.com) 给我联系。
3、把CarManagement示例工程中的/mda/conf/下的cartridge文件夹拷贝到当前工程的对应的目录下面,并可以根据实际需要在此基础之上进行修改。
4、把CarManagement示例工程中的/web/src/下的jsp文件夹拷贝到当前工程的对应的目录下面。因为CarManagement示例工程里面已经对模板的基本修改做好了设置,拷贝过来直接使用即可,并可以根据需要在此基础之上进行修改。
5、如果你是使用 maven 的 andromdapp:generate 命令生成工程目录结构的,你会在 /web/maven.xml 文件中发现有一个 war:init 命令的部分被默认的注释起来了,取消这个注释后,代码自动生成工具将自动拷贝 /web/src/jsp 下面的所有文件到自动生成war包中。
6、在拷贝后的cartridge目录中替换字符串:将“CarManagement”替换为当前工程的名称,注意整字匹配,并且匹配大小写。
7、在拷贝后的jsp目录中替换字符串:将“CarManagement”替换为当前工程的名称,注意整字匹配,并且匹配大小写。
到这里,关于工程配置就介绍完了,下一节将介绍生成代码的常用命令,并讲解如何集成到 eclipse 开发环境中。

1 条评论:
c语言示例代码学习编程
c计算多边形面积的示例
发表评论