idea创建springboot项目+mybatis_Spring Boot + MyBatis 多模块项目搭建教程
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的区别自行查阅文档)
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;
}
@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>
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),存放目录如下
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」,关注后回复「进群」或者扫描下方二维码即可进入无广告交流群。
idea创建springboot项目+mybatis_Spring Boot + MyBatis 多模块项目搭建教程相关推荐
- 2020idea创建web项目_Spring Boot + Mybatis 多模块(module)项目的完整搭建教程
点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试资料 来源:http://sina.lt/gmQc 一.前言 最近公司项 ...
- Spring Boot + Mybatis 多模块(module)项目的完整搭建教程
点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试资料 来源:http://sina.lt/gmQc 一.前言 最近公司项 ...
- java sqlite mybatis_Spring boot + Mybatis + SQLite 搭建blog API
Spring boot + Mybatis + SQLite 搭建blog API 一.准备环境 二.创建一个SpringBoot项目 在此我就不再演示如何创建SpringBoot项目了,需要的请看[ ...
- 【SpringBoot】spring boot + mybatis + druid
0.美图 1.为什么用Druid 因为在用到spring boot + mybatis的项目时候,经常发生访问接口卡,服务器项目用了几天就很卡的甚至不能访问的情况,而我们的项目和数据库都是好了,考虑到 ...
- 【超详细】SSM框架项目实战|Spring+Mybatis+Springmvc框架项目实战整合-【CRM客户管理系统】——课程笔记
相关资料网盘链接: CRM客户管理系统资料 提取码 :0u04 P1--CRM阶段简介: web项目开发:如何分析,设计,编码,测试. 形成编程思想和编程习惯. P2--CRM的技术架构 ...
- Spring Boot和多模块项目–添加模块特定的属性文件
你好! 在本文中,我将向您展示几种如何在Spring Boot项目中添加模块特定的属性文件的方法. 它将介绍使属性文件可识别配置文件的手动方法以及可识别配置文件的半自动方法. 我的Github帐户上发 ...
- java怎么新建模块_spring boot添加新模块的方法教程
前言 在springboot项目框架里,把一个项目两大模块,主项目main和测试项目test,而我们的测试项目根据功能又可以再分,比如可以有单元测试,集成测试,业务测试等等. 对于一个初学者来说,建立 ...
- maven 按业务拆分模块_关于maven单项目拆分为Maven多模块项目
最近工作遇到Maven单项目拆分多模块项目问题,故简作记录 相关分支如下: Master(线上分支) Split(拆分分支) UnionDev(团队成员同步开发合并分支) Dev1(团队成员同步开发分 ...
- springboot 集成mybatis_Spring Boot 集成Mybatis实现多数据源
静态的方式 我们以两套配置方式为例,在项目中有两套配置文件,两套mapper,两套SqlSessionFactory,各自处理各自的业务,这个两套mapper都可以进行增删改查的操作,在这两个主MYS ...
最新文章
- MySQL主从服务器配置工作原理
- 不是所有的事情都要达成共识
- 正则表达式截取URL参数值
- 【Get 以太坊技能】CentOS 7 安装 go
- 4邻接、8邻接、m邻接
- 残疾人计算机高考试题,残疾人勇夺玉溪高考榜眼:想去清华学计算机
- Python之List和Tuple类型(入门3)
- Java并发编程:synchronized
- QGIS+GH + MapServer
- PHP接口(interface)
- DDoS攻击重大历史事件
- 简单html和css静态网页制作
- 一级造价工程师(安装)- 管理笔记
- 股票实时行情数据有哪些分类?
- 假币问题(二分法与三分法实现)
- Get the information of all heroes in the League of Legends through the crawler.
- 如何将 MPG 转换为 MP4
- 邮箱安全再成热点 金笛企业邮件系统保障企业用户通信安全
- Shiro解决多个二级域名的单点登录问题
- 【C语言游戏】太空大战 | SpaceWar(基于EasyX图形库,FPS优化,碰撞判断,drawAlpha绘制透明贴图,音乐播放,源码素材免费分享)
热门文章
- mysql创建函数失败_mysql创建函数出现问题?
- android版 模拟器哪个好,安卓模拟器电脑版哪个好
- MySQL数据库密码重置
- python判断一个对象是否可迭代_python - 在Python中,如何确定对象是否可迭代? - includeStdio...
- 华清实训的收获(人工智能的小广告和福利)
- Nginx出现403 forbidden
- 计组—中央处理器(CPU)
- ADAS(1) 概述整理及自动驾驶实例
- Git提交branch到remote
- TensorFlow 2.X中的动手NLP深度学习模型准备