Spring Boot + MyBatis 多模块项目搭建教程
一、前言
1、开发工具及系统环境
- IDE:IntelliJ IDEA 2020.2.2
- 系统环境:Windows
2、项目目录结构
biz层:业务逻辑层
dao层:数据持久层
web层:请求处理层
二、搭建步骤
1、创建父工程
选择Spring Initializr,Initializr默认选择Default,点击Next

点击Next

这一步不需要选择直接Next

点击Finish创建项目

最终得到的项目目录结构如下,删除无用的.mvn目录、src目录、mvnw及mvnw.cmd文件,最终只留.gitignore和pom.xml



2、创建子模块
选择项目根目录shop右键呼出菜单,选择New -> Module

选择Maven,点击Next

填写ArifactId,Module name增加横杠提升可读性,点击Finish

同理添加shop-dao、shop-web子模块,最终得到项目目录结构如下图

3、运行项目
在shop-web层创建com.dasheng.shop.web包(注意:这是多层目录结构并非单个目录名,com >> dasheng >> shop>> web)并添加入口类ShopWebApplication.java
@SpringBootApplication
public class ShopWebApplition {
public static void main(String[] args) {
SpringApplication.run(ShopWebApplition.class, args);
}
}
在com.dasheng.shop.web包中新建controller文件目录并新建一个controller,添加test方法测试接口是否可以正常访问
@RestController
@RequestMapping("demo")
public class Controller { @GetMapping("test")
public String test() {
return "Hello World!";
}
}
运行ShopWebApplication类中的main方法启动项目,默认端口为8080

访问http://localhost:8080/demo/test得到如下效果

以上虽然项目能正常启动,但是模块间的依赖关系却还未添加,下面继续完善。
4、配置模块间的依赖关系
各个子模块的依赖关系:
biz层依赖dao层,
web层依赖biz层
父pom文件中声明所有子模块依赖(dependencyManagement及dependencies的区别自行查阅文档)
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.dasheng.shop</groupId>
<artifactId>shop-biz</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.dasheng.shop</groupId>
<artifactId>shop-dao</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.dasheng.shop</groupId>
<artifactId>shop-web</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</dependencyManagement>
在shop-web层中的pom文件中添加shop-biz依赖
<dependencies>
<dependency>
<groupId>com.dasheng.shop</groupId>
<artifactId>shop-biz</artifactId>
</dependency>
</dependencies>
在shop-biz层中的pom文件中添加shop-dao依赖
<dependencies>
<dependency>
<groupId>com.dasheng.shop</groupId>
<artifactId>shop-dao</artifactId>
</dependency>
</dependencies>
5. web层调用biz层接口测试
在shop-biz层创建com.dasheng.shop.biz包,添加service目录并在其中创建DemoService接口类
public interface DemoService {
String test();
}
@Service("DemoService")
public class DemoServiceImpl implements DemoService {
@Override
public String test() {
return null;
}
}
com.dasheng.biz.web.controllerController通过@Autowired注解注入DemoService,修改DemoController的test方法使之调用DemoService的test方法,最终如下所示:
@Autowired
private DemoService demoService;
@GetMapping("test2")
public String test2() {
return demoService.test();
}
再次运行ShopWebApplication类中的main方法启动项目,发现如下报错
***************************
APPLICATION FAILED TO START
*************************** Description: Field demoService in com.dasheng.biz.web.controller.Controller required a bean of type 'com.dasheng.biz.service.DemoService' that could not be found. The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true) Action: Consider defining a bean of type 'com.dasheng.biz.service.DemoService' in your configuration. Process finished with exit code 1
原因是找不到DemoService类,此时需要在ShopWebApplication入口类中增加包扫描,设置@SpringBootApplication注解中的scanBasePackages值为com.dasheng.shop,最终如下所示

shop-web的pom加入
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
<version>5.1.27</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>

applicatio.yml配置 新建applicatio.yml配置文件:https://blog.csdn.net/weixin_44848573/article/details/106445457
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
username: root
password: root
url: jdbc:mysql://localhost:3306/test01?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true
@SpringBootApplication(scanBasePackages = "com.dasheng.biz")
@MapperScan("com.dasheng.biz.dao")
public class ShopWebApplition {
public static void main(String[] args) {
SpringApplication.run(ShopWebApplition.class, args);
}
}
完成!!

业务层直接调用(@Autowired)dao层(Mybatis)就完事了这不在叙述了,因为没有到入实体,项目放到gitee了,有需要的同学可以下载哦!
https://gitee.com/diaoyulin/shop.git
Spring Boot + MyBatis 多模块项目搭建教程的更多相关文章
- SpringBoot+Mybatis多模块项目搭建教程
一.前言 框架为SpringBoot+Mybatis,本篇主要记录了在IDEA中搭建SpringBoot多模块项目的过程. 1.开发工具及系统环境 IDE:IntelliJ IDEA 2018.2 系 ...
- Spring Boot + JPA 多模块项目无法注入 JpaRepository 接口
问题描述 Spring Boot + JPA 多模块项目,启动报异常: nested exception is org.springframework.beans.factory.NoSuchBean ...
- Spring框架学习笔记(8)——spring boot+mybatis plus+mysql项目环境搭建
之前写的那篇Spring框架学习笔记(5)--Spring Boot创建与使用,发现有多小细节没有提及,,正好现在又学习了mybatis plus这款框架,打算重新整理一遍,并将细节说清楚 1.通过I ...
- spring boot:构建多模块项目(spring boot 2.3.1)
一,为什么要使用多模块? 1,结构更清晰,方便管理 如果只是一个小项目当然没有问题, 但如果功能越增越多则管理越来越复杂, 多模块可以使项目中模块间的结构分离 2,把项目划分成多 ...
- 在Idea创建Spring Boot + MyBatis的web项目
创建步骤如下 选择Spring initializr 2. 修改group 与 atifact id,点击next 3. dependencies里面选择Web->Web , SQL -> ...
- Spring Boot 项目实战(一)Maven 多模块项目搭建
一.前言 最近公司项目准备开始重构,框架选定为 Spring Boot ,本篇主要记录了在 IDEA 中搭建 Spring Boot Maven 多模块项目的过程. 二.软件及硬件环境 macOS S ...
- Spring boot Mybatis整合构建Rest服务(超细版)
Springboot+ Mybatis+MySql整合构建Rest服务(涵盖增.删.改.查) 1.概要 1.1 为什么要使用Spring boot? 1.1.1 简单方便.配置少.整合了大多数框架 ...
- SpringBoot+Mybatis多模块(module)项目搭建教程
一.前言 最近公司项目准备开始重构,框架选定为SpringBoot+Mybatis,本篇主要记录了在IDEA中搭建SpringBoot多模块项目的过程. 1.开发工具及系统环境 IDE:Intelli ...
- spring boot+mybatis+quartz项目的搭建完整版
1. 利用spring boot提供的工具(http://start.spring.io/)自动生成一个标准的spring boot项目架构 2. 因为这里我们是搭建spring boot+mybat ...
随机推荐
- 微服务Cloud整体聚合工程创建过程
1.父工程创建及使用 使用idea开发工具,选择File-new- project ,在选项中选择Maven工程,选择jdk版本1.8,勾选maven-archetype-site,点击next,输入 ...
- 从零入门 Serverless | 一文详解 Serverless 技术选型
作者 | 李国强 阿里云资深产品专家 今天来讲,在 Serverless 这个大领域中,不只有函数计算这一种产品形态和应用类型,而是面向不同的用户群体和使用习惯,都有其各自适用的 Serverless ...
- 浅尝装饰器和AOP
[写在前面] 参考文章:https://www.cnblogs.com/huxi/archive/2011/03/01/1967600.html[从简单的例子入手进行讲解,由浅入深,很到位] 装饰器部 ...
- Java 读取PDF中的表格
一.概述 本文以Java示例展示读取PDF中的表格的方法.这里导入Spire.PDF for Javah中的jar包,并使用其提供的相关及方法来实现获取表格中的文本内容.下表中整理了本次代码使用到的主 ...
- 【Java虚拟机5】Java内存模型(硬件层面的并发优化基础知识--指令乱序问题)
前言 其实之前大家都了解过volatile,它的第一个作用是保证内存可见,第二个作用是禁止指令重排序.今天系统学习下为什么CPU会指令重排. 存储器的层次结构图 1.CPU乱序执行指令的根源 CPU读 ...
- 剑指offer:JZ12 矩阵中的路径
JZ12 矩阵中的路径 描述 请设计一个函数,用来判断在一个n乘m的矩阵中是否存在一条包含某长度为len的字符串所有字符的路径.路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上, ...
- 极速上手 VUE 3 —— teleport传送门组件
一.teleport 介绍 teleport 传送门组件,提供一种简洁的方式,可以指定它里面的内容的父元素.通俗易懂地讲,就是 teleport 中的内容允许我们控制在任意的DOM中,使用简单. 使用 ...
- [敏捷软工团队博客]The Agiles 团队介绍&团队采访
项目 内容 课程:北航-2020-春-敏捷软工 博客园班级博客 作业要求 团队作业-团队介绍和采访 团队名称来源 The Agile is The Agile. 敏捷就是敏捷.我们只是敏捷的践行者罢了 ...
- git常用的一些简单命令
1.如果一个文件被修改了,但是还没有使用 git add 命令,此时想取消这次修改,需要执行的命令如下: git checkout -- 文件名 2.如果一个文件执行了 git add ,此时想取消这 ...
- springboot读取配置文件中的信息
在一个项目中,我们有时候会把一些配置信息写入到一个配置文件中,在java代码中读取配置文件的信息.在此记录下读取属性文件中的内容. 在springboot项目中,springboot的配置文件可以使用 ...