springboot启动不了_Spring Boot + MyBatis 多模块搭建教程

作者:枫本非凡

来源:www.cnblogs.com/orzlin/p/9717399.html

一、前言

1、创建父工程

最近公司项目准备开始重构,框架选定为 SpringBoot + Mybatis,本篇主要记录了在IDEA中搭建 Spring Boot 多模块项目的过程。

1、开发工具及系统环境

  • IDE:
  • IntelliJ IDEA 2018.2
  • 系统环境:mac OSX

2、项目目录结构

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

二、搭建步骤

1、创建父工程

IDEA 工具栏选择菜单 File -> New -> Project...

137a7d0fc6b3a583f61d1dd0db2610a7.png

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

springboot启动不了_Spring Boot + MyBatis 多模块搭建教程_第1张图片

填写输入框,点击Next

springboot启动不了_Spring Boot + MyBatis 多模块搭建教程_第2张图片

这步不需要选择直接点Next

springboot启动不了_Spring Boot + MyBatis 多模块搭建教程_第3张图片

点击Finish创建项目

springboot启动不了_Spring Boot + MyBatis 多模块搭建教程_第4张图片

最终得到的项目目录结构如下

springboot启动不了_Spring Boot + MyBatis 多模块搭建教程_第5张图片

删除无用的.mvn目录、src目录、mvnw及mvnw.cmd文件,最终只留.gitignore和pom.xml

springboot启动不了_Spring Boot + MyBatis 多模块搭建教程_第6张图片

2、创建子模块

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

springboot启动不了_Spring Boot + MyBatis 多模块搭建教程_第7张图片

选择Maven,点击Next

springboot启动不了_Spring Boot + MyBatis 多模块搭建教程_第8张图片

填写ArifactId,点击Next

springboot启动不了_Spring Boot + MyBatis 多模块搭建教程_第9张图片

修改Module name增加横杠提升可读性,点击Finish

springboot启动不了_Spring Boot + MyBatis 多模块搭建教程_第10张图片

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

springboot启动不了_Spring Boot + MyBatis 多模块搭建教程_第11张图片

3、运行项目

在beta-web层创建com.yibao.beta.web包(注意:这是多层目录结构并非单个目录名,com >> yibao >> beta >> web)并添加入口类BetaWebApplication.java

springboot启动不了_Spring Boot + MyBatis 多模块搭建教程_第12张图片

运行BetaWebApplication类中的main方法启动项目,默认端口为8080,访问

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

springboot启动不了_Spring Boot + MyBatis 多模块搭建教程_第13张图片

以上虽然项目能正常启动,但是模块间的依赖关系却还未添加,下面继续完善。微信搜索 web_resource 获取更多推送

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

各个子模块的依赖关系:biz层依赖dao层,web层依赖biz层父pom文件中声明所有子模块依赖(dependencyManagement及dependencies的区别自行查阅文档)

springboot启动不了_Spring Boot + MyBatis 多模块搭建教程_第14张图片

其中${beta.version}定义在properties标签中

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

          com.yibao.betagroupId>          beta-bizartifactId>  

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

          com.yibao.betagroupId>          beta-daoartifactId>  

5 web层调用biz层接口测试

在beta-biz层创建com.yibao.beta.biz包,添加service目录并在其中创建DemoService接口类,微信搜索 web_resource 获取更多推送

springboot启动不了_Spring Boot + MyBatis 多模块搭建教程_第15张图片

DemoController通过@Autowired注解注入DemoService,修改DemoController的test方法使之调用DemoService的test方法,最终如下所示:

springboot启动不了_Spring Boot + MyBatis 多模块搭建教程_第16张图片

再次运行BetaWebApplication类中的main方法启动项目,发现如下报错

springboot启动不了_Spring Boot + MyBatis 多模块搭建教程_第17张图片

原因是找不到DemoService类,此时需要在BetaWebApplication入口类中增加包扫描,设置@SpringBootApplication注解中的scanBasePackages值为com.yibao.beta,最终如下所示

springboot启动不了_Spring Boot + MyBatis 多模块搭建教程_第18张图片

设置完后重新运行main方法,项目正常启动,访问http://localhost:8080/demo/test得到如下效果

springboot启动不了_Spring Boot + MyBatis 多模块搭建教程_第19张图片

6. 集成Mybatis

父pom文件中声明mybatis-spring-boot-starter及lombok依赖

springboot启动不了_Spring Boot + MyBatis 多模块搭建教程_第20张图片

在beta-dao层中的pom文件中添加上述依赖

springboot启动不了_Spring Boot + MyBatis 多模块搭建教程_第21张图片

在beta-dao层创建com.yibao.beta.dao包,通过mybatis-genertaor工具生成dao层相关文件(DO、Mapper、xml),存放目录如下

springboot启动不了_Spring Boot + MyBatis 多模块搭建教程_第22张图片

applicatio.properties文件添加jdbc及mybatis相应配置项

springboot启动不了_Spring Boot + MyBatis 多模块搭建教程_第23张图片

DemoService通过@Autowired注解注入UserMapper,修改DemoService的test方法使之调用UserMapper的selectByPrimaryKey方法,最终如下所示

springboot启动不了_Spring Boot + MyBatis 多模块搭建教程_第24张图片

再次运行BetaWebApplication类中的main方法启动项目,发现如下报错

APPLICATION FAILED TO START***************************Description:Field userMapper in com.yibao.beta.biz.service.impl.DemoServiceImpl required a bean of type 'com.yibao.beta.dao.mapper.UserMapper' that could not be found.Action:Consider defining a bean of type 'com.yibao.beta.dao.mapper.UserMapper' in your configuration.
springboot启动不了_Spring Boot + MyBatis 多模块搭建教程_第25张图片

设置完后重新运行main方法,项目正常启动,访问http://localhost:8080/demo/test得到如下效果

springboot启动不了_Spring Boot + MyBatis 多模块搭建教程_第26张图片

至此,一个简单的 Spring Boot + Mybatis 多模块项目已经搭建完毕,我们也通过启动项目调用接口验证其正确性。

三、总结

一个层次分明的多模块工程结构不仅方便维护,而且有利于后续微服务化。在此结构的基础上还可以扩展common层(公共组件)、server层(如dubbo对外提供的服务)微信搜索 web_resource 获取更多推送此为项目重构的第一步,后续还会的框架中集成logback、disconf、redis、dubbo等组件。

四、未提到的坑

在搭建过程中还遇到一个maven私服的问题,原因是公司内部的maven私服配置的中央仓库为阿里的远程仓库,它与maven自带的远程仓库相比有些jar包版本并不全,导致在搭建过程中好几次因为没拉到相应jar包导致项目启动不了。

你可能感兴趣的:(springboot启动不了)