一、前言
最近公司項目準備開始重構,框架選定爲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;
@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;
@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;
public interface DemoService {
String test();
}
package com.yibao.beta.biz.service.impl;
import com.yibao.beta.biz.service.DemoService;
import org.springframework.stereotype.Service;
@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;
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;
@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;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@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;
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;
@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;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@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包導致項目啓動不了。