首页 > 代码库 > intelliJ idea创建分层的项目结构

intelliJ idea创建分层的项目结构


原文地址:http://yanwushu.sinaapp.com/create_layered_project_in_idea/


本文使用intelliJidea 14

在idea中创建一个分层(视图层、业务逻辑层、数据访问层)的项目步骤如下:

1.      创建一个project,idea在创建project的时候会默认创建一个module,此时,给这个project和module命名(比如hello_client,表示这是项目的客户端也就是视图层),并且指定项目存放路径。

2.      新建一个module,新建的时候路径指定为和上面的hello_client同级。命名为hello_service,表明这是项目的业务逻辑层。

3.      新建一个module,命名为hello_dal,表示这是项目的数据访问层,同上,此module的目录指定为和hello_client、hello_service同级。

4.      此时在project窗口中会看到并列的三个module。开发的时候就可以在不同的层次上放置不同的代码。如下图:

技术分享

图:三个同级的module代表项目中的三个不同层

5.      在项目中hello_client会引用hello_service的代码,而hello_service会引用hello_dal的代码。在idea中添加引用的方式为:

a)        选中任意一个module,按F4进入project structure;

b)        选择左侧的modules菜单;

c)        选择中间的hello_client,选中右侧的dependencies选项卡,点击右侧的绿色+按钮,选择modeule dependecy,在弹出的选择框中选择hello_service。

d)        这样就为hello_client添加了hello_service的引用。直接在hello_client中引用hello_service的代码即可。

e)        同理,添加hello_service对hello_dal的引用。

技术分享

图:hello_client添加对hello_service的引用

 

intelliJ idea创建分层的项目结构