javascript
idea创建springboot项目+mybatis_Spring Boot + MyBatis 多模块项目搭建教程
Java后端,選擇“”
優(yōu)質(zhì)文章,及時送達(dá)
作者 | 楓本非凡
鏈接 | cnblogs.com/orzlin/p/9717399.html
上篇 | IDEA 遠(yuǎn)程一鍵部署 Spring Boot 到 Docker
一、前言
最近公司項目準(zhǔn)備開始重構(gòu),框架選定為SpringBoot+Mybatis,本篇主要記錄了在IDEA中搭建SpringBoot多模塊項目的過程。
1、開發(fā)工具及系統(tǒng)環(huán)境
IDE:
IntelliJ IDEA 2018.2
系統(tǒng)環(huán)境:
mac OSX
2、項目目錄結(jié)構(gòu)
biz層:
業(yè)務(wù)邏輯層
dao層:
數(shù)據(jù)持久層
web層:
請求處理層
二、搭建步驟
1、創(chuàng)建父工程
IDEA 工具欄選擇菜單 File -> New -> Project...
選擇Spring Initializr,Initializr默認(rèn)選擇Default,點擊Next
填寫輸入框,點擊Next
這步不需要選擇直接點Next
點擊Finish創(chuàng)建項目
最終得到的項目目錄結(jié)構(gòu)如下
刪除無用的.mvn目錄、src目錄、mvnw及mvnw.cmd文件,最終只留.gitignore和pom.xml
2、創(chuàng)建子模塊
選擇項目根目錄beta右鍵呼出菜單,選擇New -> Module
選擇Maven,點擊Next
填寫ArifactId,點擊Next
修改Module name增加橫杠提升可讀性,點擊Finish
同理添加beta-dao、beta-web子模塊,最終得到項目目錄結(jié)構(gòu)如下圖
3、運行項目
在beta-web層創(chuàng)建com.yibao.beta.web包(注意:這是多層目錄結(jié)構(gòu)并非單個目錄名,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方法啟動項目,默認(rèn)端口為8080,訪問http://localhost:8080/demo/test得到如下效果
以上雖然項目能正常啟動,但是模塊間的依賴關(guān)系卻還未添加,下面繼續(xù)完善。微信搜索 web_resource 獲取更多推送
4、配置模塊間的依賴關(guān)系
各個子模塊的依賴關(guān)系:biz層依賴dao層,web層依賴biz層
父pom文件中聲明所有子模塊依賴(dependencyManagement及dependencies的區(qū)別自行查閱文檔)
com.yibao.beta beta-biz ${beta.version} com.yibao.beta beta-dao ${beta.version} com.yibao.beta beta-web ${beta.version}
其中${beta.version}定義在properties標(biāo)簽中
在beta-web層中的pom文件中添加beta-biz依賴
com.yibao.beta beta-biz
在beta-biz層中的pom文件中添加beta-dao依賴
com.yibao.beta beta-dao
4. web層調(diào)用biz層接口測試
在beta-biz層創(chuàng)建com.yibao.beta.biz包,添加service目錄并在其中創(chuàng)建DemoService接口類,微信搜索 web_resource 獲取更多推送
public interface DemoService { String test;}
@Servicepublic class DemoServiceImpl implements DemoService {
@Override public String test { return "test"; }}
DemoController通過@Autowired注解注入DemoService,修改DemoController的test方法使之調(diào)用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方法啟動項目,發(fā)現(xiàn)如下報錯
***************************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入口類中增加包掃描,設(shè)置@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); }}
設(shè)置完后重新運行main方法,項目正常啟動,訪問http://localhost:8080/demo/test得到如下效果
6. 集成Mybatis
父pom文件中聲明mybatis-spring-boot-starter及l(fā)ombok依賴
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層創(chuàng)建com.yibao.beta.dao包,通過mybatis-genertaor工具生成dao層相關(guān)文件(DO、Mapper、xml),存放目錄如下
applicatio.properties文件添加jdbc及mybatis相應(yīng)配置項
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方法使之調(diào)用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方法啟動項目,發(fā)現(xiàn)如下報錯
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注解并設(shè)置其值為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); }}
設(shè)置完后重新運行main方法,項目正常啟動,訪問http://localhost:8080/demo/test得到如下效果
至此,一個簡單的SpringBoot+Mybatis多模塊項目已經(jīng)搭建完畢,我們也通過啟動項目調(diào)用接口驗證其正確性。
四、總結(jié)
一個層次分明的多模塊工程結(jié)構(gòu)不僅方便維護(hù),而且有利于后續(xù)微服務(wù)化。在此結(jié)構(gòu)的基礎(chǔ)上還可以擴(kuò)展common層(公共組件)、server層(如dubbo對外提供的服務(wù))微信搜索 web_resource 獲取更多推送
此為項目重構(gòu)的第一步,后續(xù)還會的框架中集成logback、disconf、redis、dubbo等組件
五、未提到的坑
在搭建過程中還遇到一個maven私服的問題,原因是公司內(nèi)部的maven私服配置的中央倉庫為阿里的遠(yuǎn)程倉庫,它與maven自帶的遠(yuǎn)程倉庫相比有些jar包版本并不全,導(dǎo)致在搭建過程中好幾次因為沒拉到相應(yīng)jar包導(dǎo)致項目啟動不了。
-END-
如果看到這里,說明你喜歡這篇文章,請轉(zhuǎn)發(fā)、點贊。微信搜索「web_resource」,關(guān)注后回復(fù)「進(jìn)群」或者掃描下方二維碼即可進(jìn)入無廣告交流群。
總結(jié)
以上是生活随笔為你收集整理的idea创建springboot项目+mybatis_Spring Boot + MyBatis 多模块项目搭建教程的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python交互模式什么意思_Pytho
- 下一篇: vscode 调试_如何使用VSCode