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

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

- 3、勾選項目需要用到的依賴

- 4、設置項目名稱,點擊完成

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

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

- 3、設置模塊名稱為web,可以看到父工程為appdemo,點擊完成

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

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

- 7、展開工程框架,將父工程中的包:com.example.appdemo,以及啟動文件,都移動到web模塊的java文件夾下
多模塊項目中,項目啟動由web模塊進行管理,所以需要將啟動文件以及包結構,移動到web模塊下

移動完畢,項目架構如下

- 8、刪除沒用的文件夾及文件,刪除紅框中的內容
在多模塊工程中,開發(fā)各種代碼,分別在模塊中進行,不在父工程中開發(fā),所以父工程appdemo中的src文件夾,就沒用了
三、添加entity、service、serviceImpl、dao模塊
- 1、按照添加web模塊的方式,添加entity、service、serviceImpl、dao模塊
- 2、修改各模塊的pom文件,都增加打包方式:
<packaging>jar</packaging> - 3、父工程中的pom文件,會自動增加模塊信息
<modules>
<module>web</module>
<module>entity</module>
<module>service</module>
<module>serviceImpl</module>
<module>dao</module>
</modules>
- 4、模塊全部添加完畢后,項目文件結構如下:
四、修改項目依賴信息
修改父項目依賴
- 1、在第一步創(chuàng)建springboot框架項目后,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>
<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、將各子模塊,做為依賴,引入到父項目中(沒有引入web模塊,因為web模塊會依賴其他模塊,但是其他模塊不會依賴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對依賴進行管理,可以使子模塊在引用管理中的依賴時,不用再設置版本號。
修改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>
修改父項目appdemo的pom文件,刪除數(shù)據(jù)庫相關的依賴
<!--<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>-->
因為在父項目中設置的依賴,子模塊中會自動繼承,無需重復引用,但是并不是每個子模塊都會需要連接、操作數(shù)據(jù)的這些依賴,所以在父項目的pom文件中,將這些依賴刪除,在涉及到連接數(shù)據(jù)庫,操作數(shù)據(jù)庫的dao模塊,以及涉及到使用實體類創(chuàng)建表的entity模塊,單獨引入這些必要的依賴即可。
五、修改啟動配置
因為啟動類AppdemoApplication已經移動到web模塊,并且要求項目是從web模塊啟動,所以需要刪除父項目中的啟動配置,并在web的pom文件中增加啟動配置
- 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>
比較簡單的方法就是把父項目中的這個build配置項,復制到web模塊的pom中。
如果不做啟動項的修改,在運行啟動類時,會提示找不到main方法,導致項目無法啟動。
六、在各模塊中編寫代碼
在entity模塊中增加實體類文件Response和StuInfo

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

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

接口代碼如下
package com.example.service;
import com.example.entity.stuEntity.StuInfo;
public interface StuService {
Boolean save(StuInfo stuInfo);
}
在serviceImpl模塊中編寫接口實現(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("測試日志");
return Boolean.TRUE;
}
}
@Service,標注該類為接口實現(xiàn)類,可供spring掃描注入
@Log4j2,日志工具
@Autowired,將StuInfoDao進行注入
在web模塊中編寫controllor類,供前端請求調用

代碼如下
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);
}
}
}
修改啟動類AppdemoApplication,增加掃描注解
工程在啟動過程中,spring會對工程內的類進行掃描,自動生成對象供程序調用,但是有時候工程自動掃描時會忽略某些類,這就需要明確指定需要掃描的包,啟動類代碼如下
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】程序,將各模塊進行編譯,方便模塊之間的依賴和調用
- 3、安裝完成后,右鍵運行啟動類,成功運行
- 4、使用postman調用接口,content-type設置為【application/json;charset=utf-8】
八、搭架子時碰到的問題
- 1、數(shù)據(jù)庫連接的配置文件錯誤,導致連接數(shù)據(jù)庫時報用戶名不能連接,這個注意配置的關鍵字不要寫錯
- 2、各模塊的依賴,有些依賴會和父項目沖突,這個需要調整,重復引用的的依賴,需要做依賴的清理
- 3、spring的某些自動注入不能實現(xiàn),很多是由于spring掃描忽略導致的,需要在啟動類中添加掃描的包
- 4、項目的改來改去,碰到了
Error:java:JDK isn't specified for module錯誤,這個只要把工程父項目的【.idea】文件夾刪除,重新刷新生成即可解決 - 5、需要注意的地方,每個模塊的包名結構,應該是一樣的,比如我的,就都是com.example的
九、好玩的配置
- 1、在線生成一個banner文件樣式
- 2、將生成的字符復制到文件 banner.txt 中
- 3、將 banner.txt 文件放到web模塊的resource中,啟動項目會看到你設置的字符
到此這篇關于教你怎么在IDEA中創(chuàng)建java多模塊項目的文章就介紹到這了,更多相關IDEA中創(chuàng)建java多模塊項目內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
徹底理解Spring注解@Autowired實現(xiàn)原理
這篇文章主要為大家詳細的介紹了Spring注解@Autowired實現(xiàn)的原理,縝密的邏輯分析,實踐應用示例操作說明,讓大家徹底的理解Spring注解@Autowired背后實現(xiàn)原理2022-03-03
SpringBoot項目實現(xiàn)統(tǒng)一異常處理的最佳方案
在前后端分離的項目開發(fā)過程中,我們通常會對數(shù)據(jù)返回格式進行統(tǒng)一的處理,這樣可以方便前端人員取數(shù)據(jù),后端發(fā)生異常時同樣會使用此格式將異常信息返回給前端,本文介紹了如何在SpringBoot項目中實現(xiàn)統(tǒng)一異常處理,如有錯誤,還望批評指正2024-02-02

