1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        Spring Boot + MyBatis 多模塊項目搭建教程

        共 5931字,需瀏覽 12分鐘

         ·

        2021-11-04 17:50

        作者 | 楓本非凡

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

        一、前言

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


        1、開發(fā)工具及系統(tǒng)環(huán)境

        • IDE:IntelliJ IDEA 2018.2

        • 系統(tǒng)環(huán)境:mac OSX

        2、項目目錄結(jié)構(gòu)

        • biz層:業(yè)務(wù)邏輯層

        • dao層:數(shù)據(jù)持久層

        • web層:請求處理層

        二、搭建步驟

        1、創(chuàng)建父工程

        IDEA 工具欄選擇菜單 File -> New -> Project...

        選擇Spring Initializr,Initializr默認(rèn)選擇Default,點擊Next

        填寫輸入框,點擊Next

        這步不需要選擇直接點Next

        點擊Finish創(chuàng)建項目

        最終得到的項目目錄結(jié)構(gòu)如下

        刪除無用的.mvn目錄、src目錄、mvnw及mvnw.cmd文件,最終只留.gitignore和pom.xml

        2、創(chuàng)建子模塊

        選擇項目根目錄beta右鍵呼出菜單,選擇New -> Module

        選擇Maven,點擊Next

        填寫ArifactId,點擊Next

        修改Module name增加橫杠提升可讀性,點擊Finish

        同理添加beta-dao、beta-web子模塊,最終得到項目目錄結(jié)構(gòu)如下圖

        3、運行項目
        在beta-web層創(chuàng)建com.yibao.beta.web包(注意:這是多層目錄結(jié)構(gòu)并非單個目錄名,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目錄并新建一個controller,添加test方法測試接口是否可以正常訪問
        @RestController@RequestMapping("demo")public class DemoController {
        @GetMapping("test") public String test() { return "Hello World!"; }}
        運行BetaWebApplication類中的main方法啟動項目,默認(rèn)端口為8080,訪問http://localhost:8080/demo/test得到如下效果

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

        4、配置模塊間的依賴關(guān)系

        各個子模塊的依賴關(guān)系:biz層依賴dao層,web層依賴biz層
        父pom文件中聲明所有子模塊依賴(dependencyManagement及dependencies的區(qū)別自行查閱文檔)
        <dependencyManagement> <dependencies> <dependency> <groupId>com.yibao.betagroupId> <artifactId>beta-bizartifactId> <version>${beta.version}version> dependency> <dependency> <groupId>com.yibao.betagroupId> <artifactId>beta-daoartifactId> <version>${beta.version}version> dependency> <dependency> <groupId>com.yibao.betagroupId> <artifactId>beta-webartifactId> <version>${beta.version}version> dependency> dependencies>dependencyManagement>
        其中${beta.version}定義在properties標(biāo)簽中
        在beta-web層中的pom文件中添加beta-biz依賴
        <dependencies> <dependency> <groupId>com.yibao.betagroupId> <artifactId>beta-bizartifactId> dependency>dependencies>
        在beta-biz層中的pom文件中添加beta-dao依賴
        <dependencies> <dependency> <groupId>com.yibao.betagroupId> <artifactId>beta-daoartifactId> dependency>dependencies>
        4. web層調(diào)用biz層接口測試
        在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通過@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(); }}
        再次運行BetaWebApplication類中的main方法啟動項目,發(fā)現(xiàn)如下報錯
        ***************************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入口類中增加包掃描,設(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è)置完后重新運行main方法,項目正常啟動,訪問http://localhost:8080/demo/test得到如下效果

        6. 集成Mybatis

        父pom文件中聲明mybatis-spring-boot-starter及l(fā)ombok依賴
        dependencyManagement> <dependencies> <dependency> <groupId>org.mybatis.spring.bootgroupId> <artifactId>mybatis-spring-boot-starterartifactId> <version>1.3.2version> dependency> <dependency> <groupId>org.projectlombokgroupId> <artifactId>lombokartifactId> <version>1.16.22version> dependency> dependencies>dependencyManagement>
        在beta-dao層中的pom文件中添加上述依賴
        <dependencies> <dependency> <groupId>org.mybatis.spring.bootgroupId> <artifactId>mybatis-spring-boot-starterartifactId> dependency> <dependency> <groupId>mysqlgroupId> <artifactId>mysql-connector-javaartifactId> dependency> <dependency> <groupId>org.projectlombokgroupId> <artifactId>lombokartifactId> dependency>dependencies>
        在beta-dao層創(chuàng)建com.yibao.beta.dao包,通過mybatis-genertaor工具生成dao層相關(guān)文件(DO、Mapper、xml),存放目錄如下

        applicatio.properties文件添加jdbc及mybatis相應(yī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通過@Autowired注解注入UserMapper,修改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(); }}
        再次運行BetaWebApplication類中的main方法啟動項目,發(fā)現(xiàn)如下報錯
        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注解并設(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è)置完后重新運行main方法,項目正常啟動,訪問http://localhost:8080/demo/test得到如下效果

        至此,一個簡單的SpringBoot+Mybatis多模塊項目已經(jīng)搭建完畢,我們也通過啟動項目調(diào)用接口驗證其正確性。

        四、總結(jié)

        一個層次分明的多模塊工程結(jié)構(gòu)不僅方便維護,而且有利于后續(xù)微服務(wù)化。在此結(jié)構(gòu)的基礎(chǔ)上還可以擴展common層(公共組件)、server層(如dubbo對外提供的服務(wù))微信搜索 web_resource 獲取更多推送
        此為項目重構(gòu)的第一步,后續(xù)還會的框架中集成logback、disconf、redis、dubbo等組件

        五、未提到的坑

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

        熱門內(nèi)容:

        瀏覽 20
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        評論
        圖片
        表情
        推薦
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            日逼真爽| AV在线影院 | 雷电将军和丘丘人繁衍后代视频 | 国产丰满大乳奶水在线视频 | 亚洲永久免费网站 | 欧美精品三级网站 | www日本免费 | 边做奶水边喷h高h前夫 | 手机乱伦视频网 | 97就去干|