idea创建springboot项目+mybatis_Spring Boot + MyBatis 多模块项目搭建教程

Java后端,选择“”

优质文章,及时送达

bdeb19f6a2614fa32ad63406a83abd22.png

作者 | 枫本非凡

链接 | cnblogs.com/orzlin/p/9717399.html

上篇 | IDEA 远程一键部署 Spring Boot 到 Docker

一、前言

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

1、开发工具及系统环境

  • IDE:

    IntelliJ IDEA 2018.2

  • 系统环境:

    mac OSX

2、项目目录结构

  • biz层:

    业务逻辑层

  • dao层:

    数据持久层

  • web层:

    请求处理层

二、搭建步骤

1、创建父工程

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

d671e432b788f430b9f94e3565255bb8.png

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

a59e5df6795661b1c43ddb3ecddd1b06.png

填写输入框,点击Next

af8f8b128d37f7f27956e5ded021d0e0.png

这步不需要选择直接点Next

24086db48380307396f3524067c90260.png

点击Finish创建项目

491ce8fa900a817f9b42cb0d7b6e3f0a.png

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

957fbe7e3ee9a1275eec1ee7e764e7dd.png

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

0619339e5254ef7bf5f982c8fd7ba0be.png

2、创建子模块

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

d18d99913240baf0ac7859f3e1761625.png

选择Maven,点击Next

5c06445ca99386a3a925cc38e8e354da.png

填写ArifactId,点击Next

70dee9977bad8a91d8927eefd81b3f5e.png

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

78f62fc18111d75dc9210c15a4a32c6b.png

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

391f5d3800203b7e4c48d8eacaac8ec7.png

3、运行项目

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

@SpringBootApplicationpublic class BetaWebApplication {

public static void main(String[] args) { SpringApplication.run(BetaWebApplication.class, args); }}

在com.yibao.beta.web包中添加controller目录并新建一个controller,添加test方法测试接口是否可以正常访问

@RestController@RequestMapping("demo")public class DemoController {

@GetMapping("test") public String test { return "Hello World!"; }}

运行BetaWebApplication类中的main方法启动项目,默认端口为8080,访问http://localhost:8080/demo/test得到如下效果

ba57f8ce164388f669dac4f7081ed244.png

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

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

各个子模块的依赖关系:biz层依赖dao层,web层依赖biz层

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

com.yibao.beta beta-biz ${beta.version} com.yibao.beta beta-dao ${beta.version} com.yibao.beta beta-web ${beta.version}

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

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

com.yibao.beta beta-biz

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

com.yibao.beta beta-dao

4. web层调用biz层接口测试

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

public interface DemoService { String test;}

@Servicepublic class DemoServiceImpl implements DemoService {

@Override public String test { return "test"; }}

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

package com.yibao.beta.web.controller;@RestController@RequestMapping("demo")public class DemoController {

@Autowired private DemoService demoService;

@GetMapping("test") public String test { return demoService.test; }}

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

***************************APPLICATION FAILED TO START***************************

Description:Field demoService in com.yibao.beta.web.controller.DemoController required a bean of type 'com.yibao.beta.biz.service.DemoService' that could not be found.

Action:Consider defining a bean of type 'com.yibao.beta.biz.service.DemoService' in your configuration.

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

package com.yibao.beta.web;

@SpringBootApplication(scanBasePackages = "com.yibao.beta")@MapperScan("com.yibao.beta.dao.mapper")public class BetaWebApplication {

public static void main(String[] args) { SpringApplication.run(BetaWebApplication.class, args); }}

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

072915b1b40c315dd70b1428a2929a63.png

6. 集成Mybatis

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

dependencyManagement> org.mybatis.spring.boot mybatis-spring-boot-starter 1.3.2 org.projectlombok lombok 1.16.22

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

org.mybatis.spring.boot mybatis-spring-boot-starter mysql mysql-connector-java org.projectlombok lombok

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

8b0887fb0cc87983e0f8083911a0e8e2.png

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

spring.datasource.driverClassName = com.mysql.jdbc.Driverspring.datasource.url = jdbc:mysql://192.168.1.1/test?useUnicode=true&characterEncoding=utf-8spring.datasource.username = testspring.datasource.password = 123456

mybatis.mapper-locations = classpath:mybatis/*.xmlmybatis.type-aliases-package = com.yibao.beta.dao.entity

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

package com.yibao.beta.biz.service.impl;

@Servicepublic class DemoServiceImpl implements DemoService {

@Autowired private UserMapper userMapper;

@Override public String test { UserDO user = userMapper.selectByPrimaryKey(1); return user.toString; }}

再次运行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.

原因是找不到UserMapper类,此时需要在BetaWebApplication入口类中增加dao层包扫描,添加@MapperScan注解并设置其值为com.yibao.beta.dao.mapper,最终如下所示

package com.yibao.beta.web;

@SpringBootApplication(scanBasePackages = "com.yibao.beta")@MapperScan("com.yibao.beta.dao.mapper")public class BetaWebApplication {

public static void main(String[] args) { SpringApplication.run(BetaWebApplication.class, args); }}

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

a6eb6545f4c3d3c6fa54c972d6e82493.png

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

四、总结

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

此为项目重构的第一步,后续还会的框架中集成logback、disconf、redis、dubbo等组件

五、未提到的坑

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

-END-

如果看到这里,说明你喜欢这篇文章,请转发、点赞微信搜索「web_resource」,关注后回复「进群」或者扫描下方二维码即可进入无广告交流群。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值