On the first day of August, learn MybatisPlus. Welcome to learn 😁

Code Mantis shrimp is a sand sculpture and interesting young boy, like most friends like listening to music, games, of course, in addition to the interest of writing, Emm… The day is still very long, let us walk together 🌈

Welcome friends to pay attention to my public number: JavaCodes, although the name with Java, but the scope of more than Java field oh 😁, look forward to your attention ❤

The original link⭐MybatisPlus learning notes ⭐ (1) Environment setup and entry HelloWorld


1, MybatisPlus overview

MyBatis-Plus (MP for short) is a MyBatis enhancement tool, on the basis of MyBatis only do enhancement do not change, in order to simplify the development, improve efficiency and born.

Overall structure of MybatisPlus


2. MybatisPlus feature

  • Non-invasive: only enhanced but not changed, introducing it will not affect the existing engineering, as smooth as silk
  • Small loss: the basic CURD will be automatically injected when started, the performance is basically loss-free, and the operation is directly object-oriented
  • Powerful CRUD operations: The built-in universal Mapper and universal Service can realize most CRUD operations of a single table with only a few configurations. There is a powerful conditional constructor to meet various requirements
  • Support for calls in the form of Lambda: by using Lambda expressions, it is convenient to write all kinds of query conditions, without worrying about field error
  • Support for automatic generation of primary keys: support up to four primary key policies (including distributed unique ID generator – Sequence), configurable, perfect to solve the primary key problem
  • Support for ActiveRecord mode: Support for ActiveRecord mode invocation, entity classes only need to inherit Model class to perform powerful CRUD operations
  • Support for custom global generic operations: Support for global generic method injection (Write once, use Anywhere)
  • Built-in code generator: Use code or Maven plug-in to quickly generate Mapper, Model, Service, Controller layer code, support template engine, and more custom configuration for you to use
  • Built-in pagination plugin: Based on MyBatis physical pagination, developers do not need to care about the specific operation, after configuring the plugin, write pagination is equivalent to normal List query
  • Paging plugin supports multiple databases: support MySQL, MariaDB, Oracle, DB2, H2, HSQL, SQLite, Postgre, SQLServer and other databases
  • Built-in performance analysis plug-in: output Sql statements and their execution time. It is recommended to enable this function when developing tests to quickly catch slow queries
  • Built-in global interception plug-in: Provides intelligent analysis and block of delete and UPDATE operations in all tables. You can also customize interception rules to prevent misoperations

3. Basic environment construction

1. Set up the database environment

- create the library
CREATE DATABASE mp;
- use the library
USE mp;
- create a table
CREATE TABLE employee(
   id INT(11) PRIMARY KEY AUTO_INCREMENT,
   last_name VARCHAR(50),
   email VARCHAR(50),
   gender CHAR(1),
   age INT
);
INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Tom'.'[email protected]'.1.22);
INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Jerry'.'[email protected]'.0.25);
INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Black'.'[email protected]'.1.30);
INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('White'.'[email protected]'.0.35);

SELECT  * FROM tbl_employee;

EXPLAIN DELETE FROM tbl_employee WHERE id = 100

CREATE TABLE  tbl_user(
  id INT(11) PRIMARY KEY  AUTO_INCREMENT,
  NAME VARCHAR(50),
  logic_flag INT(11))Copy the code

Create SpringBoot project ==

Overall project structure

Create a javaBean entity class ==

public class Employee {
	
	@TableId(type = IdType.AUTO)  // Primary key growth type
    private Integer id;

    private String lastName;
    private String email;
    private Integer gender;
    private Integer age;


    @Override
    public String toString(a) {
        return "Employee{" +
                "lastName='" + lastName + '\' ' +
                ", email='" + email + '\' ' +
                ", gender=" + gender +
                ", age=" + age +
                '} ';
    }
	// Omit the getter and setter methods
    
}
Copy the code

Note: It is best to use wrapper classes when defining member variables in javabeans

4. Import dependencies ==

<dependencies>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.217.</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.016.</version>
        </dependency>

        <!--MybatisPlus-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4. 0</version>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
Copy the code

5. Configure the configuration file

6. Create mapper and add package scan annotation on the main startup class

7. Configure log output

# Configure log
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
Copy the code


4, test,

@SpringBootTest
class DemoApplicationTests {

    @Autowired
    EmployeeMapper employeeMapper;


    @Test
    void contextLoads(a) {
        List<Employee> employees = employeeMapper.selectList(null); System.out.println(employees); }}Copy the code

Test successful!





The last

I am aCode pipi shrimpI am a mantis shrimp lover who loves to share knowledge. I will keep updating my blog in the future. I look forward to your attention!!

Creation is not easy, if this blog is helpful to you, I hope you can == a key three! ==, thanks for your support, see you next time ~~~

Share the outline

Big factory interview questions column

PC crawler column

App Crawler Column