SpringBoot多模块开发

       这篇文章主要介绍SpringBoot多模块开发,众所周知,在多个项目中可能会相同的模块,如果每个项目都去创建一遍的话,这样开发效率会很低。比如在开发一个APP应用的时候,有供APP使用的接口项目、后台管理系统,两个项目共用一套数据库,分开的话需要配置多次mybatis,如果有些表需要改动的话,则都需要改动,所以使用多模块管理这些模块的话,会非常的方便。

        那我们如何分模块呢,首先,应该有个parent模块,主要用来引用项目中使用的各种jar包;第二,将工程中使用的公共Utils工具类作为一个独立的模块;第三,就是以MVC三层架构,将controller/service/dao层分为三个模块

      接下来主要讲在InteliJ IDEA中创建SpringBoot多模块项目:

     第一步:创建maven工程,File - New - Project,打开如下窗口:

SpringBoot多模块开发_第1张图片

选择maven,然后点击下一步

SpringBoot多模块开发_第2张图片

然后输入maven坐标,点击下一步

SpringBoot多模块开发_第3张图片

点击Finish,结果如下图:

SpringBoot多模块开发_第4张图片

然后删除项目中的所有文件 ,包括pom文件

SpringBoot多模块开发_第5张图片

然后File - New - Module,创建parent模块


SpringBoot多模块开发_第6张图片


SpringBoot多模块开发_第7张图片

删除collector-parent项目中的src文件夹,并在pom文件中加入pom


SpringBoot多模块开发_第8张图片

然后选中collector-parent,并右击,新建collector-common模块和collector-manager模块,其他collector-common存放公共工具代码,collector-manager中用来管理MVC三层模块:

SpringBoot多模块开发_第9张图片

删除collector-manager项目中的src目录,并在pom文件中加入pom

用同样的方法在collector-manager下创建collector-manager-dao,和collector-manager-service,在dao层中加入collector-common依赖,在collector-manager-service中加入collector-manager-dao层的依赖,如下图所示:

SpringBoot多模块开发_第10张图片
项目结构
SpringBoot多模块开发_第11张图片
collector-manager-dao层xml


SpringBoot多模块开发_第12张图片
collector-manager-service层xml

最后是创建Controller层模块,创建springboot项目(根据需求可以创建多个springboot模块),右击collector-manager,选择new -Module ,如图选择Spring Initializr

SpringBoot多模块开发_第13张图片

下一步,填写相关项目信息:

SpringBoot多模块开发_第14张图片

点击下一步,选择相应的模块,


SpringBoot多模块开发_第15张图片


点击下一步


SpringBoot多模块开发_第16张图片

点击Finish,并将collector-manager-web项目中的parent修改为collector-parent,并删除groupId和version标签,然后添加collector-manager-service依赖,并将springboot中的相关包移入到collector-parent中,最终collector-manager-web中的pom文件如下:

SpringBoot多模块开发_第17张图片

你可能感兴趣的:(SpringBoot多模块开发)