java web 之springboot教程之三十二----多模块项目

比起传统复杂的单体工程,使用Maven的多模块配置,可以帮助项目划分模块,鼓励重用,防止POM变得过于庞大,方便某个模块的构建,而不用每次都构建整个项目,并且使得针对某个模块的特殊控制更为方便。

一,创建父工程

使用 Spring Initializr 来快速创建好一个Maven工程。然后删除无关的文件,只需保留pom.xml 文件。

在 pom.xml 里面声明该父工程包含的子模块。



   4.0.0
   
      org.springframework.boot
      spring-boot-starter-parent
      2.1.4.RELEASE
       
   
   com.example
   multi-module
   0.0.1-SNAPSHOT
   multi-module
    pom
    SpringBoot 多模块构建示例

   
      1.8
   

   
   
      mm-web
      mm-service
      mm-repo
      mm-entity
   

   
   
      
         
            com.example
            mm-web
            0.0.1-SNAPSHOT
         
         
            com.example
            mm-service
            0.0.1-SNAPSHOT
         
         
            com.example
            mm-repo
            0.0.1-SNAPSHOT
         
         
            com.example
            mm-entity
            0.0.1-SNAPSHOT
         
      
   


注意这里的pom

二,创建各子模块

对着父工程右键 - New - Module - > 输入 mm-web

也使用 Spring Initializr 来快速创建好一个Maven工程。然后删除无关的文件,保留pom.xml 文件和src。

其他几个子模块类似,mm-web的pom.xml如下:



   4.0.0
   
      com.example
      multi-module
      0.0.1-SNAPSHOT
       
   
   com.example
   mm-web
   0.0.1-SNAPSHOT
   mm-web
    jar
   Demo project for Spring Boot

   
      1.8
   

   

      
         com.example
         mm-service
      
      
         com.example
         mm-entity
      

      
         org.springframework.boot
         spring-boot-starter
      

        
            org.springframework.boot
            spring-boot-starter-web
        

      
         org.springframework.boot
         spring-boot-starter-test
         test
      
   

   
      
         
            org.springframework.boot
            spring-boot-maven-plugin
         
      
   

注意这里的jar。其他几个子模块类似。

再看下启动类:

package com.example.mmweb;

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
@ComponentScan({"com.example.mmservice"})
@EntityScan("com.example.mmentity")
@EnableJpaRepositories("com.example.mmrepo")
public class MmWebApplication {

   public static void main(String[] args) {
      SpringApplication.run(MmWebApplication.class, args);
   }

}

这里要加上几个扫包注解。不然会出错。

application.yml配置文件:

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/socks?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF-8&useSSL=false
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver

sql文件:

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for t_user
-- ----------------------------
DROP TABLE IF EXISTS `t_user`;
CREATE TABLE `t_user` (
  `userid` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(255) DEFAULT NULL,
  `password` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`userid`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of t_user
-- ----------------------------
INSERT INTO `t_user` VALUES ('1', 'admin', 'admin');
INSERT INTO `t_user` VALUES ('2', 'jack', 'buzhidao');

运行启动类,访问localhost:8080可以看到结果。当然要把controller加进去。

三,运维部署

启动模块是 mm-web , 只需在它的pom.xml 添加打包插件(spring-boot-maven-plugin):


    
        
            
                
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

在IDE打开Maven插件,然后在聚合父工程multi-module中点击 clean ,然后点击 package 进行打包。如图:

java web 之springboot教程之三十二----多模块项目_第1张图片

最后得到jar包,用命令行启动即可。 

你可能感兴趣的:(JavaWeb)