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)一管理依賴(lài)的版本號(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鏈接依賴(lài) --> <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)依賴(lài) --> <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>
解釋?zhuān)?br />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依賴(lài) --> <!-- 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鏈接依賴(lài) --> <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
解釋?zhuān)篴ctive后可以配置dev、test、prod,不同的配置會(huì)去加載不同的配置信息,這樣就不用來(lái)回修改配置信息了。
更多springboot內(nèi)容,請(qǐng)查詢(xún)官方文檔
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)砸金蛋抽獎(jiǎng)功能
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)砸金蛋抽獎(jiǎng)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11使用java采集京東商城區(qū)劃數(shù)據(jù)示例
這篇文章主要介紹了java采集京東的全國(guó)區(qū)劃數(shù)據(jù)示例,保存成json形式,如想轉(zhuǎn)換到數(shù)據(jù)庫(kù)只需反序列化為對(duì)象保存到數(shù)據(jù)庫(kù)即可2014-03-03日志模塊自定義@SkipLogAspect注解跳過(guò)切面的操作方法
文章介紹了一個(gè)自定義注解@SkipLogAspect,用于在日志模塊中跳過(guò)特定方法的日志切面,這個(gè)注解可以用于需要避免大對(duì)象轉(zhuǎn)換為JSON時(shí)導(dǎo)致的OOM問(wèn)題,文章還提供了注解的實(shí)現(xiàn)代碼以及一個(gè)測(cè)試示例,展示了如何在控制器中使用該注解來(lái)跳過(guò)日志切面,感興趣的朋友一起看看吧2025-02-02mybatis新手快速入門(mén)以及一些錯(cuò)誤匯總
這篇文章主要給大家介紹了關(guān)于mybatis新手快速入門(mén)以及一些錯(cuò)誤的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03Java中ArrayList實(shí)現(xiàn)原理及基本方法
這篇文章主要介紹了Java中ArrayList實(shí)現(xiàn)原理及基本方法,ArrayList是開(kāi)發(fā)中非常常用的數(shù)據(jù)存儲(chǔ)容器之一,其底層是數(shù)組實(shí)現(xiàn)的,我們可以在集合中存儲(chǔ)任意類(lèi)型的數(shù)據(jù),ArrayList是線(xiàn)程不安全的,擅長(zhǎng)隨機(jī)訪(fǎng)問(wèn)元素,插入和刪除較慢,需要的朋友可以參考下2023-08-08淺談java中為什么重寫(xiě)equals后需要重寫(xiě)hashCode
今天帶各位學(xué)習(xí)一下java中為什么重寫(xiě)equals后需要重寫(xiě)hashCode,文中有非常詳細(xì)的圖文介紹及代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴們有很好的幫助,需要的朋友可以參考下2021-05-05詳解java中String、StringBuilder、StringBuffer的區(qū)別
這篇文章主要介紹了java中String、StringBuilder、StringBuffer的區(qū)別,文中講解的很清晰,有對(duì)于這方面不太懂的同學(xué)可以研究下2021-02-02Java中Runnable和Callable的區(qū)別和聯(lián)系及使用場(chǎng)景
Java多線(xiàn)程有兩個(gè)重要的接口,Runnable和Callable,分別提供一個(gè)run方法和call方法,二者是有較大差異的,本文介紹Java中Runnable和Callable的區(qū)別和聯(lián)系及使用場(chǎng)景,感興趣的朋友一起看看吧2025-03-03