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

发布时间:2022-06-29 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了Spring Boot + MyBatis 多模块项目搭建教程脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

一、前言

  1、开发工具及系统环境

 

    •   IDE:IntelliJ IDEA 2020.2.2 
    •   系统环境:Windows

 

  2、项目目录结构

    •   biz层:业务逻辑层

    •   DAO层:数据持久层

    •   web层:请求处理层

二、搭建步骤

  1、创建父工程

               

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

   

         选择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 多模块项目搭建教程

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

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的区别自行查阅文档)

<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,最终如下所示

     

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

   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>

 

 

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

 

 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);
    }
}

完成!!

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

 

 

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

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

 

脚本宝典总结

以上是脚本宝典为你收集整理的Spring Boot + MyBatis 多模块项目搭建教程全部内容,希望文章能够帮你解决Spring Boot + MyBatis 多模块项目搭建教程所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。