您好,登錄后才能下訂單哦!
這篇文章主要介紹了SpringBoot多模塊項(xiàng)目框架搭建過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
創(chuàng)建根項(xiàng)目,New Project 創(chuàng)建一個(gè)名為 sms-bomber 的 SpringBoot 新項(xiàng)目,打包為 JAR 的即可,這里只添加 Lombok 與 spring-boot-starter-web 依賴,這兩個(gè)依賴會(huì)傳遞給所有子模塊,刪除創(chuàng)建完成的項(xiàng)目中的 .mvn\ src\ mvnw mvnw.cmd
創(chuàng)建啟動(dòng)模塊,根目錄項(xiàng)目名上右鍵 New -> Model 選擇 Maven 類型,不要選擇 create from archetype,Parent 屬性選擇剛才創(chuàng)建的根項(xiàng)目,先創(chuàng)建一個(gè) ${PREFIXE}-start 的模塊,用于啟動(dòng)項(xiàng)目,start 模塊 resources 目錄下創(chuàng)建
server: servlet: # 項(xiàng)目上下文路徑 context-path: /sms-bomber spring: main: # 關(guān)閉 banner banner-mode: off profiles: # 測(cè)試環(huán)境 active: dev-mysql # 生產(chǎn)環(huán)境 # active: prod-mysql
以及 logback.xml
<?xml version="1.0" encoding="UTF-8"?> <configuration scan="true" scanPeriod="30 seconds"> <!-- FIXME 記得更改如下配置 --> <!-- CONSOLE/FILE --> <property name="output-ref" value="CONSOLE" /> <property name="logfile-dir" value="D:/Tmp/Log" /> <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder> </appender> <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <encoder> <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> <charset>utf-8</charset> </encoder> <file>${logfile-dir}/ticket.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy"> <fileNamePattern>${logfile-dir}/ticket.%i.log</fileNamePattern> <minIndex>1</minIndex> <maxIndex>5</maxIndex> </rollingPolicy> <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> <maxFileSize>1MB</maxFileSize> </triggeringPolicy> </appender> <root level="INFO"> <appender-ref ref="${output-ref}" /> </root> <!-- 指定包的日志等級(jí) --> <!-- 使用 additivity="true" 會(huì)同時(shí)使用 appender-ref --> <logger name="com.seliote.smsbomber" level="DEBUG" additivity="false"> <appender-ref ref="${output-ref}" /> </logger> </configuration>
創(chuàng)建項(xiàng)目啟動(dòng)類
package com.seliote.smsbomber; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * 項(xiàng)目啟動(dòng)類 * * @author LiYangDi * @since 2020/1/26 */ @SpringBootApplication(scanBasePackages = "com.seliote.smsbomber") public class Application { public static void main(String... args) { SpringApplication.run(Application.class, args); } }
創(chuàng)建 Servlet 初始化類
package com.seliote.smsbomber; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; /** * 打 WAR 包 Servlet 初始化 * * @author LiYangDi * @since 2020/1/26 */ public class ServletInitializer extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(Application.class); } }
修改 IDEA 的啟動(dòng)類,嘗試啟動(dòng)項(xiàng)目
修改啟動(dòng)模塊打包方式為 war, 修改啟動(dòng)模塊的 pom.xml,project 節(jié)點(diǎn)下添加 <packaging>war</packaging> 與
<build> <plugins> <!-- 打包配置 --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <!-- war 包名 --> <finalName>sms-bomber</finalName> </configuration> </plugin> </plugins> </build>
建一個(gè) TestCtr,maven package 一下把 war 扔進(jìn) Tomcat 里體驗(yàn)一下
創(chuàng)建模塊 persistence, service, web, common 層模塊層模塊,并配置相關(guān)依賴,根項(xiàng)目 pom.xml 里添加依賴 project 節(jié)點(diǎn)下添加,再依次
<!-- 用于多模塊版本管理 --> <dependencyManagement> <dependencies> <dependency> <groupId>com.seliote</groupId> <artifactId>sms-bomber-common</artifactId> <version>${sms-bomber.version}</version> </dependency> <dependency> <groupId>com.seliote</groupId> <artifactId>sms-bomber-persistence</artifactId> <version>${sms-bomber.version}</version> </dependency> <dependency> <groupId>com.seliote</groupId> <artifactId>sms-bomber-service</artifactId> <version>${sms-bomber.version}</version> </dependency> <dependency> <groupId>com.seliote</groupId> <artifactId>sms-bomber-web</artifactId> <version>${sms-bomber.version}</version> </dependency> <dependency> <groupId>com.seliote</groupId> <artifactId>sms-bomber-start</artifactId> <version>${sms-bomber.version}</version> </dependency> <dependency> <groupId>org.mariadb.jdbc</groupId> <artifactId>mariadb-java-client</artifactId> <version>${mariadb.version}</version> </dependency> </dependencies> </dependencyManagement>
dependencies 節(jié)點(diǎn)下添加用于全局模塊使用
<dependency> <groupId>com.seliote</groupId> <artifactId>sms-bomber-common</artifactId> </dependency>
${sms-bomber.version} 等版本定義在 properties 標(biāo)簽下定義
persistence 模塊的 pom.xml 下添加依賴
<dependencies> <!-- MariaDB 驅(qū)動(dòng) --> <dependency> <groupId>org.mariadb.jdbc</groupId> <artifactId>mariadb-java-client</artifactId> <scope>runtime</scope> </dependency> <!-- 持久層框架 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> </dependencies>
service 模塊 pom.xml 下添加
<dependencies> <dependency> <groupId>com.seliote</groupId> <artifactId>sms-bomber-persistence</artifactId> </dependency> </dependencies>
web 模塊 pom.xml 下添加
<dependencies> <dependency> <groupId>com.seliote</groupId> <artifactId>sms-bomber-service</artifactId> </dependency> </dependencies>
start 模塊 pom.xml 下添加
<dependencies> <dependency> <groupId>com.seliote</groupId> <artifactId>sms-bomber-web</artifactId> </dependency> </dependencies>
配置 SpringDataJpa
dev yml 下添加
spring: datasource: driver-class-name: org.mariadb.jdbc.Driver url: jdbc:mariadb://127.0.0.1:3306/sms_bomber username: sms_bomber password: 123456!@#$%^qp jpa: database-platform: org.hibernate.dialect.MariaDB103Dialect open-in-view: false show-sql: true
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。