<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Spring Boot + MyBatis 多模塊項(xiàng)目搭建教程

          共 7466字,需瀏覽 15分鐘

           ·

          2021-11-16 21:51

          作者 | 楓本非凡

          鏈接 | cnblogs.com/orzlin/p/9717399.html

          一、前言

          最近公司項(xiàng)目準(zhǔn)備開(kāi)始重構(gòu),框架選定為SpringBoot+Mybatis,本篇主要記錄了在IDEA中搭建SpringBoot多模塊項(xiàng)目的過(guò)程。


          1、開(kāi)發(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)如下

          刪除無(wú)用的.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
          @SpringBootApplicationpublic class BetaWebApplication {
          public static void main(String[] args) { SpringApplication.run(BetaWebApplication.class, args); }}
          在com.yibao.beta.web包中添加controller目錄并新建一個(gè)controller,添加test方法測(cè)試接口是否可以正常訪問(wèn)
          @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,訪問(wèn)http://localhost:8080/demo/test得到如下效果

          以上雖然項(xiàng)目能正常啟動(dòng),但是模塊間的依賴關(guān)系卻還未添加,下面繼續(xù)完善。微信搜索 web_resource 獲取更多推送

          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>
          4. web層調(diào)用biz層接口測(cè)試
          在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通過(guò)@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(); }}
          再次運(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;
          @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),訪問(wèn)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包,通過(guò)mybatis-genertaor工具生成dao層相關(guān)文件(DO、Mapper、xml),存放目錄如下

          applicatio.properties文件添加jdbc及mybatis相應(yīng)配置項(xià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通過(guò)@Autowired注解注入U(xiǎn)serMapper,修改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(); }}
          再次運(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;
          @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),訪問(wèn)http://localhost:8080/demo/test得到如下效果

          至此,一個(gè)簡(jiǎn)單的SpringBoot+Mybatis多模塊項(xiàng)目已經(jīng)搭建完畢,我們也通過(guò)啟動(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ù))微信搜索 web_resource 獲取更多推送
          此為項(xiàng)目重構(gòu)的第一步,后續(xù)還會(huì)的框架中集成logback、disconf、redis、dubbo等組件

          五、未提到的坑

          在搭建過(guò)程中還遇到一個(gè)maven私服的問(wèn)題,原因是公司內(nèi)部的maven私服配置的中央倉(cāng)庫(kù)為阿里的遠(yuǎn)程倉(cāng)庫(kù),它與maven自帶的遠(yuǎn)程倉(cāng)庫(kù)相比有些jar包版本并不全,導(dǎo)致在搭建過(guò)程中好幾次因?yàn)闆](méi)拉到相應(yīng)jar包導(dǎo)致項(xiàng)目啟動(dòng)不了。

          怎么接私活?這個(gè)渠道你100%有用!請(qǐng)收藏!


          ,點(diǎn)個(gè)在看 
          瀏覽 43
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  日韩无码免费看 | 国产18禁黄网站禁片免费视频 | 国产精品免费人成人网站酒店 | 国产操屄激情免费视频 | 在线啪 |