SoFunction
Updated on 2025-04-12

Various usages and example analysis of static tool Db in MyBatis-Plus

Various usages and examples of static tool Db in MyBatis-Plus

introduce:

  • MyBatis-Plus is a powerful ORM framework that provides many practical features.
  • Among them, static tool class Db is a convenient and flexible tool that can simplify interaction and operation with the database.

Case background

Suppose we have a student management system that contains the Student class as a student entity.

In this system, we need to perform database operations such as adding, deleting, modifying and searching for students.

Use static tool Db for database operations

Insert data

First, before using the static tool Db to perform database operations, we need to introduce relevant dependencies.

For example, in the maven project, we can add the following dependencies:

<dependency>
    <groupId></groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>Latest version number</version>
</dependency>

Next, we can insert data through the insert method of the static tool Db.

Here is an example:

import ;
import ;
import ;

public class StudentService {

    public void insertStudent(Student student) {
        int rows = ("INSERT INTO student (name, age) VALUES (?, ?)", (), ());
        if ((rows)) {
            ("Insert successful");
        } else {
            ("Insert failed");
        }
    }
}

In the example above, we use the static tool Db to call the insert method and pass in the SQL statement and the corresponding parameters. Among them, placeholders?Indicates the parameters to be filled.

Update data

Similarly, we can use the update method of the static tool Db to update the data.

Here is an example:

public class StudentService {

    public void updateStudent(Student student) {
        int rows = ("UPDATE student SET name=?, age=? WHERE id=?", (), (), ());
        if((rows)) {
            ("Update successful");
        } else {
            ("Update failed");
        }
    }
}

In the above example, we use the update method of the static tool Db to perform the update operation. We provide the SQL statement to be executed and the corresponding parameter values.

Query data

It is also very convenient to use the static tool Db to query.

Here is an example:

public class StudentService {

    public List<Student> getStudentsByAge(int age) {
        return ("SELECT * FROM student WHERE age=?", , age);
    }
}

In the above example, we use the queryList method of the static tool Db to perform query operations, specifying the SQL statement to be executed, the type of the result returned (here is the Student class), and the corresponding parameters.

Delete data

Finally, we can use the delete method of the static tool Db to delete the data.

Here is an example:

public class StudentService {

    public void deleteStudent(long id) {
        int rows = ("DELETE FROM student WHERE id=?", id);
        if((rows)) {
            ("Delete successful");
        } else {
            ("Delete failed");
        }
    }
}

In the above example, we use the delete method of the static tool Db to perform the delete operation and pass in the corresponding SQL statements and parameters.

Summarize

This article explains in detail the various uses of the static tool Db in MyBatis-Plus, and demonstrates and explains it in combination with specific cases. By using the Db tool, we can easily perform common operations such as data insertion, update, query and delete.

The static tool Db provides a way to simplify database operations, making the code more streamlined and easy to understand. I hope this article will be helpful to you in understanding and applying Db tools and can play a role in actual projects.

Wish you success using MyBatis-Plus! If you need to know more about the functions and usage of the Db tool, please refer to the official MyBatis-Plus documentation.

The above is personal experience. I hope you can give you a reference and I hope you can support me more.