教你怎么在IDEA中創(chuàng)建java多模塊項(xiàng)目
一、使用spring initializr創(chuàng)建java工程
- 1、啟動(dòng)IDEA,新建java工程,使用向?qū)?chuàng)建一個(gè)springboot框架的工程

- 2.設(shè)置項(xiàng)目信息,java版本選擇8

- 3、勾選項(xiàng)目需要用到的依賴

- 4、設(shè)置項(xiàng)目名稱,點(diǎn)擊完成

- 5.等待maven將項(xiàng)目所需要的依賴都下載完畢,展開項(xiàng)目結(jié)構(gòu),如下圖所示,這就創(chuàng)建完一個(gè)springboot框架的簡(jiǎn)單工程
二、修改工程,添加web模塊
- 1、修改appdemo工程的pom文件,修改工程打包方式為pom,這樣項(xiàng)目就變成了一個(gè)父工程
<packaging>pom</packaging>

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

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

- 4、等待maven導(dǎo)入模塊完畢,展開項(xiàng)目結(jié)構(gòu),如下圖,appdemo工程中增加了web模塊

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

- 7、展開工程框架,將父工程中的包:com.example.appdemo,以及啟動(dòng)文件,都移動(dòng)到web模塊的java文件夾下
多模塊項(xiàng)目中,項(xiàng)目啟動(dòng)由web模塊進(jìn)行管理,所以需要將啟動(dòng)文件以及包結(jié)構(gòu),移動(dòng)到web模塊下

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

- 8、刪除沒用的文件夾及文件,刪除紅框中的內(nèi)容
在多模塊工程中,開發(fā)各種代碼,分別在模塊中進(jìn)行,不在父工程中開發(fā),所以父工程appdemo中的src文件夾,就沒用了
三、添加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、模塊全部添加完畢后,項(xiàng)目文件結(jié)構(gòu)如下:
四、修改項(xiàng)目依賴信息
修改父項(xiàng)目依賴
- 1、在第一步創(chuàng)建springboot框架項(xiàng)目后,pom文件中自動(dòng)添加了工程需要的依賴,這個(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、將各子模塊,做為依賴,引入到父項(xiàng)目中(沒有引入web模塊,因?yàn)閣eb模塊會(huì)依賴其他模塊,但是其他模塊不會(huì)依賴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對(duì)依賴進(jìn)行管理,可以使子模塊在引用管理中的依賴時(shí),不用再設(shè)置版本號(hào)。
修改web模塊pom文件,增加如下依賴
<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文件,增加如下依賴
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>entity</artifactId>
</dependency>
</dependencies>
修改serviceImpl模塊pom文件,增加如下依賴
<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文件,增加如下依賴
<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文件,增加如下依賴
<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>
修改父項(xiàng)目appdemo的pom文件,刪除數(shù)據(jù)庫(kù)相關(guān)的依賴
<!--<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>-->
因?yàn)樵诟疙?xiàng)目中設(shè)置的依賴,子模塊中會(huì)自動(dòng)繼承,無需重復(fù)引用,但是并不是每個(gè)子模塊都會(huì)需要連接、操作數(shù)據(jù)的這些依賴,所以在父項(xiàng)目的pom文件中,將這些依賴刪除,在涉及到連接數(shù)據(jù)庫(kù),操作數(shù)據(jù)庫(kù)的dao模塊,以及涉及到使用實(shí)體類創(chuàng)建表的entity模塊,單獨(dú)引入這些必要的依賴即可。
五、修改啟動(dòng)配置
因?yàn)閱?dòng)類AppdemoApplication已經(jīng)移動(dòng)到web模塊,并且要求項(xiàng)目是從web模塊啟動(dòng),所以需要?jiǎng)h除父項(xiàng)目中的啟動(dòng)配置,并在web的pom文件中增加啟動(dòng)配置
- 1、注釋掉父項(xiàng)目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)單的方法就是把父項(xiàng)目中的這個(gè)build配置項(xiàng),復(fù)制到web模塊的pom中。
如果不做啟動(dòng)項(xiàng)的修改,在運(yùn)行啟動(dòng)類時(shí),會(huì)提示找不到main方法,導(dǎo)致項(xiàng)目無法啟動(dòng)。
六、在各模塊中編寫代碼
在entity模塊中增加實(shí)體類文件Response和StuInfo

- 1、Response類用于在controllor中給前端返回結(jié)果使用,代碼如下(為了減少代碼量,使用了lombok,如果不使用lombok,需要自己手動(dòng)編寫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類作為數(shù)據(jù)庫(kù)建表、前后端數(shù)據(jù)傳輸、dao層操作數(shù)據(jù)的數(shù)據(jù)模板使用,代碼如下
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;//年級(jí)
private int class_num;//班級(jí)
private int status;//狀態(tài),1-數(shù)據(jù)有效,2-數(shù)據(jù)刪除
private Timestamp createtime;//創(chuàng)建時(shí)間
private Timestamp updatetime;//更新時(shí)間
}
@Table(name = “stuinfo”)會(huì)報(bào)紅線,如果不想顯示紅線,需要在項(xiàng)目中配置數(shù)據(jù)庫(kù)信息,然后做下關(guān)聯(lián)就行,不做處理也無影響
在dao模塊中,增加數(shù)據(jù)庫(kù)操作接口

- 1、因?yàn)椴僮鲾?shù)據(jù)庫(kù),使用的是jpa,jap提供了很多封裝,此處只是用于做demo,所以不寫過于復(fù)雜,只需要繼承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、增加數(shù)據(jù)庫(kù)連接以及jpa配置文件
因?yàn)榇嬖陂_發(fā)環(huán)境,測(cè)試環(huán)境,預(yù)發(fā)環(huán)境,線上環(huán)境等多種環(huán)境,為了方便切換不同環(huán)境的,所以可以設(shè)置多個(gè)配置文件,配置文件名稱格式為:application-XXX.yml,如開發(fā)環(huán)境的,可以寫成:application-dev.yml
如果引用dev的這個(gè)配置文件,只需要在配置文件application.yml中,激活該配置文件即可,格式如下
spring:
profiles:
active: dev
application-dev.yml中配置了數(shù)據(jù)庫(kù)連接和jpa等信息,內(nèi)容如下(該部分需要根據(jù)自己的數(shù)據(jù)庫(kù)信息,還有數(shù)據(jù)庫(kù)的版本進(jìn)行調(diào)整)
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數(shù)據(jù)庫(kù)的url,網(wǎng)上很多介紹,不做贅述
username、password:在選擇的時(shí)候,不要寫成data-username、data-password,否則啟動(dòng)項(xiàng)目的時(shí)候,會(huì)報(bào)連接數(shù)據(jù)庫(kù)賬號(hào)無效或無權(quán)限的情況,我是經(jīng)常會(huì)寫錯(cuò),導(dǎo)致連接數(shù)據(jù)庫(kù)的經(jīng)常出現(xiàn)問題。
driver-class-name:這個(gè)驅(qū)動(dòng)配置,不同版本的要求不同,不過現(xiàn)在高版本的驅(qū)動(dòng)應(yīng)該都是這個(gè)
在service模塊中定義接口StuService

接口代碼如下
package com.example.service;
import com.example.entity.stuEntity.StuInfo;
public interface StuService {
Boolean save(StuInfo stuInfo);
}
在serviceImpl模塊中編寫接口實(shí)現(xiàn)類

代碼如下
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("測(cè)試日志");
return Boolean.TRUE;
}
}
@Service,標(biāo)注該類為接口實(shí)現(xiàn)類,可供spring掃描注入
@Log4j2,日志工具
@Autowired,將StuInfoDao進(jìn)行注入
在web模塊中編寫controllor類,供前端請(qǐng)求調(diào)用

代碼如下
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)類AppdemoApplication,增加掃描注解
工程在啟動(dòng)過程中,spring會(huì)對(duì)工程內(nèi)的類進(jìn)行掃描,自動(dòng)生成對(duì)象供程序調(diào)用,但是有時(shí)候工程自動(dòng)掃描時(shí)會(huì)忽略某些類,這就需要明確指定需要掃描的包,啟動(dòng)類代碼如下
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);
}
}
七、清理、安裝、運(yùn)行、測(cè)試

- 1、使用maven工具【clean】上次打包的文件
- 2、清理完畢后,【install】程序,將各模塊進(jìn)行編譯,方便模塊之間的依賴和調(diào)用
- 3、安裝完成后,右鍵運(yùn)行啟動(dòng)類,成功運(yùn)行
- 4、使用postman調(diào)用接口,content-type設(shè)置為【application/json;charset=utf-8】
八、搭架子時(shí)碰到的問題
- 1、數(shù)據(jù)庫(kù)連接的配置文件錯(cuò)誤,導(dǎo)致連接數(shù)據(jù)庫(kù)時(shí)報(bào)用戶名不能連接,這個(gè)注意配置的關(guān)鍵字不要寫錯(cuò)
- 2、各模塊的依賴,有些依賴會(huì)和父項(xiàng)目沖突,這個(gè)需要調(diào)整,重復(fù)引用的的依賴,需要做依賴的清理
- 3、spring的某些自動(dòng)注入不能實(shí)現(xiàn),很多是由于spring掃描忽略導(dǎo)致的,需要在啟動(dòng)類中添加掃描的包
- 4、項(xiàng)目的改來改去,碰到了
Error:java:JDK isn't specified for module錯(cuò)誤,這個(gè)只要把工程父項(xiàng)目的【.idea】文件夾刪除,重新刷新生成即可解決 - 5、需要注意的地方,每個(gè)模塊的包名結(jié)構(gòu),應(yīng)該是一樣的,比如我的,就都是com.example的
九、好玩的配置
- 1、在線生成一個(gè)banner文件樣式
- 2、將生成的字符復(fù)制到文件 banner.txt 中
- 3、將 banner.txt 文件放到web模塊的resource中,啟動(dòng)項(xiàng)目會(huì)看到你設(shè)置的字符
到此這篇關(guān)于教你怎么在IDEA中創(chuàng)建java多模塊項(xiàng)目的文章就介紹到這了,更多相關(guān)IDEA中創(chuàng)建java多模塊項(xiàng)目?jī)?nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
徹底理解Spring注解@Autowired實(shí)現(xiàn)原理
這篇文章主要為大家詳細(xì)的介紹了Spring注解@Autowired實(shí)現(xiàn)的原理,縝密的邏輯分析,實(shí)踐應(yīng)用示例操作說明,讓大家徹底的理解Spring注解@Autowired背后實(shí)現(xiàn)原理2022-03-03
新手也能看懂的SpringBoot異步編程指南(簡(jiǎn)單易懂)
這篇文章主要介紹了新手也能看懂的SpringBoot異步編程指南(簡(jiǎn)單易懂),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
Java的Struts框架中配置國(guó)際化的資源存儲(chǔ)的要點(diǎn)解析
這篇文章主要介紹了Java的Struts框架中配置國(guó)際化的資源存儲(chǔ)的要點(diǎn)解析,針對(duì)用戶所使用的語言來配置資源文件,需要的朋友可以參考下2016-04-04
Java對(duì)象和Json文本轉(zhuǎn)換工具類的實(shí)現(xiàn)
Json?是一個(gè)用于Java對(duì)象和Json文本相互轉(zhuǎn)換的工具類,本文主要介紹了Java對(duì)象和Json文本轉(zhuǎn)換工具類,具有一定的參考價(jià)值,感興趣的可以了解一下2022-03-03
SpringBoot項(xiàng)目實(shí)現(xiàn)統(tǒng)一異常處理的最佳方案
在前后端分離的項(xiàng)目開發(fā)過程中,我們通常會(huì)對(duì)數(shù)據(jù)返回格式進(jìn)行統(tǒng)一的處理,這樣可以方便前端人員取數(shù)據(jù),后端發(fā)生異常時(shí)同樣會(huì)使用此格式將異常信息返回給前端,本文介紹了如何在SpringBoot項(xiàng)目中實(shí)現(xiàn)統(tǒng)一異常處理,如有錯(cuò)誤,還望批評(píng)指正2024-02-02

