SpringBoot+Mybatis多模块(module)项目搭建教程
作者:枫本非凡
cnblogs.com/orzlin/p/9717399.html
编辑:Java知音
一、前言
最近公司项目准备开始重构,框架选定为SpringBoot+Mybatis,本篇主要记录了在IDEA中搭建SpringBoot多模块项目的过程。
1、开发工具及系统环境
IDE:IntelliJ IDEA 2018.2
系统环境:mac OSX
2、项目目录结构
biz层:业务逻辑层
dao层:数据持久层
web层:请求处理层
二、搭建步骤
1、创建父工程
① IDEA 工具栏选择菜单 File -> New -> Project…
② 选择Spring Initializr,Initializr默认选择Default,点击Next
③ 填写输入框,点击Next
④ 这步不需要选择直接点Next
⑤ 点击Finish创建项目
⑥ 最终得到的项目目录结构如下
⑦ 删除无用的.mvn目录、src目录、mvnw及mvnw.cmd文件,最终只留.gitignore和pom.xml
2、创建子模块
① 选择项目根目录beta右键呼出菜单,选择New -> Module
② 选择Maven,点击Next
③ 填写ArifactId,点击Next
④ 修改Module name增加横杠提升可读性,点击Finish
⑤ 同理添加【beta-dao】、【beta-web】子模块,最终得到项目目录结构如下图
3、运行项目
① 在beta-web层创建com.yibao.beta.web包(注意:这是多层目录结构并非单个目录名,com >> yibao >> beta >> web)并添加入口类BetaWebApplication.java
package com.yibao.beta.web;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;/*** @author linjian* @date 2018/9/29*/
@SpringBootApplication
public class BetaWebApplication {public static void main(String[] args) {SpringApplication.run(BetaWebApplication.class, args);}
}
② 在com.yibao.beta.web包中添加controller目录并新建一个controller,添加test方法测试接口是否可以正常访问
package com.yibao.beta.web.controller;import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/*** @author linjian* @date 2018/9/29*/
@RestController
@RequestMapping("demo")
public class DemoController {@GetMapping("test")public String test() {return "Hello World!";}
}
③ 运行BetaWebApplication类中的main方法启动项目,默认端口为8080,访问http://localhost:8080/demo/test得到如下效果
以上虽然项目能正常启动,但是模块间的依赖关系却还未添加,下面继续完善
4、配置模块间的依赖关系
各个子模块的依赖关系:biz层依赖dao层,web层依赖biz层
① 父pom文件中声明所有子模块依赖(dependencyManagement及dependencies的区别自行查阅文档)
<dependencyManagement><dependencies><dependency><groupId>com.yibao.beta</groupId><artifactId>beta-biz</artifactId><version>${beta.version}</version></dependency><dependency><groupId>com.yibao.beta</groupId><artifactId>beta-dao</artifactId><version>${beta.version}</version></dependency><dependency><groupId>com.yibao.beta</groupId><artifactId>beta-web</artifactId><version>${beta.version}</version></dependency></dependencies>
</dependencyManagement>
其中${beta.version}定义在properties标签中
② 在beta-web层中的pom文件中添加beta-biz依赖
<dependencies><dependency><groupId>com.yibao.beta</groupId><artifactId>beta-biz</artifactId></dependency>
</dependencies>
③ 在beta-biz层中的pom文件中添加beta-dao依赖
<dependencies><dependency><groupId>com.yibao.beta</groupId><artifactId>beta-dao</artifactId></dependency>
</dependencies>
5、web层调用biz层接口测试
① 在beta-biz层创建com.yibao.beta.biz包,添加service目录并在其中创建DemoService接口类
package com.yibao.beta.biz.service;/*** @author linjian* @date 2018/9/29*/
public interface DemoService {String test();
}
package com.yibao.beta.biz.service.impl;import com.yibao.beta.biz.service.DemoService;
import org.springframework.stereotype.Service;/*** @author linjian* @date 2018/9/29*/
@Service
public class DemoServiceImpl implements DemoService {@Overridepublic String test() {return "test";}
}
② DemoController通过@Autowired注解注入DemoService,修改DemoController的test方法使之调用DemoService的test方法,最终如下所示
package com.yibao.beta.web.controller;import com.yibao.beta.biz.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/*** @author linjian* @date 2018/9/29*/
@RestController
@RequestMapping("demo")
public class DemoController {@Autowiredprivate 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;import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;/*** @author linjian* @date 2018/9/29*/
@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得到如下效果
6、集成Mybatis
① 父pom文件中声明mybatis-spring-boot-starter及lombok依赖
dependencyManagement><dependencies><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.3.2</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.16.22</version></dependency></dependencies>
</dependencyManagement>
② 在beta-dao层中的pom文件中添加上述依赖
<dependencies><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency>
</dependencies>
③ 在beta-dao层创建com.yibao.beta.dao包,通过mybatis-genertaor工具生成dao层相关文件(DO、Mapper、xml),存放目录如下
④ applicatio.properties文件添加jdbc及mybatis相应配置项
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://192.168.1.1/test?useUnicode=true&characterEncoding=utf-8
spring.datasource.username = test
spring.datasource.password = 123456mybatis.mapper-locations = classpath:mybatis/*.xml
mybatis.type-aliases-package = com.yibao.beta.dao.entity
⑤ DemoService通过@Autowired注解注入UserMapper,修改DemoService的test方法使之调用UserMapper的selectByPrimaryKey方法,最终如下所示
package com.yibao.beta.biz.service.impl;import com.yibao.beta.biz.service.DemoService;
import com.yibao.beta.dao.entity.UserDO;
import com.yibao.beta.dao.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;/*** @author linjian* @date 2018/9/29*/
@Service
public class DemoServiceImpl implements DemoService {@Autowiredprivate UserMapper userMapper;@Overridepublic 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;import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;/*** @author linjian* @date 2018/9/29*/
@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得到如下效果
至此,一个简单的SpringBoot+Mybatis多模块项目已经搭建完毕,我们也通过启动项目调用接口验证其正确性。
四、总结
一个层次分明的多模块工程结构不仅方便维护,而且有利于后续微服务化。在此结构的基础上还可以扩展common层(公共组件)、server层(如dubbo对外提供的服务)
此为项目重构的第一步,后续还会的框架中集成logback、disconf、redis、dubbo等组件
五、未提到的坑
在搭建过程中还遇到一个maven私服的问题,原因是公司内部的maven私服配置的中央仓库为阿里的远程仓库,它与maven自带的远程仓库相比有些jar包版本并不全,导致在搭建过程中好几次因为没拉到相应jar包导致项目启动不了。
SpringBoot+Mybatis多模块(module)项目搭建教程相关推荐
- idea创建springboot项目+mybatis_Spring Boot + MyBatis 多模块项目搭建教程
Java后端,选择"" 优质文章,及时送达 作者 | 枫本非凡 链接 | cnblogs.com/orzlin/p/9717399.html 上篇 | IDEA 远程一键部署 Sp ...
- 快速简洁的Vue+Electron项目搭建教程
Vue+Electron项目搭建教程 最近写一个项目,需要使用Electron集成Vue,特此记录搭建过程 Vue+Electron 常用搭建方式 在之前,通常我们会使用electron-vue脚手架 ...
- java idea 模块_IDEA搭建java多模块module项目-Go语言中文社区
1.新建项目 2.用maven创建多模块项目 点击next进入下一步 3.建立groupId,artifactId,version信息 4.建项目名与项目位置 点击finish后进入第5步 5.新建项 ...
- 47K Star 的SpringBoot+MyBatis+docker电商项目,附带超详细的文档!
点击上方 好好学java ,选择 星标 公众号重磅资讯,干货,第一时间送达 今日推荐:推荐19个github超牛逼项目!个人原创100W +访问量博客:点击前往,查看更多 简介 该项目是一套电商系统, ...
- SpringBoot个人博客从无到有项目搭建——实战综合介绍
SpringBoot个人博客项目搭建 博客Github源码:https://github.com/lindaifeng/QingFengInn-blog 个人博客主页:http://www.qingf ...
- 纯国产环境JAVA程序(Springboot + Mybatis + 达梦数据库)搭建
目录 JAVA程序搭建 前言 达梦数据库安装(WINDOWS) SpringBoot项目搭建 整合达梦数据库 源码 总结 JAVA程序搭建 前言 写在前面: 文章满满干货,每一步都是自己从零开始操作并 ...
- Springboot整合eureka父子项目——多模块module项目,即微服务项目
父项目 父工程也是一个POM工程,该工程中定义了所有平时常用jar包的版本和依赖管理,以及一些常用插件的管理. 到此我们可以看出springboot项目继承spring-boot-starter-pa ...
- SpringBoot+MyBatis+MySQ二手商城项目
1.项目功能:登录.注册.热销商品.用户管理(密码.个人信息.头像.收货地址).购物车(展示.增加.删除).订单模块. 2.开发顺序:注册.登录.用户管理.购物车.商品.订单模块. 3.某一个模块的开 ...
- react项目搭建教程
搭建步骤 安装环境 参考 菜鸟教程[https://www.runoob.com/nodejs/nodejs-tutorial.html] 安装所需要的node环境 创建项目 环境安装好之后我们进行项 ...
最新文章
- 碰到故障大全---cd
- ICCV 2021 Oral | 无需法向的大场景点云表面重建
- 28篇标志性论文见证「自然语言处理NLP」2019-2020年度亮点进展
- hp服务器ilo批量配置脚本
- 「超全」工欲善其事必先利其器!
- boost::python模块实现使用任意 Python 序列创建 ndarray 的示例
- python多进程内存共享_python 归纳 (二十)_多进程数据共享和同步_共享内存Value Array...
- 了解Spring Web初始化
- Java 正则表达式 工具类 中文 英文 email 手机号 身份证 数字 日期
- fafu oj 1266 数数
- CocoaPods 安装
- 在CRM中怎么应用大数据挖掘
- ffmpeg下载视频
- rom大小 stm32f205_最小的基于STM32F205VE的系统板
- 3串口多串口双串口以及2串口转WiFi多跳路由通讯实现一
- java编写简单聊天界面_java实现简单聊天室单人版
- C++ _stdcall和__stdcall、_fastcall的区别
- 图片太大怎么压缩变小?教你四招快捷压缩图片
- UVM糖果爱好者教程 - 31.provides_responses?
- Android Camera旋转角度
热门文章
- C#线程同步(1)- 临界区&Lock
- 2003 SERVER 本地连接 TCP/IP问题[转]
- Bluetooth GAP介绍
- C++ Primer 5th笔记(chap 16 模板和泛型编程)类模板部分特例化
- 跨链(2)跨链技术“侧链(Sidechains)”
- [ARM异常]-ARMV8-aarch32的异常向量表介绍
- [architecture]-AMBA AXI AHB APB学习总结
- 2021-06-14
- (65)如何根据句柄从二级、三级结构句柄表中找到内核对象
- (61)分析 KiFindReadyThread 函数 —— 线程优先级