网站首页 > 技术文章 正文
Java后端,选择“”
优质文章,及时送达
作者 | 枫本非凡
链接 | 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...
选择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
@SpringBootApplication
public 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得到如下效果
以上虽然项目能正常启动,但是模块间的依赖关系却还未添加,下面继续完善。微信搜索 web_resource 获取更多推送
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>
4. web层调用biz层接口测试
在beta-biz层创建com.yibao.beta.biz包,添加service目录并在其中创建DemoService接口类,微信搜索 web_resource 获取更多推送
public interface DemoService {
String test;
}
@Service
public 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得到如下效果
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 = 123456
mybatis.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;
@Service
public 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得到如下效果
至此,一个简单的SpringBoot+Mybatis多模块项目已经搭建完毕,我们也通过启动项目调用接口验证其正确性。
四、总结
一个层次分明的多模块工程结构不仅方便维护,而且有利于后续微服务化。在此结构的基础上还可以扩展common层(公共组件)、server层(如dubbo对外提供的服务)微信搜索 web_resource 获取更多推送
此为项目重构的第一步,后续还会的框架中集成logback、disconf、redis、dubbo等组件
五、未提到的坑
在搭建过程中还遇到一个maven私服的问题,原因是公司内部的maven私服配置的中央仓库为阿里的远程仓库,它与maven自带的远程仓库相比有些jar包版本并不全,导致在搭建过程中好几次因为没拉到相应jar包导致项目启动不了。
-END-
如果看到这里,说明你喜欢这篇文章,请转发、点赞。微信搜索「web_resource」,关注后回复「进群」或者扫描下方二维码即可进入无广告交流群。
猜你喜欢
- 2024-10-09 用IDEA把SpringBoot项目打成jar发布项目
- 2024-10-09 SpringBoot 01 环境搭建入门(简单springboot项目搭建)
- 2024-10-09 IDEA中创建Springboot父子工程(idea创建父子项目)
- 2024-10-09 16:实现SpringBoot单个、多个文件的上传
- 2024-10-09 手把手教你用Spring-Boot搭建项目
- 2024-10-09 Spring Boot入门-快速搭建web项目
- 2024-10-09 使用IDEA创建gradle的spring boot项目并提交到DS916+的git server上
- 2024-10-09 Springboot工程建立(springboot工程搭建步骤)
- 2024-10-09 SpringBoot进阶3:项目搭建方法与项目结构分析
- 2024-10-09 SpringBoot+Mybatis多模块(module)项目搭建教程
- 1507℃桌面软件开发新体验!用 Blazor Hybrid 打造简洁高效的视频处理工具
- 506℃Dify工具使用全场景:dify-sandbox沙盒的原理(源码篇·第2期)
- 486℃MySQL service启动脚本浅析(r12笔记第59天)
- 466℃服务器异常重启,导致mysql启动失败,问题解决过程记录
- 464℃启用MySQL查询缓存(mysql8.0查询缓存)
- 444℃「赵强老师」MySQL的闪回(赵强iso是哪个大学毕业的)
- 423℃mysql服务怎么启动和关闭?(mysql服务怎么启动和关闭)
- 419℃MySQL server PID file could not be found!失败
- 最近发表
-
- netty系列之:搭建HTTP上传文件服务器
- 让deepseek教我将deepseek接入word
- 前端大文件分片上传断点续传(前端大文件分片上传断点续传怎么操作)
- POST 为什么会发送两次请求?(post+为什么会发送两次请求?怎么回答)
- Jmeter之HTTP请求与响应(jmeter运行http请求没反应)
- WAF-Bypass之SQL注入绕过思路总结
- 用户疯狂点击上传按钮,如何确保只有一个上传任务在执行?
- 二 计算机网络 前端学习 物理层 链路层 网络层 传输层 应用层 HTTP
- HTTP请求的完全过程(http请求的基本过程)
- dart系列之:浏览器中的舞者,用dart发送HTTP请求
- 标签列表
-
- c++中::是什么意思 (83)
- 标签用于 (65)
- 主键只能有一个吗 (66)
- c#console.writeline不显示 (75)
- pythoncase语句 (81)
- es6includes (73)
- windowsscripthost (67)
- apt-getinstall-y (86)
- node_modules怎么生成 (76)
- chromepost (65)
- c++int转char (75)
- static函数和普通函数 (76)
- el-date-picker开始日期早于结束日期 (70)
- js判断是否是json字符串 (67)
- checkout-b (67)
- localstorage.removeitem (74)
- vector线程安全吗 (70)
- & (66)
- java (73)
- js数组插入 (83)
- linux删除一个文件夹 (65)
- mac安装java (72)
- eacces (67)
- 查看mysql是否启动 (70)
- 无效的列索引 (74)