Spring Boot + MyBatis 多模块项目搭建教程

一、前言

1、开发工具及系统环境

*
– IDE:IntelliJ IDEA 2020.2.2
– 系统环境:Windows

2、项目目录结构

*
– biz层:业务逻辑层
– dao层:数据持久层
– web层:请求处理层

二、搭建步骤

1、创建父工程

选择Spring Initializr,Initializr默认选择Default,点击Next

Spring Boot + MyBatis 多模块项目搭建教程

点击Next

Spring Boot + MyBatis 多模块项目搭建教程

这一步不需要选择直接Next

Spring Boot + MyBatis 多模块项目搭建教程

点击Finish创建项目

Spring Boot + MyBatis 多模块项目搭建教程

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

Spring Boot + MyBatis 多模块项目搭建教程

2、创建子模块

选择项目根目录shop右键呼出菜单,选择New -> Module

Spring Boot + MyBatis 多模块项目搭建教程

选择Maven,点击Next

Spring Boot + MyBatis 多模块项目搭建教程

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

Spring Boot + MyBatis 多模块项目搭建教程

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

Spring Boot + MyBatis 多模块项目搭建教程

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

Spring Boot + MyBatis 多模块项目搭建教程

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

Spring Boot + MyBatis 多模块项目搭建教程

以上虽然项目能正常启动,但是模块间的依赖关系却还未添加,下面继续完善。

4、 配置模块间的依赖关系

各个子模块的依赖关系:

biz层依赖dao层,

web层依赖biz层

父pom文件中声明所有子模块依赖(dependencyManagement及dependencies的区别自行查阅文档)

com.dasheng.shop            shop-biz            0.0.1-SNAPSHOT                            com.dasheng.shop            shop-dao            0.0.1-SNAPSHOT                            com.dasheng.shop            shop-web            0.0.1-SNAPSHOT

在shop-web层中的pom文件中添加shop-biz依赖

com.dasheng.shop
        shop-biz

在shop-biz层中的pom文件中添加shop-dao依赖

com.dasheng.shop
        shop-dao

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方法启动项目,发现如下报错

csharp;gutter:true;</p> <hr /> <p>APPLICATION FAILED TO START</p> <hr /> <p>Description:</p> <p>Field demoService in com.dasheng.biz.web.controller.Controller required a bean of type 'com.dasheng.biz.service.DemoService' that could not be found.</p> <p>The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true)</p> <p>Action:</p> <p>Consider defining a bean of type 'com.dasheng.biz.service.DemoService' in your configuration.</p> <p>Process finished with exit code 1</p> <pre><code> 原因是找不到DemoService类,此时需要在ShopWebApplication入口类中增加包扫描,设置@SpringBootApplication注解中的scanBasePackages值为com.dasheng.shop,最终如下所示 ![Spring Boot + MyBatis 多模块项目搭建教程](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230605/2151015-20211121213450201-1666173579.png) shop-web的pom加入 </code></pre> <p>org.mybatis.spring.boot mybatis-spring-boot-starter 1.1.1</p> <pre><code> mysql mysql-connector-java runtime 5.1.27 org.mybatis mybatis-spring 1.3.2 </code></pre> <pre><code> ![Spring Boot + MyBatis 多模块项目搭建教程](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230605/2151015-20211121213542959-629115408.png) applicatio.yml配置 新建applicatio.yml配置文件:https://blog.csdn.net/weixin_44848573/article/details/106445457 </code></pre> <p>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</p> <pre><code> </code></pre> <p>@SpringBootApplication(scanBasePackages = "com.dasheng.biz") @MapperScan("com.dasheng.biz.dao") public class ShopWebApplition { public static void main(String[] args) { SpringApplication.run(ShopWebApplition.class, args); } }

完成!!

Spring Boot + MyBatis 多模块项目搭建教程

业务层直接调用(@Autowired )dao层(Mybatis)就完事了这不在叙述了,因为没有到入实体,项目放到gitee了,有需要的同学可以下载哦!

https://gitee.com/diaoyulin/shop.git

Original: https://www.cnblogs.com/diaoyulin/p/15586139.html
Author: yl_diao
Title: Spring Boot + MyBatis 多模块项目搭建教程

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/576312/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • FastDFS客户端与django自定义文件存储系统

    1. FastDFS的Python客户端 python版本的FastDFS客户端使用说明参考https://github.com/jefforeilly/fdfs_client-p…

    数据库 2023年6月14日
    0117
  • COM组件 学习笔记

    COM组件是 以Win32动态链接库dll或可执行文件exe的形式发布的可执行代码组成的; COM组件是动态链接的,COM使用dll将组件动态链接起来; COM组件是语言无关的; …

    数据库 2023年6月14日
    0120
  • ShardingSphere 异构迁移最佳实践:将3.5亿量级的顾客系统 RTO 减少60倍

    Apache ShardingSphere 助力当当 3.5 亿用户量级顾客系统重构,由 PHP+SQL Server 技术栈无缝转型为 Java+ShardingSphere+M…

    数据库 2023年6月16日
    0158
  • MySQL增删改

    数据处理之增删改 插入数据(增) 前提:创建一个空表:id,name,hire_data,salary, 方法一:逐一添加数据 [En] method 1: add data on…

    数据库 2023年5月24日
    099
  • django中出现 错误 Errno 10053

    django中出现 错误 Errno 10053 pycharm里出现下面错误File “C:\Python27\lib\socket.py”, line …

    数据库 2023年6月9日
    0133
  • 23种设计模式之策略模式

    文章目录 概览 策略模式的优缺点 策略模式的应用场景 策略模式的结构与实现 * 模式的结构 模式的实现 策略模式的扩展 总结 ; 概览 策略模式定义了一系列算法,并将每个算法封装起…

    数据库 2023年6月6日
    0144
  • 记录不存在则插入,存在则更新 → MySQL 的实现方式有哪些?

    开心一刻 今天,爸爸、我和女儿一起吃了晚饭,我们每人都吃了一只鸡腿。 [En] Today, my father, me and my daughter had dinner to…

    数据库 2023年5月24日
    0147
  • SQL Server的Descending Indexes降序索引

    SQL Server的Descending Indexes降序索引 1、建立测试环境 测试环境:SQL Server 2012 表结构如下 USE [test] GO CREATE…

    数据库 2023年6月9日
    0126
  • MAC MySQL安装配置

    1. 下载 注意选择对应的版本,M系列芯片对应ARM 2. 安装 一直点击继续即可, 注意要记住root用户端密码 3. 配置 在 ~/.bash_profile 增加 4. 服务…

    数据库 2023年6月14日
    0135
  • JavaWeb-MVC、过滤器

    一、MVC架构图 Model 业务处理:业务逻辑(Service) 数据持久层:CRUD(Dao) View 展示数据 提供连接发起Servlet请求(a,form,img&#82…

    数据库 2023年6月16日
    0139
  • 数据火器库八卦系列之瑞士军刀随APP携带的SQLite

    来源:云数据库技术 数据库打工仔喃喃自语的八卦历史 为导弹巡洋舰设计,用在手机上的数据库 Small and Simple, and Better 如何看出是自己的娃:产品定位,特…

    数据库 2023年6月11日
    0130
  • MySQL8新增降序索引

    MySQL8新增降序索引 桃花坞里桃花庵,桃花庵里桃花仙。桃花仙人种桃树,又摘桃花卖酒钱。 一、MySQL5.7 降序索引 MySQL 在语法上很早就已经支持降序索引,但实际上创建…

    数据库 2023年5月24日
    0119
  • 07 sql函数

    函数:切记函数和括号要紧密相连内置函数1.算术函数abs mod roundmax min avg sum count 这几个为聚集函数,特别在分组中常用 select abs(-…

    数据库 2023年6月16日
    0121
  • Geoserver对发布的数据源进行金字塔切片

    一、建立切片数据源1.1建立工作区 1.2添加数据我这里是老师给的高清卫星地图数据,格式为tif工作区选择之前建立的工作区,浏览那里选择对应的文件 1.3建立切片源的图层这里建立的…

    数据库 2023年6月6日
    0130
  • docker部署redis集群

    docker部署redis集群 1.0 安装环境 1.1 安装Centos7 Docker官方建议在Ubuntu中安装,因为Docker是基于Ubuntu发布的,而且一般Docke…

    数据库 2023年6月9日
    0121
  • 博客园美化-随季节变化实现不同的飘落效果

    最近在研究博客园的美化效果,看到有一个樱花飘落的效果,忽然突发奇想,如果能根据当前日期所处的季节实现不同的飘落效果岂不是更酷。😂 最近在研究博客园的美化效果,看到有一个樱花飘落的效…

    数据库 2023年6月6日
    0155
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球