国产成人精品18p,天天干成人网,无码专区狠狠躁天天躁,美女脱精光隐私扒开免费观看

教你怎么在IDEA中創(chuàng )建java多模塊項目

發(fā)布時(shí)間:2021-07-17 21:51 來(lái)源:腳本之家 閱讀:0 作者:老衲帥過(guò) 欄目: 編程語(yǔ)言 歡迎投稿:712375056

目錄

一、使用spring initializr創(chuàng )建java工程

  •  1、啟動(dòng)IDEA,新建java工程,使用向導創(chuàng )建一個(gè)springboot框架的工程

  • 2.設置項目信息,java版本選擇8

  • 3、勾選項目需要用到的依賴(lài)

  • 4、設置項目名稱(chēng),點(diǎn)擊完成

  • 5.等待maven將項目所需要的依賴(lài)都下載完畢,展開(kāi)項目結構,如下圖所示,這就創(chuàng )建完一個(gè)springboot框架的簡(jiǎn)單工程

 

二、修改工程,添加web模塊

  • 1、修改appdemo工程的pom文件,修改工程打包方式為pom,這樣項目就變成了一個(gè)父工程
<packaging>pom</packaging>

  • 2、打開(kāi)文件-新建-模塊,打開(kāi)新模塊創(chuàng )建向導,選擇maven模式,不需要選擇模板,點(diǎn)擊下一步

  • 3、設置模塊名稱(chēng)為web,可以看到父工程為appdemo,點(diǎn)擊完成

  • 4、等待maven導入模塊完畢,展開(kāi)項目結構,如下圖,appdemo工程中增加了web模塊

  • 5、在appdemo的pom文件中,會(huì )自動(dòng)添加模塊信息
<modules>
        <module>web</module>
    </modules>
  • 6、修改web模塊中的pom文件,增加打包方式<packaging>jar</packaging>

  • 7、展開(kāi)工程框架,將父工程中的包:com.example.appdemo,以及啟動(dòng)文件,都移動(dòng)到web模塊的java文件夾下

多模塊項目中,項目啟動(dòng)由web模塊進(jìn)行管理,所以需要將啟動(dòng)文件以及包結構,移動(dòng)到web模塊下

移動(dòng)完畢,項目架構如下

  • 8、刪除沒(méi)用的文件夾及文件,刪除紅框中的內容

在多模塊工程中,開(kāi)發(fā)各種代碼,分別在模塊中進(jìn)行,不在父工程中開(kāi)發(fā),所以父工程appdemo中的src文件夾,就沒(méi)用了

 

三、添加entity、service、serviceImpl、dao模塊

  • 1、按照添加web模塊的方式,添加entity、service、serviceImpl、dao模塊
  • 2、修改各模塊的pom文件,都增加打包方式:<packaging>jar</packaging>
  • 3、父工程中的pom文件,會(huì )自動(dòng)增加模塊信息
<modules>
        <module>web</module>
        <module>entity</module>
        <module>service</module>
        <module>serviceImpl</module>
        <module>dao</module>
    </modules>
  • 4、模塊全部添加完畢后,項目文件結構如下:

 

四、修改項目依賴(lài)信息

修改父項目依賴(lài)

  • 1、在第一步創(chuàng )建springboot框架項目后,pom文件中自動(dòng)添加了工程需要的依賴(lài),這個(gè)暫時(shí)不需要修改
<dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
  • 2、將各子模塊,做為依賴(lài),引入到父項目中(沒(méi)有引入web模塊,因為web模塊會(huì )依賴(lài)其他模塊,但是其他模塊不會(huì )依賴(lài)web模塊)
<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.example</groupId>
                <artifactId>entity</artifactId>
                <version>0.0.1-SNAPSHOT</version>
            </dependency>
            <dependency>
                <groupId>com.example</groupId>
                <artifactId>service</artifactId>
                <version>0.0.1-SNAPSHOT</version>
            </dependency>
            <dependency>
                <groupId>com.example</groupId>
                <artifactId>serviceImpl</artifactId>
                <version>0.0.1-SNAPSHOT</version>
            </dependency>
            <dependency>
                <groupId>com.example</groupId>
                <artifactId>dao</artifactId>
                <version>0.0.1-SNAPSHOT</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

使用dependencyManagement對依賴(lài)進(jìn)行管理,可以使子模塊在引用管理中的依賴(lài)時(shí),不用再設置版本號。

修改web模塊pom文件,增加如下依賴(lài)

<dependencies>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>entity</artifactId>
        </dependency>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>service</artifactId>
        </dependency>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>serviceImpl</artifactId>
        </dependency>
    </dependencies>

修改service模塊pom文件,增加如下依賴(lài)

<dependencies>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>entity</artifactId>
        </dependency>
    </dependencies>

修改serviceImpl模塊pom文件,增加如下依賴(lài)

<dependencies>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>entity</artifactId>
        </dependency>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>dao</artifactId>
        </dependency>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>service</artifactId>
        </dependency>
    </dependencies>

修改entity模塊pom文件,增加如下依賴(lài)

<dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
    </dependencies>

修改dao模塊pom文件,增加如下依賴(lài)

<dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
    </dependencies>

修改父項目appdemo的pom文件,刪除數據相關(guān)的依賴(lài)

<!--<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>-->

因為在父項目中設置的依賴(lài),子模塊中會(huì )自動(dòng)繼承,無(wú)需重復引用,但是并不是每個(gè)子模塊都會(huì )需要連接、操作數據的這些依賴(lài),所以在父項目的pom文件中,將這些依賴(lài)刪除,在涉及到連接數據庫,操作數據庫的dao模塊,以及涉及到使用實(shí)體類(lèi)創(chuàng )建表的entity模塊,單獨引入這些必要的依賴(lài)即可。

五、修改啟動(dòng)配置

因為啟動(dòng)類(lèi)AppdemoApplication已經(jīng)移動(dòng)到web模塊,并且要求項目是從web模塊啟動(dòng),所以需要刪除父項目中的啟動(dòng)配置,并在web的pom文件中增加啟動(dòng)配置

  • 1、注釋掉父項目pom文件中build部分
<!--<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>-->
  • 2、在web模塊的pom文件中增加build配置
	<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

比較簡(jiǎn)單的方法就是把父項目中的這個(gè)build配置項,復制到web模塊的pom中。
如果不做啟動(dòng)項的修改,在運行啟動(dòng)類(lèi)時(shí),會(huì )提示找不到main方法,導致項目無(wú)法啟動(dòng)。

六、在各模塊中編寫(xiě)代碼

在entity模塊中增加實(shí)體類(lèi)文件Response和StuInfo

  • 1、Response類(lèi)用于在controllor中給前端返回結果使用,代碼如下(為了減少代碼量,使用了lombok,如果不使用lombok,需要自己手動(dòng)編寫(xiě)setter/getter方法)
package com.example.entity.common;
import lombok.Data;

@Data
public class Response<T> {
    private int code;
    private String message;
    private T data;

    public Response(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }
}
  • 2、StuInfo類(lèi)作為數據庫建表、前后端數據傳輸、dao層操作數據的數據模板使用,代碼如下
package com.example.entity.stuEntity;
import lombok.Data;
import javax.persistence.*;
import java.sql.Timestamp;

@Data
@Entity
@Table(name = "stuinfo")
public class StuInfo{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;//id,鍵值,自增
    private int stuid;//學(xué)生id
    private String name;//姓名
    private int gender;//性別
    private int age;//年齡
    private int grade_num;//年級
    private int class_num;//班級
    private int status;//狀態(tài),1-數據有效,2-數據刪除
    private Timestamp createtime;//創(chuàng  )建時(shí)間
    private Timestamp updatetime;//更新時(shí)間
}

@Table(name = “stuinfo”)會(huì )報紅線(xiàn),如果不想顯示紅線(xiàn),需要在項目中配置數據庫信息,然后做下關(guān)聯(lián)就行,不做處理也無(wú)影響

在dao模塊中,增加數據庫操作接口

  • 1、因為操作數據庫,使用的是jpa,jap提供了很多封裝,此處只是用于做demo,所以不寫(xiě)過(guò)于復雜,只需要繼承JpaRepository接口即可,代碼如下
package com.example.dao;

import com.example.entity.stuEntity.StuInfo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface StuInfoDao extends JpaRepository<StuInfo, Long> {

}
  • 2、增加數據庫連接以及jpa配置文件

因為存在開(kāi)發(fā)環(huán)境,測試環(huán)境,預發(fā)環(huán)境,線(xiàn)上環(huán)境等多種環(huán)境,為了方便切換不同環(huán)境的,所以可以設置多個(gè)配置文件,配置文件名稱(chēng)格式為:application-XXX.yml,如開(kāi)發(fā)環(huán)境的,可以寫(xiě)成:application-dev.yml

如果引用dev的這個(gè)配置文件,只需要在配置文件application.yml中,激活該配置文件即可,格式如下

spring:
  profiles:
    active: dev

application-dev.yml中配置了數據庫連接和jpa等信息,內容如下(該部分需要根據自己的數據庫信息,還有數據庫的版本進(jìn)行調整)

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/demo_db?useUnicode=true&zeroDateTimeBehavior=convertToNull&autoReconnect=true&serverTimezone=UTC
#    url: jdbc:mysql://localhost:3306/demo_db?serverTimezone=UTC&useSSL=true&allowPublicKeyRetrieval=true&useUnicode=true
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver
  jpa:
    database: mysql
    show-sql: true
    hibernate:
      ddl-auto: create
      naming:
        implicit-strategy: org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy

以上配置文件中,

url:連接mysql數據庫的url,網(wǎng)上很多介紹,不做贅述

username、password:在選擇的時(shí)候,不要寫(xiě)成data-username、data-password,否則啟動(dòng)項目的時(shí)候,會(huì )報連接數據庫賬號無(wú)效或無(wú)權限的情況,我是經(jīng)常會(huì )寫(xiě)錯,導致連接數據庫的經(jīng)常出現問(wèn)題。

driver-class-name:這個(gè)驅動(dòng)配置,不同版本的要求不同,不過(guò)現在高版本的驅動(dòng)應該都是這個(gè)

在service模塊中定義接口StuService

接口代碼如下

package com.example.service;

import com.example.entity.stuEntity.StuInfo;

public interface StuService {

    Boolean save(StuInfo stuInfo);
}

在serviceImpl模塊中編寫(xiě)接口實(shí)現類(lèi)

代碼如下

package com.example.serviceImpl;

import com.example.dao.StuInfoDao;
import com.example.entity.stuEntity.StuInfo;
import com.example.service.StuService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.sql.Timestamp;

@Service
@Log4j2
public class StuServiceImpl implements StuService {
    @Autowired
    private StuInfoDao stuInfoDao;

    @Override
    public Boolean save(StuInfo stuInfo) {
        stuInfo.setStatus(1);
        stuInfo.setCreatetime(new Timestamp(System.currentTimeMillis()));
        stuInfo.setUpdatetime(new Timestamp(System.currentTimeMillis()));
        log.error("測試日志");
        return Boolean.TRUE;
    }
}

@Service,標注該類(lèi)為接口實(shí)現類(lèi),可供spring掃描注入

@Log4j2,日志工具

@Autowired,將StuInfoDao進(jìn)行注入

在web模塊中編寫(xiě)controllor類(lèi),供前端請求調用

代碼如下

package com.example.appdemo.controllor;

import com.example.entity.common.Response;
import com.example.entity.stuEntity.StuInfo;
import com.example.service.StuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/stu")
public class StuInfoControllor {
    @Autowired
    private StuService stuService;

    @PostMapping("/addStu")
    public Response<Boolean> addStu(@RequestBody StuInfo stuInfo) {
        if (stuService.save(stuInfo)) {
            return new Response<>(200, "保存成功", Boolean.TRUE);
        } else {
            return new Response<>(400, "保存失敗", Boolean.FALSE);
        }
    }
}

修改啟動(dòng)類(lèi)AppdemoApplication,增加掃描注解

工程在啟動(dòng)過(guò)程中,spring會(huì )對工程內的類(lèi)進(jìn)行掃描,自動(dòng)生成對象供程序調用,但是有時(shí)候工程自動(dòng)掃描時(shí)會(huì )忽略某些類(lèi),這就需要明確指定需要掃描的包,啟動(dòng)類(lèi)代碼如下

package com.example.appdemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@SpringBootApplication(scanBasePackages = "com.example")
@ComponentScan({"com.example"})
@EnableJpaRepositories("com.example")
@EntityScan("com.example.entity")
public class AppdemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(AppdemoApplication.class, args);
    }
}

七、清理、安裝、運行、測試

  • 1、使用maven工具【clean】上次打包的文件
  • 2、清理完畢后,【install】程序,將各模塊進(jìn)行編譯,方便模塊之間的依賴(lài)和調用
  • 3、安裝完成后,右鍵運行啟動(dòng)類(lèi),成功運行
  • 4、使用postman調用接口,content-type設置為【application/json;charset=utf-8】

 

八、搭架子時(shí)碰到的問(wèn)題

  • 1、數據庫連接的配置文件錯誤,導致連接數據庫時(shí)報用戶(hù)名不能連接,這個(gè)注意配置的關(guān)鍵字不要寫(xiě)錯
  • 2、各模塊的依賴(lài),有些依賴(lài)會(huì )和父項目沖突,這個(gè)需要調整,重復引用的的依賴(lài),需要做依賴(lài)的清理
  • 3、spring的某些自動(dòng)注入不能實(shí)現,很多是由于spring掃描忽略導致的,需要在啟動(dòng)類(lèi)中添加掃描的包
  • 4、項目的改來(lái)改去,碰到了Error:java:JDK isn't specified for module錯誤,這個(gè)只要把工程父項目的【.idea】文件夾刪除,重新刷新生成即可解決
  • 5、需要注意的地方,每個(gè)模塊的包名結構,應該是一樣的,比如我的,就都是com.example的

九、好玩的配置

  • 1、在線(xiàn)生成一個(gè)banner文件樣式
  • 2、將生成的字符復制到文件 banner.txt 中
  • 3、將 banner.txt 文件放到web模塊的resource中,啟動(dòng)項目會(huì )看到你設置的字符

到此這篇關(guān)于教你怎么在IDEA中創(chuàng )建java多模塊項目的文章就介紹到這了,更多相關(guān)IDEA中創(chuàng )建java多模塊項目?jì)热菡埶阉髂_本之家以前的文章或繼續瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。

亚洲综合久久成人AV| 特黄A级毛片免费视频| 熟妇高潮一区二区三区 | 在厨房被C到高潮A毛片奶水| 国产免费不卡午夜福利在线| 成人网站免费看黄A站视频|