最近公司項目準備開始重構,框架選定爲SpringBoot+Mybatis,本篇主要記錄了在IDEA中搭建SpringBoot多模塊項目的過程。java
① IDEA 工具欄選擇菜單 File -> New -> Project...mysql
② 選擇Spring Initializr,Initializr默認選擇Default,點擊Nextgit
③ 填寫輸入框,點擊Nextweb
④ 這步不須要選擇直接點Nextredis
⑤ 點擊Finish建立項目spring
⑥ 最終獲得的項目目錄結構以下sql
⑦ 刪除無用的.mvn目錄、src目錄、mvnw及mvnw.cmd文件,最終只留.gitignore和pom.xmlmybatis
① 選擇項目根目錄beta右鍵呼出菜單,選擇New -> Moduleapp
② 選擇Maven,點擊Next框架
③ 填寫ArifactId,點擊Next
④ 修改Module name增長橫槓提高可讀性,點擊Finish
⑤ 同理添加【beta-dao】、【beta-web】子模塊,最終獲得項目目錄結構以下圖
① 在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; /** * @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目錄並新建一個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; /** * @author linjian * @date 2018/9/29 */ @RestController @RequestMapping("demo") public class DemoController { @GetMapping("test") public String test() { return "Hello World!"; } }
③ 運行BetaWebApplication類中的main方法啓動項目,默認端口爲8080,訪問http://localhost:8080/demo/test獲得以下效果
以上雖然項目能正常啓動,可是模塊間的依賴關係卻還未添加,下面繼續完善
各個子模塊的依賴關係: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>
① 在beta-biz層建立com.yibao.beta.biz包,添加service目錄並在其中建立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 { @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; /** * @author linjian * @date 2018/9/29 */ @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; /** * @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); } }
設置完後從新運行main方法,項目正常啓動,訪問http://localhost:8080/demo/test獲得以下效果
① 父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; /** * @author linjian * @date 2018/9/29 */ @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; /** * @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); } }
設置完後從新運行main方法,項目正常啓動,訪問http://localhost:8080/demo/test獲得以下效果
至此,一個簡單的SpringBoot+Mybatis多模塊項目已經搭建完畢,咱們也經過啓動項目調用接口驗證其正確性。
一個井井有條的多模塊工程結構不只方便維護,並且有利於後續微服務化。在此結構的基礎上還能夠擴展common層(公共組件)、server層(如dubbo對外提供的服務)
此爲項目重構的第一步,後續還會的框架中集成logback、disconf、redis、dubbo等組件
在搭建過程當中還遇到一個maven私服的問題,緣由是公司內部的maven私服配置的中央倉庫爲阿里的遠程倉庫,它與maven自帶的遠程倉庫相比有些jar包版本並不全,致使在搭建過程當中好幾回由於沒拉到相應jar包致使項目啓動不了。