Build spring boot Multi module project (source code attached)

  • 2020-11-03 22:12:23
  • OfStack

This paper introduces Maven's multi-module project to build spring boot, which is Shared with everyone. The details are as follows:

Note: All projects are created in idea

idea Created the maven project

1-1: Delete src,target directory and keep only pom.xml 1-2: The root directory pom. xml can be inherited by the quilt module, so the project is just demo and does not consider too many performance issues, so many dependencies will be made. All written at the root level 'pom. xml', submodules can be used only by inheritance. 1-3: The root level ES22en. xml file is in Appendix 1 1-4: Dependent module mybatis ES25en-ES26en related modules

2. Create submodules (module)

2-1: file > new > module input model 2-2: file > new > module input dao 2-3: file > new > module input service 2-4: file > new > module input webapi

3. Modify the configuration of sub-module ES55en.ES56en


<?xml version="1.0" encoding="UTF-8"?>
<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>parent</artifactId>
    <groupId>com.luyh.projectv1</groupId>
    <version>1.0-SNAPSHOT</version>
    <relativePath>../pom.xml</relativePath>
  </parent>
  <modelVersion>4.0.0</modelVersion>

  <artifactId>projectv1-model</artifactId>
</project>

Note: < font color="red" > < relativePath > ../pom.xml < /relativePath > < /font > This paragraph must be added to inherit from the parent module

At this point, the infrastructure of the project has been set up, and now we can write the code. Oh, wait a minute. Let me first introduce the responsibilities of each sub-ES76en

4. "Job Responsibilities" of sub-modules in the project

The model module holds all of the entity classes dao this module holds the concrete implementation of data interaction for service to call service This module holds the business code implementation for the API layer to call webapi this module can also be removed from the project and the webapi layer is put in to write demo

5.model layer entity class preparation

Build a package name com. luyh. projectv1. model Member.java my git,git address at the bottom

6.dao layer Database operation layer

Establish com. luyh. projectv1. dao. config, inside the package only 2 let spring boot automatic loading configuration configuration java classes MemberMapper. java see the code MemberMapper.xml was established under resources/mybatis Establish IMember java Establish ES119en. java to implement Imember interface Establish the resources/ ES123en.properties file to configure the database connection

service writes the business logic

Establish com. luyh. projectv1 service package Set up the ES132en.ES133en interface Establish the ES134en.java implementation class MemberService. java class automatically infuses DaoMember and calls its methods to get the data

webapi to obtain json data

Set up ES144en.ES145en to launch the application Establish com. luyh. projectv1. webapi. controller. MemberController. Write a rest java Controller style Start the

9.sql file please import mysql data sql file by yourself

Here is the project address, click to download

Appendix 1


<?xml version="1.0" encoding="UTF-8"?>
<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>

  <groupId>com.luyh.projectv1</groupId>
  <artifactId>parent</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.3.RELEASE</version>
  </parent>
  <modules>

    <module>model</module>
    <module>dao</module>
    <module>service</module>
    <module>webapi</module>
  </modules>

  <!-- Declare dependency -->
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>

    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>1.2.2</version>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.2.8</version>
    </dependency>

    <dependency>
      <groupId>org.apache.tomcat</groupId>
      <artifactId>tomcat-jdbc</artifactId>
    </dependency>

    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
    </dependency>
  </dependencies>

  <!-- Set up the maven warehouse -->

  <repositories>
    <repository>
      <id>spring-releases</id>
      <url>https://repo.spring.io/libs-release</url>
    </repository>
  </repositories>
  <pluginRepositories>
    <pluginRepository>
      <id>spring-releases</id>
      <url>https://repo.spring.io/libs-release</url>
    </pluginRepository>
  </pluginRepositories>


</project>


Related articles: