- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) > 編程語(yǔ)言 >
- 教你怎么在IDEA中創(chuàng )建java多模塊項目
<packaging>pom</packaging>
<modules> <module>web</module> </modules>
<packaging>jar</packaging>
多模塊項目中,項目啟動(dòng)由web模塊進(jìn)行管理,所以需要將啟動(dòng)文件以及包結構,移動(dòng)到web模塊下
移動(dòng)完畢,項目架構如下
在多模塊工程中,開(kāi)發(fā)各種代碼,分別在模塊中進(jìn)行,不在父工程中開(kāi)發(fā),所以父工程appdemo中的src文件夾,就沒(méi)用了
<packaging>jar</packaging>
<modules> <module>web</module> <module>entity</module> <module>service</module> <module>serviceImpl</module> <module>dao</module> </modules>
修改父項目依賴(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> <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>
<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)類(lèi)AppdemoApplication已經(jīng)移動(dòng)到web模塊,并且要求項目是從web模塊啟動(dòng),所以需要刪除父項目中的啟動(dòng)配置,并在web的pom文件中增加啟動(dòng)配置
<!--<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> <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)。
在entity模塊中增加實(shí)體類(lèi)文件Response和StuInfo
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; } }
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模塊中,增加數據庫操作接口
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> { }
因為存在開(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); } }
Error:java:JDK isn't specified for module
錯誤,這個(gè)只要把工程父項目的【.idea】文件夾刪除,重新刷新生成即可解決到此這篇關(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í)歡迎投稿傳遞力量。
Copyright ? 2009-2022 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 特網(wǎng)科技 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 百度云 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站