原创地址:https://segmentfault.com/a/1190000005020589

我的DEMO码云地址,持续添加新功能: https://gitee.com/itbase/SpringBootDemo

Maven 搭建spring boot多模块项目


备注:所有项目都在idea中创建

1.idea创建maven项目
  • 1-1: 删除src,target目录,只保留pom.xml

  • 1-2: 根目录pom.xml可被子模块继承,因此项目只是demo,未考虑太多性能问题,所以将诸多依赖

    都写在根级`pom.xml`,子模块只需继承就可以使用。
  • 1-3: 根级pom.xml文件在附录1

  • 1-4: 依赖模块 mybatis spring-boot相关模块

2.创建子模块(module)
  • 2-1: file > new > module 输入 model

  • 2-2: file > new > module 输入 dao

  • 2-3: file > new > module 输入 service

  • 2-4: file > new > module 输入 webapi

3.修改子模块pom.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>parent</artifactId>
<groupId>com.luyh.projectv1</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion> <artifactId>projectv1-model</artifactId>
</project>

注意:<font color="red"><relativePath>../pom.xml</relativePath></font>此段必须加上,用来继承父模块


至此,项目的基础结构搭建完毕了,接下来可以来撸代码了,哦哦稍等,我先介绍下各个子module的工作职责吧

4.子模块在项目中担任的'工作职责'
  • model 此模块存放着所有的实体类

  • dao 此模块存放着数据交互的具体实现,供service调用

  • service 此模块存放业务代码实现,供API层调用

  • webapi 此模块也可以不出现在项目中,为了写demo故将webapi层放进来

5.model层实体类编写
  • 建立包名 com.luyh.projectv1.model

  • 建实体类 Member.java 具体代码请clone我的git,git地址在最下方

6.dao层数据库操作层
  • 建立com.luyh.projectv1.dao.config,该包内只有2个让spring boot自动加载配置的配置java类

  • 建立MemberMapper.java 具体内容看代码

  • 在resources/mybatis 下建立MemberMapper.xml

  • 建立IMember.java

  • 建立Member.java 实现Imember接口

  • 建立resources/application.properties文件用于配置数据库连接

7. service 编写业务逻辑
  • 建立 com.luyh.projectv1.service

  • 建立IMemberService.java接口

  • 建立MemberService.java实现类

  • MemberService.java 类中自动注入DaoMember 并调用其方法获取数据

8. webapi 编写webapi获取json数据
  • 建立Application.java 启动应用

  • 建立 com.luyh.projectv1.webapi.controller.MemberController.java 写个rest风格Controller

  • 启动

9.sql文件 请自行导入mysql数据 sql文件

这里是项目地址,点击下载

附录1

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.luyh.projectv1</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
</parent>
<modules> <module>model</module>
<module>dao</module>
<module>service</module>
<module>webapi</module>
</modules> <!--申明依赖关系-->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency> <dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
</dependency> <dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</dependency> <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies> <!--设置maven仓库--> <repositories>
<repository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories> </project>

Maven 搭建spring boot多模块项目(附源码),亲测可以,感谢原创的更多相关文章

  1. Maven 搭建spring boot多模块项目

    Maven 搭建spring boot多模块项目 备注:所有项目都在idea中创建 1.idea创建maven项目 1-1: 删除src,target目录,只保留pom.xml 1-2: 根目录pom ...

  2. Myeclipse下使用Maven搭建spring boot项目

    开发环境:Myeclipse2017.JDK1.6.Tomcat 8.0.Myeclipse下使用Maven搭建spring boot项目,详细过程如下: 1. New -> Project.. ...

  3. Spring Boot 多模块项目创建与配置 (一) (转)

    Spring Boot 多模块项目创建与配置 (一) 最近在负责的是一个比较复杂项目,模块很多,代码中的二级模块就有9个,部分二级模块下面还分了多个模块.代码中的多模块是用maven管理的,每个模块都 ...

  4. Spring Boot 多模块项目创建与配置 (一)

    最近在负责的是一个比较复杂项目,模块很多,代码中的二级模块就有9个,部分二级模块下面还分了多个模块.代码中的多模块是用maven管理的,每个模块都使用spring boot框架.之前有零零散散学过一些 ...

  5. 利用Intellij+MAVEN搭建Spring+Mybatis+MySql+SpringMVC项目详解

    http://blog.csdn.net/noaman_wgs/article/details/53893948 利用Intellij+MAVEN搭建Spring+Mybatis+MySql+Spri ...

  6. Spring Boot 多模块项目创建与配置 (转)

    转载:https://www.cnblogs.com/MaxElephant/p/8205234.html 最近在负责的是一个比较复杂项目,模块很多,代码中的二级模块就有9个,部分二级模块下面还分了多 ...

  7. Spring boot 多模块项目 + Swagger 让你的API可视化

    Spring boot 多模块项目 + Swagger 让你的API可视化 前言 手写 Api 文档的几个痛点: 文档需要更新的时候,需要再次发送一份给前端,也就是文档更新交流不及时. 接口返回结果不 ...

  8. 涨姿势:Spring Boot 2.x 启动全过程源码分析

    目录 SpringApplication 实例 run 方法运行过程 总结 上篇<Spring Boot 2.x 启动全过程源码分析(一)入口类剖析>我们分析了 Spring Boot 入 ...

  9. Spring Boot REST(二)源码分析

    Spring Boot REST(二)源码分析 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10117436.html) SpringBoot RE ...

随机推荐

  1. Win7如何解决telnet不是内部或外部命令的方案!

    https://jingyan.baidu.com/article/7908e85c6ec355af491ad265.html Telnet用于远程操作互联网中的设备或终端计算机服务器,可以有效的减少 ...

  2. 配置SSH服务使用证书登录Ubuntu服务器

    根据项目要求,需要将项目迁移到Linux系统上,作为测试,选用的是阿里云服务器,1核CPU,1G内存(没错就是这么穷),操作系统Ubuntu 16.04 64位.当然其实如果使用阿里云服务器其实是不需 ...

  3. jquery multi-select 实例demo

    运行效果: 其他的不多说了,都是用的jquery.multiSelect.js组件实现的,直接看代码吧 代码下载地址: http://download.csdn.net/detail/ajavabir ...

  4. 初识thinkphp(2)

    thinkphp的url路径的表示格式为 http://ip/tp/public/index.php/模块/控制器/操作 这里url最后的操作就是类里面的函数. 0x01:url访问格式 官方文档中有 ...

  5. 简单的Python 火车抢票程序

    当你想查询一下火车票信息的时候,你还在上12306官网吗?或是打开你手机里的APP?下面让我们来用Python写一个命令行版的火车票查看器, 只要在命令行敲一行命令就能获得你想要的火车票信息!如果你刚 ...

  6. jsonp的理解

    众所周知:在开发过程中,有时候需要客户端从服务器接收或向服务器发送一些数据:如果使用普通的ajax,则会遇到跨域访问无权限的问题. 要解决这个问题,就需要了解一下jsonp了: 1. ajax请求普通 ...

  7. UVA.11427.Expect the Expected(期望)

    题目链接 \(Description\) https://blog.csdn.net/Yukizzz/article/details/52084528 \(Solution\) 首先每一天之间是独立的 ...

  8. Consul + fabio 实现自动服务发现、负载均衡 - DockOne.io

    Consul + fabio 实现自动服务发现.负载均衡 - DockOne.io   http://dockone.io/article/1567

  9. STM32F4 Timer simplified block diagram

    Timers TIM1 and TIM8 use 16-bit counters and are the most complex timers of all timers included in t ...

  10. bitnami下mysql配置-包含phpMyAdmin配置

    mysql开启远程访问: 默认情况下mysql的绑定ip是bind-address=127.0.0.1 找到my.cnf bitnami@linux:~$ sudo find / -name my.c ...