Springboot分模塊項(xiàng)目搭建的實(shí)現(xiàn)
一、創(chuàng)建聚合父工程
(1) eclipse -> File -> new -> Other… -> Maven -> Maven Project

(2) configure project

(3) pom.xml配置
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<description>SpringBoot分模塊</description>
<modelVersion>4.0.0</modelVersion>
<groupId>com.button</groupId>
<artifactId>springboot-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<springboot-web.version>0.0.1-SNAPSHOT</springboot-web.version>
<springboot-service.version>0.0.1-SNAPSHOT</springboot-service.version>
<springboot-dao.version>0.0.1-SNAPSHOT</springboot-dao.version>
<springboot-entity.version>0.0.1-SNAPSHOT</springboot-entity.version>
<mybatis-spring-boot-starter.version>1.1.1</mybatis-spring-boot-starter.version>
<HikariCP.version>2.4.13</HikariCP.version>
<pagehelper.version>5.0.0</pagehelper.version>
<pagehelper-spring-boot-autoconfigure.version>1.2.3</pagehelper-spring-boot-autoconfigure.version>
<pagehelper-spring-boot-starter.version>1.2.3</pagehelper-spring-boot-starter.version>
<json-lib.version>2.2.2</json-lib.version>
</properties>
<!-- 這里繼承SpringBoot提供的父工程 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath />
</parent>
<!-- 這里聲明多個(gè)子模塊 -->
<modules>
<module>springboot-web</module>
<module>springboot-service</module>
<module>springboot-dao</module>
<module>springboot-entity</module>
</modules>
<!-- 這里統(tǒng)一管理依賴的版本號(hào) -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.button</groupId>
<artifactId>springboot-web</artifactId>
<version>${springboot-web.version}</version>
</dependency>
<dependency>
<groupId>com.button</groupId>
<artifactId>springboot-service</artifactId>
<version>${springboot-service.version}</version>
</dependency>
<dependency>
<groupId>com.button</groupId>
<artifactId>springboot-dao</artifactId>
<version>${springboot-dao.version}</version>
</dependency>
<dependency>
<groupId>com.button</groupId>
<artifactId>springboot-entity</artifactId>
<version>${springboot-entity.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis-spring-boot-starter.version}</version>
</dependency>
<!--mysql鏈接依賴 -->
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP-java7</artifactId>
<version>${HikariCP.version}</version>
</dependency>
<!-- 分頁(yè)插件pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>${pagehelper.version}</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-autoconfigure</artifactId>
<version>${pagehelper-spring-boot-autoconfigure.version}</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>${pagehelper-spring-boot-starter.version}</version>
</dependency>
<!-- json -->
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>${json-lib.version}</version>
<classifier>jdk15</classifier>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<!-- java編譯插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
二、創(chuàng)建springboot-web
(1) 右鍵點(diǎn)擊父工程 -> new -> Other… -> Maven -> Maven Module

(2) pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <artifactId>springboot-web</artifactId> <packaging>jar</packaging> <name>springboot-web</name> <parent> <groupId>com.button</groupId> <artifactId>springboot-parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <!-- Web模塊相關(guān)依賴 --> <dependencies> <dependency> <groupId>com.button</groupId> <artifactId>springboot-service</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> <scope>true</scope> </dependency> </dependencies> <!--只需在啟動(dòng)類(lèi)所在模塊的POM文件:指定打包插件 --> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <jvmArguments>-Dfile.encoding=UTF-8</jvmArguments> <fork>true</fork><!-- 沒(méi)有該配置,devtools 不生效 --> </configuration> </plugin> </plugins> </build> </project>
解釋:
pom文件中的如下配置:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
<scope>true</scope>
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!-- 沒(méi)有該配置,devtools 不生效 -->
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
如上的兩項(xiàng)配置,添加之后會(huì)打開(kāi)springboot的熱部署,這樣每次修改文件之后,不用手動(dòng)重新啟動(dòng)項(xiàng)目。配置熱部署可以讓項(xiàng)目自動(dòng)加載變化的文件,省去的手動(dòng)操作。(項(xiàng)目搭建好之后,啟動(dòng)項(xiàng)目,隨便創(chuàng)建/修改一個(gè)文件并保存,會(huì)發(fā)現(xiàn)控制臺(tái)打印 springboot 重新加載文件的log。)
三、創(chuàng)建springboot-service
參照如上創(chuàng)建方式
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <artifactId>springboot-service</artifactId> <packaging>jar</packaging> <name>springboot-service</name> <parent> <groupId>com.button</groupId> <artifactId>springboot-parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <dependencies> <dependency> <groupId>com.button</groupId> <artifactId>springboot-dao</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> </dependency> </dependencies> </project>
四、創(chuàng)建springboot-dao
參照如上創(chuàng)建方式
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <artifactId>springboot-dao</artifactId> <packaging>jar</packaging> <name>springboot-dao</name> <parent> <groupId>com.button</groupId> <artifactId>springboot-parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <dependencies> <dependency> <groupId>com.button</groupId> <artifactId>springboot-entity</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> <!--數(shù)據(jù)庫(kù)連接jdbc依賴 --> <!-- JDBC連接數(shù)據(jù)庫(kù),因?yàn)橐肏ikariCP,所以需要將SpringBoot中的tomcat-jdbc排除 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> <exclusions> <exclusion> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-jdbc</artifactId> </exclusion> </exclusions> </dependency> <!-- mybatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> <!--mysql鏈接依賴 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>com.zaxxer</groupId> <artifactId>HikariCP-java7</artifactId> </dependency> <!-- 分頁(yè)插件pagehelper --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-autoconfigure</artifactId> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> </dependency> </dependencies> <build> <!-- 一定要聲明如下配置 --> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources> </build> </project>
五、創(chuàng)建springboot-entity
參照如上創(chuàng)建方式
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <artifactId>springboot-entity</artifactId> <packaging>jar</packaging> <name>springboot-entity</name> <parent> <groupId>com.button</groupId> <artifactId>springboot-parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> </project>
六、項(xiàng)目代碼
1、springboot-dao
①UserMapper.java
package com.button.project.dao;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import com.button.project.pojo.UserModel;
@Mapper
public interface UserMapper {
List<UserModel> getUser();
}②mapper/UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.button.project.dao.UserMapper">
<resultMap id="user" type="map">
<result column="id" property="id" javaType="integer"/>
<result column="name" property="name" javaType="String"/>
<result column="age" property="age" javaType="integer"/>
</resultMap>
<select id="getUser" resultType="com.button.project.pojo.UserModel">
SELECT
*
FROM
tb_user
</select>
</mapper>
③config/application-mybatis.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd ">
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
2、springboot-entity
UserModel.java
package com.button.project.pojo;
import java.io.Serializable;
public class UserModel implements Serializable{
private static final long serialVersionUID = 1L;
private Integer id;
private String name;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "UserModel [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
3、springboot-service
①UserService.java
package com.button.project.service;
import java.util.List;
import com.button.project.pojo.UserModel;
public interface UserService {
List<UserModel> getUser();
}
②UserServiceImpl.java
package com.button.project.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.button.project.dao.UserMapper;
import com.button.project.pojo.UserModel;
import com.button.project.service.UserService;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List<UserModel> getUser() {
return userMapper.getUser();
}
}
4、springboot-web
①UserController.java
package com.button.project.controller;
import java.util.List;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.button.project.pojo.UserModel;
@RequestMapping(value = "/user", produces = "application/json;charset=UTF-8")
public interface UserController {
@RequestMapping(value = "/getUser", method={RequestMethod.POST, RequestMethod.GET})
List<UserModel> getUser();
}
②UserControllerImpl.java
package com.button.project.controller.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import com.button.project.controller.UserController;
import com.button.project.pojo.UserModel;
import com.button.project.service.UserService;
@RestController
public class UserControllerImpl implements UserController {
@Autowired
private UserService userService;
@Override
public List<UserModel> getUser() {
return userService.getUser();
}
}
③SpringBootApplicationService
package com.button.project;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.ImportResource;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@ImportResource("classpath:config/application-mybatis.xml")
@ServletComponentScan
@EnableCaching
@EnableTransactionManagement
@SpringBootApplication
public class SpringBootApplicationService {
//項(xiàng)目啟動(dòng)入口
public static void main(String[] args) {
SpringApplication.run(SpringBootApplicationService.class, args);
}
}④InitConstantListener.java
package com.button.project.init;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@WebListener
public class InitConstantListener implements ServletContextListener {
private static final Logger logger = LoggerFactory.getLogger(InitConstantListener.class);
@Override
public void contextInitialized(ServletContextEvent sce) {
logger.info("初始化數(shù)據(jù).");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
logger.info("銷(xiāo)毀數(shù)據(jù).");
}
}⑤application.yml
server:
servlet:
context-path: /button
port: 8080
uri-encoding: utf-8
logging:
config: classpath:logback.xml
spring:
dataSource:
url: jdbc:mysql://*********:3306/button-pro?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&allowMultiQueries=true&useSSL=true
username: *****
password: ******
driver-class-name: com.mysql.jdbc.Driver
type: com.zaxxer.hikari.HikariDataSource
hikari:
minimum-idle: 5
maximum-pool-size: 15
idle-timeout: 30000
pool-name: DatebookHikariCP
max-lifetime: 1800000
connection-timeout: 30000
connection-test-query: 'SELECT 1'
mybatis:
# 加載mapper
mapper-locations: "classpath:mapper/*.xml"
mapper:
not-empty: false
identity: MYSQL
pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql
⑥logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="30 seconds">
<property name="LOG_HOME" value="/com/button/" />
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} -%msg%n</pattern>
</encoder>
</appender>
<appender name="FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_HOME}/springboot.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>
${LOG_HOME}/springboot.log.%d{yyyy-MM-dd}.%i.log
</FileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{50} -%msg%n</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</root>
</configuration>
七、完整項(xiàng)目結(jié)構(gòu)如下:

注:一個(gè)項(xiàng)目從開(kāi)發(fā),測(cè)試再到生產(chǎn),可能會(huì)使用到不同的配置信息,每次去修改application.yml文件中的信息就顯得特別麻煩,可以使用如下方式解決。
比如項(xiàng)目運(yùn)行環(huán)境開(kāi)發(fā)(dev),測(cè)試(test),生產(chǎn)(prod),我們可以在src/main/resources文件夾下創(chuàng)建如下四個(gè)文件:
application-dev.yml
server:
servlet:
context-path: /button-dev
port: 8080
uri-encoding: utf-8
application-test.yml
server:
servlet:
context-path: /button-test
port: 8081
uri-encoding: utf-8
application-prod.yml
server:
servlet:
context-path: /button-prod
port: 8082
uri-encoding: utf-8
這三個(gè)文件配置好項(xiàng)目不同環(huán)境的配置信息
application.yml文件配置如下:
spring:
profiles:
active: dev
解釋:active后可以配置dev、test、prod,不同的配置會(huì)去加載不同的配置信息,這樣就不用來(lái)回修改配置信息了。
更多springboot內(nèi)容,請(qǐng)查詢官方文檔
https://docs.spring.io/spring-boot/docs/1.5.8.RELEASE/reference/html/
源碼地址:https://gitee.com/superbutton/SpringBoot-Components/tree/develop/SpringBoot-Moudle
到此這篇關(guān)于Springboot分模塊項(xiàng)目搭建的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Springboot分模塊搭建內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
利用Java實(shí)現(xiàn)復(fù)制Excel工作表功能
這篇文章主要給大家介紹了關(guān)于如何利用Java實(shí)現(xiàn)復(fù)制Excel工作表功能的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用java具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
JDK-StringJoiner構(gòu)造及添加元素源碼分析
這篇文章主要為大家介紹了JDK-StringJoiner構(gòu)造及添加元素源碼分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
java實(shí)現(xiàn)把對(duì)象數(shù)組通過(guò)excel方式導(dǎo)出的功能
本文主要介紹了java實(shí)現(xiàn)把對(duì)象數(shù)組通過(guò)excel方式導(dǎo)出的功能的相關(guān)知識(shí)。具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-03-03
Java面向?qū)ο笾畣卫O(shè)計(jì)模式詳解
這篇文章主要介紹了Java面向?qū)ο笾畣卫O(shè)計(jì)模式詳解,所謂類(lèi)的單例設(shè)計(jì)模式,就是采取一定的方法保證在整個(gè)的軟件系統(tǒng)中,對(duì)某個(gè)類(lèi)只能存在一個(gè)對(duì)象實(shí)例,并且該類(lèi)只提供一個(gè)取得其對(duì)象實(shí)例的方法,需要的朋友可以參考下2024-01-01
java的SimpleDateFormat線程不安全的幾種解決方案
但我們知道SimpleDateFormat是線程不安全的,處理時(shí)要特別小心,要加鎖或者不能定義為static,要在方法內(nèi)new出對(duì)象,再進(jìn)行格式化,本文就介紹了幾種方法,感興趣的可以了解一下2021-08-08
java實(shí)現(xiàn)文件分片上傳并且斷點(diǎn)續(xù)傳的示例代碼
本文主要介紹了java實(shí)現(xiàn)文件分片上傳并且斷點(diǎn)續(xù)傳的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05
微信公眾號(hào)支付(二)實(shí)現(xiàn)統(tǒng)一下單接口
本篇文章主要給大家介紹調(diào)用微信公眾支付的統(tǒng)一下單API,通過(guò)參數(shù)封裝為xml格式并發(fā)送到微信給的接口地址就可以獲得返回內(nèi)容,需要的朋友可以參考下本文2015-09-09
Spring 框架實(shí)現(xiàn)賬戶轉(zhuǎn)賬功能(推薦)
通過(guò)本文的介紹,我們了解了如何使用Spring框架實(shí)現(xiàn)一個(gè)簡(jiǎn)單的賬戶轉(zhuǎn)賬功能,主要使用了 Spring 的依賴注入、和事務(wù)管理功能,保證了轉(zhuǎn)賬操作的原子性和數(shù)據(jù)的一致性,感興趣的朋友跟隨小編一起看看吧2025-07-07

