浅谈springboot多模块(modules)开发
为何模块开发
先举个栗⼦,同⼀张数据表,可能要在多个项⽬中或功能中使⽤,所以就有可能在每个模块都要搞⼀个mybatis去配置。如果⼀开始规定说这张表⼀定不可以改字段属性,那么没⽑病。但是事实上,⼀张表从项⽬开始到结束,不知道被改了多少遍,所以,你有可能在多个项⽬中去改mybatis改到吐⾎!
在举⼀个栗⼦,⼀个web服务⾥包含了多个功能模块,⽐如其中⼀个功能可能会消耗⼤量资源和时间,当⽤户调⽤这个功能的时候,可能会影响到其他功能的正常使⽤,这个时候,如果把各个功能模块分出来单独部署,然后通过http请求去调⽤,⾄于性能和响应速度,再单独去优化,将会⾮常爽!这也有利于将来的
分布式集
根据当前的业务需求,我需要重构现有的web功能,多模块化,然后单独部署,基本架构⽰意图如下
怎样分模块
注意:下⾯配置的步骤是基于IntelliJ IDEA 2016.3.4(64),不保证eclipse能成功。如果你还在使⽤eclipse,建议你删掉它,使⽤idea吧
1、创建maven主项⽬例如,springbootmodules,并删掉src⽂件
2、右键项⽬分别创建三个module,dao,service1,service2
3、将之前项⽬⽤到的依赖写在主项⽬的pom⾥,这⾥要注意
4、dao层主要提供实体类,CURD接⼝和xml映射⽂件
5、⼀定要在service1和service2配置数据库的相关信息,并添加spring的相关配置
6、编写接⼝测试
相关代码
⽗项⽬pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.boot.lean</groupId>
<artifactId>springbootquick</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>dao</module>
<module>service1</module>
<module>service2</module>
</modules>
<packaging>pom</packaging>
<name>springbootquick</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>    <shadedClassifier>bin</shadedClassifier>
<java.version>1.8</java.version>
<mybatis-spring-boot>1.2.0</mybatis-spring-boot>
<mysql-connector>5.1.39</mysql-connector>
</properties>
<dependencies>
<!-- Spring Boot Web 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Boot Test 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Spring Boot Mybatis 依赖 -->
<dependency>
<groupId&batis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis-spring-boot}</version>
</dependency>
<!-- MySQL 连接驱动依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector}</version>
</dependency>
<!-- Junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.apachemons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<skipTests>true</skipTests>  <!--默认关掉单元测试 -->
</configuration>
</plugin>
<plugin>
<groupId&ator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
dao模块的pom(⾥⾯配置了mybatis的逆向功能插件)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd">  <parent>
<artifactId>springbootquick</artifactId>
<groupId>com.boot.lean</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dao</artifactId>
<packaging>jar</packaging>
<build>
<!-- ⼀定要声明如下配置-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
springboot结构</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
</project>
service1和service2的pom⼀样
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd">  <parent>
<artifactId>springbootquick</artifactId>
<groupId>com.boot.lean</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>service1</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.boot.lean</groupId>
<artifactId>dao</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
需要注意的是,service模块⾥我⽤的是注解配置,如图所⽰
结构⽰意图
注意配置⽂件⾥的端⼝号
打包测试
在⽗项⽬下执⾏maven命令
mvn package
service1和service2⽬录下分别会产⽣target⽂件,⾥⾯包含可执⾏jar包,分别执⾏
java -jar service1-1.0-SNAPSHOT
java -jar service2-1.0-SNAPSHOT
如果⼀切顺利的话,你可以得出下⾯的操作结果
注意端⼝号哦
有什么问题,⾃⾏解决,然后你会发现,跨过这个坑,还有⽆数个坑在等你~
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。