SpringBoot多模块搭建

SpringBoot多模块搭建

  • SpringBoot项目创建图解
  • 子模块创建图解
  • demo父项目
  • dao子模块
  • biz子模块
  • web子模块
  • 运行

SpringBoot项目创建图解

项目名称为:demo
点击next后根据所需自行填写相应信息,这里不做过多介绍
SpringBoot多模块搭建_第1张图片
删除多余的文件
SpringBoot多模块搭建_第2张图片

子模块创建图解

SpringBoot多模块搭建_第3张图片SpringBoot多模块搭建_第4张图片SpringBoot多模块搭建_第5张图片SpringBoot多模块搭建_第6张图片直接点击下方Finish,完成子模块创建
依次创建dao/biz/web子模块
SpringBoot多模块搭建_第7张图片

demo父项目

demo父项目主要用于版本控制
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>
    <packaging>pompackaging>
    <modules>
        <module>demo-daomodule>
        <module>demo-bizmodule>
        <module>demo-webmodule>
    modules>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.2.2.RELEASEversion>
        <relativePath/> 
    parent>
    <groupId>com.examplegroupId>
    <artifactId>demoartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>demoname>
    <description>Demo project for Spring Bootdescription>

    <properties>
        <java.version>1.8java.version>
        <demo.version>0.0.1-SNAPSHOTdemo.version>
        <springboot.version>2.2.2.RELEASEspringboot.version>
        <mybatis.version>2.0.0mybatis.version>
        <mysql-connector.version>5.1.32mysql-connector.version>
        <lombok.version>1.16.18lombok.version>
    properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.examplegroupId>
                <artifactId>demo-daoartifactId>
                <version>${demo.version}version>
            dependency>
            <dependency>
                <groupId>com.examplegroupId>
                <artifactId>demo-bizartifactId>
                <version>${demo.version}version>
            dependency>
            <dependency>
                <groupId>com.examplegroupId>
                <artifactId>demo-webartifactId>
                <version>${demo.version}version>
            dependency>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-testartifactId>
            dependency>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
                <version>${springboot.version}version>
            dependency>
            <dependency>
                <groupId>org.mybatis.spring.bootgroupId>
                <artifactId>mybatis-spring-boot-starterartifactId>
                <version>${mybatis.version}version>
            dependency>
            <dependency>
                <groupId>mysqlgroupId>
                <artifactId>mysql-connector-javaartifactId>
                <version>${mysql-connector.version}version>
            dependency>
            <dependency>
                <groupId>org.projectlombokgroupId>
                <artifactId>lombokartifactId>
                <version>${lombok.version}version>
            dependency>
        dependencies>
    dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>

project>

dao子模块

SpringBoot多模块搭建_第8张图片
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">
    <parent>
        <artifactId>demoartifactId>
        <groupId>com.examplegroupId>
        <version>0.0.1-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>demo-daoartifactId>

    <dependencies>
        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
        dependency>
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
        dependency>
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
        dependency>
    dependencies>

project>

这里properties命名方式是为了方便多配置文件管理
application-dao.properties

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/goods?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=root
mybatis.mapper-locations=classpath:mybatis/*.xml
mybatis.type-aliases-package=com.demo.dao.entity

可通过Mybatis逆向工程生成相应的实体,接口,配置文件这里不做介绍
详情请参考–>http://mybatis.org/generator/quickstart.html

biz子模块

SpringBoot多模块搭建_第9张图片
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">
    <parent>
        <artifactId>demoartifactId>
        <groupId>com.examplegroupId>
        <version>0.0.1-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>demo-bizartifactId>

    <dependencies>
    	
        <dependency>
            <groupId>com.examplegroupId>
            <artifactId>demo-daoartifactId>
        dependency>
    dependencies>

project>

DemoService 接口

package com.demo.biz.service;

public interface DemoService {
    String test();
}

DemoService 接口实现类

package com.demo.biz.service.impl;

import com.demo.biz.service.DemoService;
import com.demo.dao.entity.Good;
import com.demo.dao.mapper.GoodMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class DemoServiceImpl implements DemoService {

    @Autowired
    private GoodMapper goodMapper;

    @Override
    public String test() {
        Good good = goodMapper.selectByPrimaryKey("1");
        return good.toString();
    }
}

web子模块

SpringBoot多模块搭建_第10张图片
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">
    <parent>
        <artifactId>demoartifactId>
        <groupId>com.examplegroupId>
        <version>0.0.1-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>demo-webartifactId>

    <dependencies>
    	
        <dependency>
            <groupId>com.examplegroupId>
            <artifactId>demo-bizartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
    dependencies>

project>

application.properties配置文件
使用spring.profiles.active区分不同环境下的配置文件(多文件配置)

spring.profiles.active=dao

server.port=80

启动类:DemoWebApplication.java
扫描com.demo路径
@SpringBootApplication(scanBasePackages = “com.demo”)
扫描mapper接口
@MapperScan(“com.demo.dao.mapper”)

package com.demo.web;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication(scanBasePackages = "com.demo")
@MapperScan("com.demo.dao.mapper")
public class DemoWebApplication {

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

}

WEB访问

package com.demo.web.controll;

import com.demo.biz.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("demo")
public class DemoController {

    @Autowired
    private DemoService demoService;

    @GetMapping("test")
    public String test() {
        return demoService.test();
    }

}

运行

SpringBoot多模块搭建_第11张图片SpringBoot多模块搭建_第12张图片

你可能感兴趣的:(实用技巧)