1. Create the parent project boot-parent
Create a SpringBoot parent project, keep only pom.xml, and import all dependencies used in the project
<dependencies>
<!-- Spring&SpringMVC -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- mybatis-plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
<!-- mysql-connector -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.32</version>
</dependency>
</dependencies>
2. Create subprojects
2.1. Create the controller subproject gaofeng-admin
Create an admin submodule to add configuration files and implement the role of the Controller layer
2.1.1、application.yml
Manage the configuration items of the entire project in the configuration file
server:
port: 8070
spring:
datasource:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.01:3306/t_mybatis?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true
username: root
password: Aa123123.
mybatis-plus:
type-aliases-package: demo.entity
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
global-config:
db-config:
table-prefix: t_
id-type: auto
mapper-locations: classpath:mappers/*.xml
2.2. Create system subproject gaofeng-system
Create a system subproject to implement the development of the entity layer, DAO layer, and Service layer
2.2.1. Entity layer User
package demo.entity;
import lombok.Data;
@Data
public class User {
private String userName;
private String passwd;
}
2.2.2, DAO layer UserMapper
package demo.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import demo.entity.User;
import org.springframework.stereotype.Repository;
@Repository
public interface UserMapper extends BaseMapper<User> {
}
2.2.3. Service layer UserService
package demo.service;
import demo.entity.User;
import java.util.List;
public interface UserService {
List<User> findAll();
}
package demo.service.imp;
import demo.entity.User;
import demo.mapper.UserMapper;
import demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List<User> findAll() {
return userMapper.selectList(null);
}
}
2.3. Add the dependencies of the system subproject to the controller subproject
Since the controller subproject implements the functions of the Controller layer and needs to rely on the Service layer functions of the system subproject, it is necessary to add the system subproject through the dependency
tag in the controller subproject. Project dependencies
2.3.1. Add dependencies
gaofeng-admin/pom.xml
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>gaofeng-system</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
2.3.2. Complete the functions of the Controller layer
package demo.controller;
import demo.entity.User;
import demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/list")
public List<User> findAll(){
return userService.findAll();
}
}
3. Compile and package
Step 1: Delete the buid element of the parent project pom file
Step 2: Add the buid element to the subproject pom file
Since there is only one startup class in the entire project, the current project design places the startup class in the gaofeng-admin subproject, so you only need to add packaging information to the gaofeng-admin subproject, mainly setting the path of the startup class.
gaofeng-admin\pom.xml
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>
<!--启动类全限定名-->
demo.Demo
</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Step 3: Clear all compiled files in the project
Use the mavnen => Lifecycle => clean command in the parent project to clear all compiled files (target folder and all contents in it) in all projects (parent project and sub-project)
Step 4: Compile the project
Use the mavnen => Lifecycle => install command in the parent project to compile all projects (parent projects and sub-projects), that is, generate the target folders of all sub-projects and all the contents in them.
Step 5: Create a runnable jar package
Since there is only one startup class in the entire project, the current project design places the startup class in the gaofeng-admin subproject, so you only need to package the gaofeng-admin subproject
Use the mavnen => Lifecycle => package command in the gaofeng-admin subproject to package
After the packaging is successful, you can see the packaged jar file in the sub-project gaofeng-admin\target folder.
Use the command in the command window (cmd) to run the jar file
Order:
cd /d D:\workspace\idea\boot-parent\gaofeng-admin\target
java -jar gaofeng-admin-0.0.1-SNAPSHOT.jar
running result:
3. Appendix
4.1. Project source code
4.2. Complete pom.xml of parent project
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
<modules>
<module>gaofeng-admin</module>
<module>gaofeng-system</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.9</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>boot-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>boot-parent</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- mybatis-plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
<!-- mysql-connector -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.32</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
4.3. Subproject gaofeng-admin complete pom.xml
<?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>boot-parent</artifactId>
<groupId>com.example</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>gaofeng-admin</artifactId>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>gaofeng-system</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>
demo.Demo
</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>