[springboot advanced] Construction based on the starter project (2) Build the starter project-mysql

Table of contents

1. Create the mysql-spring-boot-starter project

2. Add pom file dependencies

3. Build configuration

1. mybatis-plus paging configuration MybatisPlusConfig

2. mybatis-plus code generator CodeGenerator

Fourth, load the automatic configuration

5. Packing

6. Use


This series explains how the project is built, mainly using the combination of the parent project parent and the custom starter. The project uses the latest springboot3 and jdk19. For the code repository of this series, see   the starter directory of the gitee repository .

In this article, we start to learn how to create our own starter, realize the encapsulation and automatic configuration of some common modules, simulate the starter mode of spirngboot, and see how to build the project as a mysql starter.

1. Create the mysql-spring-boot-starter project

Generally, the official starter is spring-boot-starter-{module name}, so when we customize it here, it is different from the official command and puts the module name in front.

We still create it as a springboot project, as shown below.

Select the latest version 3.0.0, the following dependencies do not need to be checked, we will add them later.

2. Add pom file dependencies

<?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>
	<parent>
		<groupId>org.liurb.springboot.scaffold</groupId>
		<artifactId>backend-parent</artifactId>
		<version>1.0.0</version>
		<relativePath />
	</parent>

	<artifactId>mysql-spring-boot-starter</artifactId>
	<version>1.0.0</version>
	<name>mysql-spring-boot-starter</name>
	<description>mysql-spring-boot-starter</description>

	<dependencies>

		<dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>mybatis-plus-boot-starter</artifactId>
		</dependency>

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

		<dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>mybatis-plus-generator</artifactId>
		</dependency>

		<dependency>
			<groupId>com.github.yulichang</groupId>
			<artifactId>mybatis-plus-join</artifactId>
		</dependency>

		<dependency>
			<groupId>com.mysql</groupId>
			<artifactId>mysql-connector-j</artifactId>
			<scope>runtime</scope>
		</dependency>

		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>true</optional>
		</dependency>

		<!--表示两个项目之间依赖不传递;不设置optional或者optional是false,表示传递依赖-->
		<!--例如:project1依赖a.jar(optional=true),project2依赖project1,则project2不依赖a.jar-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-configuration-processor</artifactId>
			<optional>true</optional>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-autoconfigure</artifactId>
			<optional>true</optional>
		</dependency>

	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<classifier>exec</classifier>
				</configuration>
			</plugin>
        </plugins>
	</build>

	<repositories>
		<!-- 下载mybatis-plus SNAPSHOT 版本所需仓库 -->
		<repository>
			<id>ossrh</id>
			<name>OSS Snapshot repository</name>
			<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
			<releases>
				<enabled>false</enabled>
			</releases>
			<snapshots>
				<enabled>true</enabled>
			</snapshots>
		</repository>

	</repositories>

</project>

Dependency description:

1) mybatis-plus-boot-starter: When using mybatis-plus, you need to pay attention to the version of springboot3. For details, please refer to  Solving the sqlSession exception reported by springboot3 integrated mybatis-plus

2) mybatis-plus-generator: code generator

3) mybatis-plus-join: Multi-table join table plug-in, you can write join table sql requirements through api

4) mysql-connector-j: Mysql link driver, note that it has been migrated to a new package since version 8.0.31

Warehouse description:

OSS Snapshot repository solves the problem of spirngboot3 in order to introduce the snapshot version of mybatis-plus

3. Build configuration

The built starter directory and code are as shown below.

1. mybatis-plus paging configuration MybatisPlusConfig

@AutoConfiguration
@ConditionalOnClass(value = {PaginationInnerInterceptor.class})
public class MybatisPlusConfig {

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
        mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return mybatisPlusInterceptor;
    }

}

2. mybatis-plus code generator CodeGenerator

The project uses version 3.5.3. Note that each version may be written differently, and the general official website has instructions.

It is also easy to use, just write a main method and pass in the corresponding value, use the builder mode

    public static void main(String[] args) {
        CodeGenerator codeGenerator = CodeGenerator.builder().jdbcUrl(jdbcUrl).jdbcUserName(jdbcUserName).jdbcPassword(jdbcPassword)
                .daoOutputDirRelate(daoOutputDirRelate).mapperOutputDirRelate(mapperOutputDirRelate).author(author)
                .packageName(packageName).moduleName(moduleName).tableNames(tableNames).tablePrefix(tablePrefix)
                .build();

        codeGenerator.generator();
    }

Fourth, load the automatic configuration

From springboot 2.7, the spring.factories method has been marked as expired, so it has been completely removed from springboot3. So we need to create the org.springframework.boot.autoconfigure.AutoConfiguration.imports file in the resources/META-INF directory.

Define the package path of the MybatisPlusConfig class in our conf directory here. If there are multiple cases, configure one per line.

5. Packing

At this time, execute mvn package & mvn install to install the starter into the local warehouse.

6. Use

You can see the springboot-advance-demo project in the gitee warehouse.

Guess you like

Origin blog.csdn.net/lrb0677/article/details/128275461