javascript
SpringBoot+Mybatis多模块(module)项目搭建教程
作者:楓本非凡
cnblogs.com/orzlin/p/9717399.html
編輯:Java知音
一、前言
最近公司項(xiàng)目準(zhǔn)備開始重構(gòu),框架選定為SpringBoot+Mybatis,本篇主要記錄了在IDEA中搭建SpringBoot多模塊項(xiàng)目的過程。
1、開發(fā)工具及系統(tǒng)環(huán)境
IDE:IntelliJ IDEA 2018.2
系統(tǒng)環(huán)境:mac OSX
2、項(xiàng)目目錄結(jié)構(gòu)
biz層:業(yè)務(wù)邏輯層
dao層:數(shù)據(jù)持久層
web層:請(qǐng)求處理層
二、搭建步驟
1、創(chuàng)建父工程
① IDEA 工具欄選擇菜單 File -> New -> Project…
② 選擇Spring Initializr,Initializr默認(rèn)選擇Default,點(diǎn)擊Next
③ 填寫輸入框,點(diǎn)擊Next
④ 這步不需要選擇直接點(diǎn)Next
⑤ 點(diǎn)擊Finish創(chuàng)建項(xiàng)目
⑥ 最終得到的項(xiàng)目目錄結(jié)構(gòu)如下
⑦ 刪除無用的.mvn目錄、src目錄、mvnw及mvnw.cmd文件,最終只留.gitignore和pom.xml
2、創(chuàng)建子模塊
① 選擇項(xiàng)目根目錄beta右鍵呼出菜單,選擇New -> Module
② 選擇Maven,點(diǎn)擊Next
③ 填寫ArifactId,點(diǎn)擊Next
④ 修改Module name增加橫杠提升可讀性,點(diǎn)擊Finish
⑤ 同理添加【beta-dao】、【beta-web】子模塊,最終得到項(xiàng)目目錄結(jié)構(gòu)如下圖
3、運(yùn)行項(xiàng)目
① 在beta-web層創(chuàng)建com.yibao.beta.web包(注意:這是多層目錄結(jié)構(gòu)并非單個(gè)目錄名,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目錄并新建一個(gè)controller,添加test方法測(cè)試接口是否可以正常訪問
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!";} }③ 運(yùn)行BetaWebApplication類中的main方法啟動(dòng)項(xiàng)目,默認(rèn)端口為8080,訪問http://localhost:8080/demo/test得到如下效果
以上雖然項(xiàng)目能正常啟動(dòng),但是模塊間的依賴關(guān)系卻還未添加,下面繼續(xù)完善
4、配置模塊間的依賴關(guān)系
各個(gè)子模塊的依賴關(guān)系:biz層依賴dao層,web層依賴biz層
① 父pom文件中聲明所有子模塊依賴(dependencyManagement及dependencies的區(qū)別自行查閱文檔)
<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標(biāo)簽中
② 在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層調(diào)用biz層接口測(cè)試
① 在beta-biz層創(chuàng)建com.yibao.beta.biz包,添加service目錄并在其中創(chuàng)建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方法使之調(diào)用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();} }③ 再次運(yùn)行BetaWebApplication類中的main方法啟動(dòng)項(xiàng)目,發(fā)現(xiàn)如下報(bào)錯(cuò)
*************************** 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類,此時(shí)需要在BetaWebApplication入口類中增加包掃描,設(shè)置@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);} }設(shè)置完后重新運(yùn)行main方法,項(xiàng)目正常啟動(dòng),訪問http://localhost:8080/demo/test得到如下效果
6、集成Mybatis
① 父pom文件中聲明mybatis-spring-boot-starter及l(fā)ombok依賴
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層創(chuàng)建com.yibao.beta.dao包,通過mybatis-genertaor工具生成dao層相關(guān)文件(DO、Mapper、xml),存放目錄如下
④ applicatio.properties文件添加jdbc及mybatis相應(yīng)配置項(xiàng)
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注解注入U(xiǎn)serMapper,修改DemoService的test方法使之調(diào)用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();} }⑥ 再次運(yùn)行BetaWebApplication類中的main方法啟動(dòng)項(xiàng)目,發(fā)現(xiàn)如下報(bào)錯(cuò)
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類,此時(shí)需要在BetaWebApplication入口類中增加dao層包掃描,添加@MapperScan注解并設(shè)置其值為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);} }設(shè)置完后重新運(yùn)行main方法,項(xiàng)目正常啟動(dòng),訪問http://localhost:8080/demo/test得到如下效果
至此,一個(gè)簡單的SpringBoot+Mybatis多模塊項(xiàng)目已經(jīng)搭建完畢,我們也通過啟動(dòng)項(xiàng)目調(diào)用接口驗(yàn)證其正確性。
四、總結(jié)
一個(gè)層次分明的多模塊工程結(jié)構(gòu)不僅方便維護(hù),而且有利于后續(xù)微服務(wù)化。在此結(jié)構(gòu)的基礎(chǔ)上還可以擴(kuò)展common層(公共組件)、server層(如dubbo對(duì)外提供的服務(wù))
此為項(xiàng)目重構(gòu)的第一步,后續(xù)還會(huì)的框架中集成logback、disconf、redis、dubbo等組件
五、未提到的坑
在搭建過程中還遇到一個(gè)maven私服的問題,原因是公司內(nèi)部的maven私服配置的中央倉庫為阿里的遠(yuǎn)程倉庫,它與maven自帶的遠(yuǎn)程倉庫相比有些jar包版本并不全,導(dǎo)致在搭建過程中好幾次因?yàn)闆]拉到相應(yīng)jar包導(dǎo)致項(xiàng)目啟動(dòng)不了。
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的SpringBoot+Mybatis多模块(module)项目搭建教程的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java 集合框架看这一篇就够了
- 下一篇: 推荐一些冷门但是超级实用的工具