一、前言
1、開發工具及系統環境
-
- IDE:IntelliJ IDEA 2020.2.2
- 系統環境:Windows
2、專案目錄結構
-
-
biz層:業務邏輯層
-
dao層:資料持久層
-
web層:請求處理層
-
二、搭建步驟
1、建立父工程
選擇Spring Initializr,Initializr預設選擇Default,點選Next
點選Next
這一步不需要選擇直接Next
點選Finish建立專案
最終得到的專案目錄結構如下,刪除無用的.mvn目錄、src目錄、mvnw及mvnw.cmd檔案,最終只留.gitignore和pom.xml
2、建立子模組
選擇專案根目錄shop右鍵撥出選單,選擇New -> Module
選擇Maven,點選Next
填寫ArifactId,Module name增加橫槓提升可讀性,點選Finish
同理新增shop-dao、shop-web子模組,最終得到專案目錄結構如下圖
3、執行專案
在shop-web層建立com.dasheng.shop.web包(注意:這是多層目錄結構並非單個目錄名,com >> dasheng >> shop>> web)並新增入口類ShopWebApplication.java
@SpringBootApplication public class ShopWebApplition { public static void main(String[] args) { SpringApplication.run(ShopWebApplition.class, args); } }
在com.dasheng.shop.web包中新建controller檔案目錄並新建一個controller,新增test方法測試介面是否可以正常訪問
@RestController @RequestMapping("demo") public class Controller { @GetMapping("test") public String test() { return "Hello World!"; } }
執行ShopWebApplication類中的main方法啟動專案,預設埠為8080
訪問http://localhost:8080/demo/test得到如下效果
以上雖然專案能正常啟動,但是模組間的依賴關係卻還未新增,下面繼續完善。
4、配置模組間的依賴關係
各個子模組的依賴關係:
biz層依賴dao層,
web層依賴biz層
父pom檔案中宣告所有子模組依賴(dependencyManagement及dependencies的區別自行查閱文件)
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.dasheng.shop</groupId>
<artifactId>shop-biz</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.dasheng.shop</groupId>
<artifactId>shop-dao</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.dasheng.shop</groupId>
<artifactId>shop-web</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</dependencyManagement>
在shop-web層中的pom檔案中新增shop-biz依賴
<dependencies> <dependency> <groupId>com.dasheng.shop</groupId> <artifactId>shop-biz</artifactId> </dependency> </dependencies>
在shop-biz層中的pom檔案中新增shop-dao依賴
<dependencies> <dependency> <groupId>com.dasheng.shop</groupId> <artifactId>shop-dao</artifactId> </dependency> </dependencies>
5. web層呼叫biz層介面測試
在shop-biz層建立com.dasheng.shop.biz包,新增service目錄並在其中建立DemoService介面類
public interface DemoService { String test(); }
@Service("DemoService") public class DemoServiceImpl implements DemoService { @Override public String test() { return null; } }
com.dasheng.biz.web.controllerController通過@Autowired註解注入DemoService,修改DemoController的test方法使之呼叫DemoService的test方法,最終如下所示:
@Autowired private DemoService demoService; @GetMapping("test2") public String test2() { return demoService.test(); }
再次執行ShopWebApplication類中的main方法啟動專案,發現如下報錯
*************************** APPLICATION FAILED TO START *************************** Description: Field demoService in com.dasheng.biz.web.controller.Controller required a bean of type 'com.dasheng.biz.service.DemoService' that could not be found. The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true) Action: Consider defining a bean of type 'com.dasheng.biz.service.DemoService' in your configuration. Process finished with exit code 1
原因是找不到DemoService類,此時需要在ShopWebApplication入口類中增加包掃描,設定@SpringBootApplication註解中的scanBasePackages值為com.dasheng.shop,最終如下所示
shop-web的pom加入
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> <version>5.1.27</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.2</version> </dependency>
applicatio.yml配置 新建applicatio.yml配置檔案:https://blog.csdn.net/weixin_44848573/article/details/106445457
spring: datasource: driver-class-name: com.mysql.jdbc.Driver username: root password: root url: jdbc:mysql://localhost:3306/test01?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true
@SpringBootApplication(scanBasePackages = "com.dasheng.biz") @MapperScan("com.dasheng.biz.dao") public class ShopWebApplition { public static void main(String[] args) { SpringApplication.run(ShopWebApplition.class, args); } }
完成!!
業務層直接呼叫(@Autowired)dao層(Mybatis)就完事了這不在敘述了,因為沒有到入實體,專案放到gitee了,有需要的同學可以下載哦!
https://gitee.com/diaoyulin/shop.git