[前端學(xué)java09-SpringBoot實(shí)戰(zhàn)] 多模塊配置 + Mybatis-plus + 單多模塊打包部署

導(dǎo)航

[react] Hooks

[封裝01-設(shè)計(jì)模式] 設(shè)計(jì)原則 和 工廠模式(簡(jiǎn)單抽象方法) 適配器模式 裝飾器模式
[封裝02-設(shè)計(jì)模式] 命令模式 享元模式 組合模式 代理模式

[React 從零實(shí)踐01-后臺(tái)] 代碼分割
[React 從零實(shí)踐02-后臺(tái)] 權(quán)限控制
[React 從零實(shí)踐03-后臺(tái)] 自定義hooks
[React 從零實(shí)踐04-后臺(tái)] docker-compose 部署react+egg+nginx+mysql
[React 從零實(shí)踐05-后臺(tái)] Gitlab-CI使用Docker自動(dòng)化部署

[源碼-webpack01-前置知識(shí)] AST抽象語(yǔ)法樹(shù)
[源碼-webpack02-前置知識(shí)] Tapable
[源碼-webpack03] 手寫(xiě)webpack - compiler簡(jiǎn)單編譯流程
[源碼] Redux React-Redux01
[源碼] axios
[源碼] vuex
[源碼-vue01] data響應(yīng)式 和 初始化渲染
[源碼-vue02] computed 響應(yīng)式 - 初始化,訪問(wèn),更新過(guò)程
[源碼-vue03] watch 偵聽(tīng)屬性 - 初始化和更新
[源碼-vue04] Vue.set 和 vm.$set
[源碼-vue05] Vue.extend

[源碼-vue06] Vue.nextTick 和 vm.$nextTick
[部署01] Nginx
[部署02] Docker 部署vue項(xiàng)目
[部署03] gitlab-CI

[數(shù)據(jù)結(jié)構(gòu)和算法01] 二分查找和排序

[深入01] 執(zhí)行上下文
[深入02] 原型鏈
[深入03] 繼承
[深入04] 事件循環(huán)
[深入05] 柯里化 偏函數(shù) 函數(shù)記憶
[深入06] 隱式轉(zhuǎn)換 和 運(yùn)算符
[深入07] 瀏覽器緩存機(jī)制(http緩存機(jī)制)
[深入08] 前端安全
[深入09] 深淺拷貝
[深入10] Debounce Throttle
[深入11] 前端路由
[深入12] 前端模塊化
[深入13] 觀察者模式 發(fā)布訂閱模式 雙向數(shù)據(jù)綁定
[深入14] canvas
[深入15] webSocket
[深入16] webpack
[深入17] http 和 https
[深入18] CSS-interview
[深入19] 手寫(xiě)Promise
[深入20] 手寫(xiě)函數(shù)
[深入21] 數(shù)據(jù)結(jié)構(gòu)和算法 - 二分查找和排序
[深入22] js和v8垃圾回收機(jī)制
[深入23] JS設(shè)計(jì)模式 - 代理,策略,單例

[前端學(xué)java01-SpringBoot實(shí)戰(zhàn)] 環(huán)境配置和HelloWorld服務(wù)
[前端學(xué)java02-SpringBoot實(shí)戰(zhàn)] mybatis + mysql 實(shí)現(xiàn)歌曲增刪改查
[前端學(xué)java03-SpringBoot實(shí)戰(zhàn)] lombok,日志,部署
[前端學(xué)java04-SpringBoot實(shí)戰(zhàn)] 靜態(tài)資源 + 攔截器 + 前后端文件上傳
[前端學(xué)java05-SpringBoot實(shí)戰(zhàn)] 常用注解 + redis實(shí)現(xiàn)統(tǒng)計(jì)功能
[前端學(xué)java06-SpringBoot實(shí)戰(zhàn)] 注入 + Swagger2 3.0 + 單元測(cè)試JUnit5
[前端學(xué)java07-SpringBoot實(shí)戰(zhàn)] IOC掃描器 + 事務(wù) + Jackson
[前端學(xué)java08-SpringBoot實(shí)戰(zhàn)總結(jié)1-7] 階段性總結(jié)
[前端學(xué)java09-SpringBoot實(shí)戰(zhàn)] 多模塊配置 + Mybatis-plus + 單多模塊打包部署

(一) 前置知識(shí)

(1) 一些單詞

batch 批 分批

Stalled 停滯
pratices 實(shí)踐,通常的做法 // best practices 最佳實(shí)踐
key map 鍵盤(pán)映射

artifact 人工制品
look up 查找 查閱 // lookup parent from repository
property 屬性
annotation 注解 // java中的注解
surefire 萬(wàn)全 可靠的
verify 驗(yàn)證 校驗(yàn)

(2) mac版 idea快捷鍵記錄

1. 全局查找
command + shift + r
2. 調(diào)出actions
command + 3
3. 設(shè)置
command + ,
4. 文件間的跳轉(zhuǎn)
command + e
5. 信息提示
command + 1
6. 跳轉(zhuǎn)到別的類(lèi)
control + 鼠標(biāo)左鍵
7. 復(fù)制文件的引用路徑
鼠標(biāo)右鍵 - copy path - copy reference
8. 左右移動(dòng)代碼塊
- 左移動(dòng) shift + tab
- 右移動(dòng) tab
9. 查找文件
- ctrl + h


1. command + k 查找下一個(gè)
2. ctrl + h 查找文件
3. 設(shè)置快捷鍵 ( command + , ) => ( keymap ) => ( 搜索find ) => ( 添加查找快捷鍵 ) => ( command + f )

(3) pom.xml配置項(xiàng)

  • 項(xiàng)目的打包類(lèi)型
    • packaging
      • pom jar war
      • 默認(rèn)打包類(lèi)型packaging=jar,即默認(rèn)是打成jar包
  • pom模型版本
    • modelVersion
  • 項(xiàng)目信息
    • 項(xiàng)目唯一標(biāo)識(shí) groupId ,包括幾個(gè)字段,比如com.wu7,com是域,wu7是域名
    • 項(xiàng)目名或者模塊名 artifactId
    • 項(xiàng)目版本 version
    • 項(xiàng)目名稱(chēng) name
    • 打包方式(項(xiàng)目的打包類(lèi)型) packaging pom jar war
    • 一般來(lái)說(shuō),( 包名根目錄 ) = ( groupId ) + ( artifactId ),不允許重復(fù)
    • packageName = groupId + artifactId
  • 父級(jí)項(xiàng)目 parent
    • 也包括 groupId, artifactIdversion,relativePath
    • relativePath
      • relativePath用來(lái)指定查找該父項(xiàng)目 pom.xml 的相對(duì)路徑
      • 默認(rèn):../pom.xml
      • 優(yōu)先級(jí):relativePath > 本地倉(cāng)庫(kù) > 遠(yuǎn)程倉(cāng)庫(kù)
      • 1.設(shè)定一個(gè)空值,將始終從倉(cāng)庫(kù)中獲取,而不從本地路徑獲取,如 <relativePath/>
      • 2.沒(méi)有relativePath標(biāo)簽表示從 ../pom.xml 來(lái)查找
  • 屬性信息 properties
    • java的版本:java.version
    • 編譯字符編碼 project.build.sourceEncoding
    • 輸出字符編碼 project.reporting.outputEncoding
    • 打包時(shí)跳過(guò)test <skipTests>true</skipTests>
  • ================================= 依賴(lài)關(guān)系 dependencies
  • 編譯 build
    • 插件集 plugins
    • 插件 plugin
  • ================================= 模塊 modules
    • 在父模塊中用來(lái)表示包含的 ( 子模塊 )

(4) jackson

image.png
  • 之前掘金寫(xiě)的文章

  • jackson fastjson gson

  • 安裝

    • 一般在web開(kāi)發(fā)中使用了 spring-boot-starter-web 啟動(dòng)器的話(huà),已經(jīng)包含了 jackson
    • 如果沒(méi)有使用上邊的starter,則需要自己安裝 spring-boot-starter-json 依賴(lài)
      <dependency>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-json</artifactId>
      </dependency>
      
  • jackson常用注解

    • @JsonFormat
    • @JsonPropertyOrder
    • @JsonProperty
    • @JsonInclude
    • @JsonIgnore
    • @JsonFormat(pattern = "YYYY-MM-dd HH:mm:ss", timezone = "GMT+8") - 格式化時(shí)間,用于類(lèi)的屬性
    • @JsonPropertyOrder(value = {"singer", "album"}) - 調(diào)整屬性的順序,用于類(lèi)
    • @JsonProperty("aaa") - 修改返回?cái)?shù)據(jù)的屬性名稱(chēng),修改為aaa
    • @JsonInclude(JsonInclude.Include.NON_NULL) - 包含則不返回該屬性,包含null則不返回該屬性
    • @JsonIgnore - 返回的屬性不包括該字段
  • @JsonFormat - 格式化時(shí)間

    • ( 全局配置 ) 和 ( bean中單獨(dú)配置@JsonFormat() )
    • 日期格式化配置優(yōu)先級(jí) ( bean對(duì)象中屬性 @JsonFormat 注解 > 全局配置)
    • @JsonFormat(pattern = "YYYY-MM-dd HH:mm:ss", timezone = "GMT+8")
1. 全局配置
- 在application.yml中全局配置jackson

spring:
  jackson:
    date-format: YYYY-MM-dd HH:mm:ss # 全局時(shí)間格式化
    time-zone: GMT+8 # 時(shí)區(qū)
2. 局部配置
- 實(shí)體中使用 @JsonFormat()

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@TableName("music")
public class MpMusicBean {
    Integer id;
    String name;
    String album;
    String singer;

    @TableField(value = "startTime") // value是數(shù)據(jù)庫(kù)字段名
    @JsonFormat(pattern = "YYYY-MM-dd") // jackson格式化時(shí)間,優(yōu)先級(jí) ( bean中設(shè)置 > 全局配置 )
    Timestamp startTime;

    @TableField(value = "endTime") // value是數(shù)據(jù)庫(kù)字段名
    @JsonFormat(pattern = "YYYY-MM-dd HH:mm:ss") // jackson格式化時(shí)間,優(yōu)先級(jí) ( bean中設(shè)置 > 全局配置 )
    Timestamp endTime;
}
  • @JsonPropertyOrder - 調(diào)整屬性順序
@JsonPropertyOrder(value = {"singer", "album"}) // jackson調(diào)整屬性順序,singer在album前面
public class MpMusicBean {
    Integer id;
    String name;
    String album;
    String singer;
}

結(jié)果:
未使用前
    {
            "album": "七里香112233",
            "singer": "周杰倫112233",

    },
使用后
    {
            "singer": "周杰倫112233", //----------------- 1
            "album": "七里香112233",  // ---------------- 2
    },

  • @JsonProperty
    @JsonProperty("start") // jackson修改返回?cái)?shù)據(jù)的屬性名
    @TableField(value = "startTime") // value是數(shù)據(jù)庫(kù)字段名
    @JsonFormat(pattern = "YYYY-MM-dd") // jackson格式化時(shí)間,優(yōu)先級(jí) ( bean中設(shè)置 > 全局配置 )
    // 這三個(gè)注解的最終效果:將數(shù)據(jù)庫(kù)中的startTime列的數(shù)據(jù)key對(duì)應(yīng)實(shí)體的startTime,然后將返回值修改成star,并格式化時(shí)間
    Timestamp startTime;
  • @JsonInclude
@JsonInclude(JsonInclude.Include.NON_NULL) // jackson,包含null,則不返回該字段
String singer;
  • @JsonIgnore
@JsonIgnore // jackson返回值中不包含該屬性,忽略該屬性
String name;

(5) SimpleDateFormat,sring,Timestamp,datetime 幾個(gè)的關(guān)系

  • Timestamp
    • ( java中的Timestamp ) 相當(dāng)于 ( 數(shù)據(jù)庫(kù)中的datetime )
    • Timestamp.valueOf(String v) 將string轉(zhuǎn)成Timestamp
  • SimpleDateFormat
    • 主要用來(lái)格式化時(shí)間
    • new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") 生成實(shí)例,然后再調(diào)用實(shí)例上的方法
    @Test
    void testDate() {
        Date date = new Date(); // 獲取時(shí)間實(shí)例
        SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss"); // 生成實(shí)例
        String format = dateFormat.format(date); // 調(diào)用實(shí)例實(shí)例上的format()方法,返回一個(gè)字符串
        log.info("SimpleDateFormat: {}", format);
    }


(二) 多模塊配置 module

(2.1) 多模塊的優(yōu)點(diǎn)

  • 每個(gè)模塊具有 ( 高內(nèi)聚 ) 性,方便校驗(yàn),調(diào)試,測(cè)試

(2.2) 模塊的依賴(lài)關(guān)系

common 一般不依賴(lài)任何模塊,除非common封裝的方法涉及到其他模塊
mapper => 依賴(lài) common
service => 依賴(lài) common mapper
controller => 依賴(lài) common service mapper
注意點(diǎn):
1. 需要?jiǎng)h除 ( 父模塊的src ) 文件夾,因?yàn)楦改K不需要寫(xiě)代碼
2. controller 模塊是需要建 ( srping-boot ) 項(xiàng)目
3. 除了controller模塊,其他模塊都是建 ( maven ) 項(xiàng)目

(2.3) idea 做相關(guān)設(shè)置

  • 字符編碼設(shè)置:ctrl + , => File Encodings => utf-8
  • 注解生效激活:compiler => annotation processors => Enable annotation processing

(2.4) 多模塊配置具體過(guò)程

(1) 父模塊
1.1 首先新建父模塊 ----------------------------------- 【 創(chuàng)建的類(lèi)型為springboot項(xiàng)目 】
1.2 修改父模塊的 pom.xml,將 ( packaging ) 類(lèi)型從jar改為pom,即打包類(lèi)型為pom
1.3 dependences 交給父模塊來(lái)打理
1.4 注意區(qū)分 ( dependencies ) 和 ( dependencyManagenment )
     - dependencyManagenment => dependencies => dependency
     - dependencyManagenment 只是聲明依賴(lài),并不實(shí)際導(dǎo)入package,子模塊需要顯示的引入
1.5 創(chuàng)建好子模塊后,父項(xiàng)目的pom.xml中會(huì)自動(dòng)生成 ( modules ) 標(biāo)簽,并顯示所有子模塊


(2) 子模塊
2.1 創(chuàng)建module - mapper service controller ---------- 【 創(chuàng)建類(lèi)型事maven項(xiàng)目 】


(3) 模塊的依賴(lài)關(guān)系
      common 一般不依賴(lài)任何模塊,除非common封裝的方法涉及到其他模塊
      mapper => 依賴(lài) common
      service => 依賴(lài) common mapper
      controller => 依賴(lài) common service mapper
   
   
(4) 具體配置
4.1在子模塊中的pom.xml中配置好模塊的依賴(lài)關(guān)系即可
4.2 所有模塊的 groupId 都是一樣的
4.3 controller
<dependencies>
    <!-- controller 依賴(lài) service -->
    <dependency>
        <groupId>com.woow.wu7</groupId>
        <artifactId>service</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
    </dependency>
</dependencies>
image.png

image.png


(三) Mybatis-plus

(1) Mybatis-plus ( Mapper ) 的基本使用

(1.1) 安裝idea插件 - MyBatisX

  • command + , 選擇 plugins,搜索 MyBatisX 安裝
  • 好處就是點(diǎn)擊圖標(biāo)能直接跳轉(zhuǎn)mapper和xml配置文件

(1.2) 添加 maven 依賴(lài)

  • 注意:Mybatis-plus 包中已經(jīng)包含了 mybatis 和 jdbc 的start包了,所以不需要再安裝這兩個(gè)包
    • 包含 mybatis-spring-boot-starter
    • 包含 spring-boot-starter-data-jdbc
  • application.yml 文件中通過(guò) mybatis-plus: xxx 對(duì)其進(jìn)行定制配置
    • mapperLocations: 自動(dòng)配置好了
      • 默認(rèn)值是:classpath*:/mapper/**/*.xml,表示在 resources/mapper
      • 所以sql映射文件,建議放在 resrouces/mapper 文件中
    • SqlSessionFactory:自動(dòng)在容器中配置好了,底層是容器中的默認(rèn)數(shù)據(jù)源
    • SqlSessionTemplate:自動(dòng)在容器中配置好了
    • @Mapper 標(biāo)注也會(huì)被自動(dòng)掃描
      • 建議使用@MapperScan進(jìn)行指定掃描的包,進(jìn)行批量掃描,就不用一個(gè)個(gè)@Mapper了
<!-- mybatis plus包含了 ( mybatis-spring-boot-starter ) 和 ( spring-boot-starter-data-jdbc ) -->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.2</version>
</dependency>

(1.3) 配置 @MapperScan

  • 指定掃描的mapper包,就不用在一個(gè)個(gè)@Mapper了
  • @MapperScan注解的 ( 參數(shù) ) 是 ( mappe文件夾路徑引用 )
@SpringBootApplication
@MapperScan("com.example.demo.mapper") // 指定mapper的目錄,將自動(dòng)掃描,則不用在每個(gè)類(lèi)上都加@Mapper
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

(1.4) 準(zhǔn)備一個(gè)bean對(duì)象即user類(lèi) - MybatisPlusUserBean

  • 注意注意注意:( 實(shí)體類(lèi)名 ) 必須和 ( 數(shù)據(jù)庫(kù)表名 ) 保持一致,否則需要指定數(shù)據(jù)庫(kù)名稱(chēng)
  • 這里實(shí)體名叫 MybatisPlusUserBean,那么數(shù)據(jù)庫(kù)名就要叫 mybatis_plus_user_bean
  • @TableName 指定數(shù)據(jù)庫(kù)表名,默認(rèn)情況下mp會(huì)將實(shí)體名首字母小寫(xiě),用_連接,作為表名
@Data
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Component
@TableName("mybatis_plus_user_bean") 
// @TableName 
// 指定數(shù)據(jù)庫(kù)表名,默認(rèn)情況下mp會(huì)將實(shí)體名首字母小寫(xiě),用_連接,作為表名
// @TableName 是mybatis-plus中的注解
public class MybatisPlusUserBean {
    private Long id;
    private String name;
    private Integer age;
    private String email;
    
    @TableField(exist = false) // mybatis-plus的注解,表示該屬性在數(shù)據(jù)庫(kù)中不存在
    private String other;
}

(1.5) 編寫(xiě)一個(gè)mapper - 繼承BaseMapper

  • 繼承 extends BaseMapper<bean對(duì)象> 就具有crud的能力,不用在手寫(xiě)crud了
  • bean對(duì)象的命名需要和數(shù)據(jù)庫(kù)的表一致
  • 比如實(shí)體名叫 MybatisPlusUserBean,那么數(shù)據(jù)庫(kù)名就要叫 mybatis_plus_user_bean
  • 可以不用再寫(xiě) @Mapper,因?yàn)榕渲昧?@MapperScan
@Mapper
public interface MybatisPlusUserMapper extends BaseMapper<MybatisPlusUserBean> {
}

(1.6) 準(zhǔn)備好數(shù)據(jù)庫(kù)和user表

  • 1.注意注意注意:( 實(shí)體類(lèi)名 ) 必須和 ( 數(shù)據(jù)庫(kù)表名 ) 保持一致
  • 2.也可以通過(guò) @TableName 指定數(shù)據(jù)庫(kù)表名,默認(rèn)情況下mp會(huì)將實(shí)體名首字母小寫(xiě),用_連接,作為表名

(1.7) 測(cè)試是否已經(jīng)配置成功

@SpringBootTest
@Slf4j
public class MybatisPlusUserTest {

    @Autowired
    MybatisPlusUserMapper mybatisPlusUserMapper;

    // mapper相關(guān)測(cè)試
    // 查詢(xún)
    @Test
    void getUser() {
        MybatisPlusUserBean user = mybatisPlusUserMapper.selectById(1);
        log.info("用戶(hù)信息{}", user);
    }

    // mapper相關(guān)測(cè)試
    // 添加
    @Test
    void addUser() {
        MybatisPlusUserBean user = new MybatisPlusUserBean();
        long a = 11;
        MybatisPlusUserBean user2  = user.builder()
                .name("周杰倫2")
                .age(200)
                .email("woow.wu7@gmail.com")
                .id(a)
                .build();

        int status = mybatisPlusUserMapper.insert(user2);
        log.info("Mybatis-plus插入insert()方法的返回值{}", status);
    }

    // mapper相關(guān)測(cè)試
    // 刪除
    @Test
    void deleteUser() {
        int id = 11;
        int status = mybatisPlusUserMapper.deleteById(id);
    }

    // mapper相關(guān)測(cè)試
    // 更新
    @Test
    void editUser() {
        MybatisPlusUserBean user = new MybatisPlusUserBean();
        long userId = 11;
        MybatisPlusUserBean user2  = user.builder()
                .name("周杰倫13000")
                .age(3000)
                .email("woow.wu3000@gmail.com")
                .id(userId)
                .build();
        int status = mybatisPlusUserMapper.updateById(user2);
    }
}
  • mybatis-plus 的 mapper 查詢(xún) selectById

    image.png

  • mybatis-plus 的 mapper 插入 insert

    image.png

  • mybatisx插件如下


    image.png

(2) Mybatis-plus ( Service ) 的基本使用

(2.1) 以上 1 步,測(cè)試了extends BaseMapper,繼續(xù)測(cè)試 extends IService

  • ( mapper ) 是除了繼承mybatis-plus的 ( BaseMaper )
  • Interface接口 - ( interface ) 也可以繼承mybatis-plus的 ( IService )
  • Implements實(shí)現(xiàn)類(lèi) - (implements service) 繼承 ( ServiceImpl<mapper bean> )

(2.2) 編寫(xiě)一個(gè) Interface Service

MpIService - 是一個(gè)interface
-------

public interface MpIService extends IService<MybatisPlusUserBean> {
}

(2.3) 編寫(xiě)一個(gè)實(shí)現(xiàn)類(lèi)

MpIServiceImpl - 是一個(gè)實(shí)現(xiàn)類(lèi)
------

@Service
public class MpIServiceImpl extends ServiceImpl<MybatisPlusUserMapper, MybatisPlusUserBean> implements MpIService {
}

-------
// extends ServiceImpl<MybatisPlusUserMapper, MybatisPlusUserBean>
  // - MybatisPlusUserMapper 是上面繼承了 BaseMapper 的 mapper
// MpIServiceImpl 除了繼承 ServiceImpl 外,還需要實(shí)現(xiàn)我們自己定義的 MpIService
  // - MpIService 需要繼承 IS而ver

(2.4) 測(cè)試

@SpringBootTest
@Slf4j
public class MpIServiceTest {

    @Autowired
    MpIService mpIService;

    @Test
    void mpIServiceTest() {
        List<MybatisPlusUserBean> data = mpIService.list();
        log.info("{}", data);
    }
}
image.png

(3) Mybatis-plus 開(kāi)啟日志

mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 開(kāi)啟mybatis-plus日志


(四) springboot 單模塊項(xiàng)目打包和部署

(4.1) package 和 install 的區(qū)別

  • package 是簡(jiǎn)單的打包
  • install 除了打包,還會(huì)把這個(gè)打包好的包安裝到maven倉(cāng)庫(kù)中
  • 注意:如果 ( 其他項(xiàng)目依賴(lài)a項(xiàng)目 ),則應(yīng)該使用 ( install ) 來(lái)進(jìn)行打包a項(xiàng)目

打包

(4.2.1) 單模塊打包過(guò)程

  • 如果springboot項(xiàng)目中有jsp頁(yè)面,就只能打成war包,沒(méi)有則可以達(dá)成jar包
  • 步驟 ( idea方式 )
    • 1.添加 srping boot 的打包插件 spring-boot-maven-plugin
    <!-- maven插件 -->
    <plugins>
      <plugin>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
    
    • 2.在idea中關(guān)閉項(xiàng)目,清楚緩存,然后選擇 Lifecycle=> clean compile package,package打包說(shuō)明沒(méi)有依賴(lài)關(guān)系,不會(huì)打包后不會(huì)放在maven倉(cāng)庫(kù)中
    • 3.打完包,會(huì)生成 target 文件夾,里面就會(huì)有 .jar
  • 步驟( 使用命令的方式打包,不用idea來(lái)操作 )
1. mvn -Dmaven.test.skip -U clean package
-D 是指定參數(shù)的意思
-D maven.test.skip 表示跳過(guò)test檢查
-U 表示強(qiáng)制去遠(yuǎn)程更新插件或依賴(lài)
clean package 表示清楚target然后再打包
這個(gè)命令能執(zhí)行的前提是需要插件:Spring-boot-maven-plugin

(4.2.2) 多模塊打包過(guò)程

(1) 將父模塊的pom.xml文件中的build標(biāo)簽的所有內(nèi)容復(fù)制到controller模塊,因?yàn)閏ontroller其實(shí)已經(jīng)變成了主模塊
(2) 想build標(biāo)簽中添加 configuration => mainClass => application主類(lèi)的引用路徑
(3) 最終的controller模塊中的build標(biāo)簽內(nèi)容如下
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.woow.wu7.Application</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
(4) 然后在利用idea或者mvn命令來(lái)打包即可
image.png

(4.3) 如果打包過(guò)程報(bào)錯(cuò):提示test沒(méi)通過(guò),則可以設(shè)置忽略測(cè)試

  • 有兩種方式跳過(guò)test進(jìn)行打包
    • 1.在 pom.xml 中做如下配置
    <properties>
       <skipTests>true</skipTests>
    </properties> 
    
    • 2.添加plugin maven-surefire-plugin
    <plugin>
       <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-surefire-plugin</artifactId>
       <!-- 打包時(shí)跳過(guò)檢查 -->
       <!-- surefire 是萬(wàn)全,可靠的意思 -->
       <configuration>
          <skip>true</skip>
       </configuration>
    </plugin>
    

部署jar包

(4.4) 使用命令運(yùn)行 jar 包

結(jié)尾是否有 &
---

(1) java -jar 7-react-admin-java-0.0.1-SNAPSHOT.jar
// ssh通過(guò) ( ctrl+c ) 則可以終止java服務(wù)
// 缺點(diǎn):當(dāng)退出命令行工具后,java服務(wù)將不在運(yùn)行


(2) java -jar 7-react-admin-java-0.0.1-SNAPSHOT.jar &
// 1. 在尾部加上 ( & ) 表示該命令在后臺(tái)執(zhí)行,ssh通過(guò) ( ctrl+c ) 則無(wú)法終止java服務(wù)
// 2. 只有 ( 關(guān)閉命令窗口 ) 才會(huì)終止java程序
指定環(huán)境
---

(3) java -jar -Dspring.profiles.active=production target/7-react-admin-java-0.0.1-SNAPSHOT.jar
// -Dspring.profiles.active=production 指定環(huán)境變量是生產(chǎn)環(huán)境
// 則需要在 src/main/resources/application-production.properties 文件來(lái)配置生產(chǎn)環(huán)境中的配置
// -D 表示指定參數(shù)

(4.5) 持久化

  • nohup 和 & 的區(qū)別
    • 不加& 當(dāng)ctrl+c后就會(huì)終止java程序執(zhí)行
    • & 后臺(tái)運(yùn)行,但是 ( 關(guān)閉了終端或者ssh窗口 ) 就還是會(huì)終止java程序執(zhí)行
    • nohup + & ( 關(guān)閉終端或窗口 ) 仍然運(yùn)行,即持久化操作
nohup java -jar -Dspring.profiles.active=production target/7-react-admin-java-0.0.1-SNAPSHOT.jar >temp.txt &

(4.6) 查看已部署的java項(xiàng)目的后臺(tái)運(yùn)行進(jìn)程

  • ps -ef | grep java > a.txt
    • 檢查java程序是否存在,可以看到 PID PPID CMD等信息,即可以根據(jù)PID關(guān)閉相關(guān)進(jìn)程,比如java
    • ps 將某個(gè)進(jìn)程顯示出來(lái)
      • -e 顯示所有進(jìn)程
      • -f 全格式
    • grep 表示查找
    • ps -ef | grep java > a.txt 表示將查到的java后臺(tái)進(jìn)程結(jié)果寫(xiě)進(jìn) a.txt 文件中
      image.png

(4.7) 殺掉java進(jìn)程,終止java程序在后臺(tái)運(yùn)行

  • kill 進(jìn)程號(hào)
  • 進(jìn)程號(hào)可以通過(guò) ps -ef | grep java 來(lái)查看
  • kill 76619
image.png

部署war包

1. 如果是多模塊,并且想用war包的方式進(jìn)行部署,那么
2. 首先需要將controller模塊中的pom.xml中的 ( packaging ) 改成 ( war )
3. 然后需要在 ( src/main ) 中新建 ( webapp/WEB-INF/web.xml ) 文件
4. 然后輸入命令打包即可
image.png

資料

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容