Upgrade Mybatis to Mybatis-Plus under Spring Boot

Introduction

Mybatis-Plus is an enhanced tool based on MyBatis, which simplifies the development process of using MyBatis in Spring Boot projects. By introducing Mybatis-Plus, we can reduce a lot of boilerplate code and provide more convenient functions.

This article will introduce you to how to upgrade the MyBatis framework in an existing Spring Boot project to Mybatis-Plus.

step

Step 1: Add dependencies

pom.xmlFirst, add the Mybatis-Plus dependency to your file:

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>最新版本号</version>
</dependency>

Please make sure to 最新版本号replace it with the latest stable version number of Mybatis-Plus.

Step 2: Configure the data source

Mybatis-Plus needs to be integrated with data sources. Open the application.properties(or application.yml) file and add the following content to configure the data source information:

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/your_database_name
spring.datasource.username=your_username
spring.datasource.password=your_password

Make sure to replace your_database_name, your_usernameand your_passwordwith your actual database name, username, and password.

Step 3: Configure Mybatis-Plus

In Spring Boot, the configuration of Mybatis-Plus is very simple. Just add the following in application.properties(or ):application.yml

# MyBatis-Plus配置
mybatis-plus.config-location=classpath:mybatis/mybatis-config.xml

Here mybatis-config.xmlis a custom Mybatis configuration file, which we will create later.

Step 4: Create Mybatis-Plus configuration file

resourcesCreate a mybatisfolder named under the directory and a file named inside it mybatis-config.xml. Add the following content to the file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
        <!-- 开启驼峰命名自动映射 -->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
</configuration>

The configuration here is used to enable the camel case naming automatic mapping function of Mybatis-Plus and map the underlined fields in the database to the camel case naming attributes of the Java class.

Step 5: Reconstruct the Mapper interface

Now, we need to modify the original Mapper interface to adapt to the specifications of Mybatis-Plus. Mybatis-Plus provides a set of annotation-based Mapper interfaces and SQL methods, which can avoid manually writing Mapper XML files.

For example, the original Mapper interface might look like this:

public interface UserMapper {
    List<User> findAll();
    void save(User user);
    // ...
}

Modify it to:

public interface UserMapper extends BaseMapper<User> {
    // 此处无需再定义findAll和save方法
    // ...
}

Using BaseMapperit as a parent interface will automatically provide a series of CRUD operation methods.

Step 6: Complete the configuration

Finally, restart your Spring Boot application. Mybatis-Plus will automatically scan and load all Mapper interfaces.

Now, you have successfully upgraded the MyBatis framework in the original Spring Boot project to Mybatis-Plus. By introducing Mybatis-Plus, you can enjoy a more convenient and efficient development experience.

Hope this article helps you!

Guess you like

Origin blog.csdn.net/weixin_52721608/article/details/132919858