我们现在的需求是:希望当接到一个新的工程项目时,程序员只需要根据项目需求,设计出数据库的结构(表,表的字段,表间关系),之后只要按一个按键,就能立刻得到基于该数据库的所有分层架构代码文件.也就是说,我们希望能在不敲一行代码的情况下获取所有我们想要的代码!而这就是这2个软件能做到的事情,也是我们安装它们的目的.
好了,角色已经介绍完了,接着看看它们是如何演绎这场精彩的演出的吧:
首先我们需要创建一个测试数据库,为了简单,我们在这个数据库中只创建一个Student表和一个Major表.其表结构和关系如下所示.

看看怎样用PowerDesigner快速的创建出这个数据库吧.
1.现在开始使用PowerDesigner创建数据库,首先运行程序,进入主界面:

2.File—New Model—Physical Data Model—Physical Diagram—Model name 设置为test,DBMS属性设置为Microsoft SQL Server 2005:

3.首先用表格工具创建一个表格模板:

4.双击表格模板,设置属性,我们首先设置Major表:

5.设置好表名,点击Columns标签,设置字段属性,设置如图所示:

6.因为MajorID字段我们要设置为自动增长,所以要设置它的高级属性,选择MajorID字段,点击属性按钮,在General面板中勾选上Identity复选框:

7.确定后我们再创建一个Student表,字段设置如图所示:

8.接着是为Student创建一个MajorID外键,使用PowerDesigner可以很轻松的完成这个工作,选择关系设置工具,在Student表上按住左键不放,拖拽至Major表,便可为Student表添加一个MajorID的外键:

9.哈哈,现在测试表已经设置好了,接着设置一下我们要生成的数据库吧,这些表都将被创建到该数据库中,我们在设计面板空白处右键—Properties,在弹出的属性设置对话框设置如下:

10好了,在此我们对新数据库的设置已经完成,但是在SQL中还是空空如也啊,我们要怎么把这边设计好的结构移植到SQLServer 2005中呢?执行操作:Database—Generate Database,设置好存储过程导出目录和文件名,点击确定即可:

11.到你的导出目录,就可以看见导出的数据库创建存储过程了,打开SQL,执行一下,你就会看到数据库被神奇的创建好了:


12.好了,数据库的准备工作做好了,下一篇我们就将使用该数据库结合CodeSmith自动批量生成代码啦~
上述实践中生成的源代码:


SQL.sql
/*==============================================================*/
/* Database name: PD_test */
/* DBMS name: Microsoft SQL Server 2005 */
/* Created on: 2010/6/13 星期日 17:27:17 */
/*==============================================================*/
dropdatabase PD_test
go
/*==============================================================*/
/* Database: PD_test */
/*==============================================================*/
createdatabase PD_test
go
use PD_test
go
/*==============================================================*/
/* Table: Major */
/*==============================================================*/
createtable Major (
MajorID intidentity,
Name nvarchar(20) notnull,
Remark nvarchar(Max) null,
constraint PK_MAJOR primarykey (MajorID)
)
go
/*==============================================================*/
/* Table: Student */
/*==============================================================*/
createtable Student (
StudentID nvarchar(20) notnull,
MajorID intnull,
Name nvarchar(20) notnull,
Sex bitnotnull,
Age intnull,
Remark nvarchar(Max) null,
constraint PK_STUDENT primarykey (StudentID)
)
go
altertable Student
addconstraint FK_STUDENT_REFERENCE_MAJOR foreignkey (MajorID)
references Major (MajorID)
go