Shangyitong-background building-MybatisPlus quick start

Write catalog title here

Create database

The corresponding database Schema script is as follows:

CREATE TABLE USER
(
    id BIGINT(20)NOT NULL COMMENT '主键ID',
    NAME VARCHAR(30)NULL DEFAULT NULL COMMENT '姓名',
    age INT(11)NULL DEFAULT NULL COMMENT '年龄',
    email VARCHAR(50)NULL DEFAULT NULL COMMENT '邮箱',
    PRIMARY KEY (id)
);

The corresponding database Data script is as follows:

INSERT INTO user (id, name, age, email)VALUES
(1, 'Jone', 18, '[email protected]'),
(2, 'Jack', 20, '[email protected]'),
(3, 'Tom', 28, '[email protected]'),
(4, 'Sandy', 21, '[email protected]'),
(5, 'Billie', 24, '[email protected]');

Quick start

1.1 Springboot initializr

Use Spring Initializr to quickly initialize a Spring Boot project
Group: com.atguigu
Artifact: mybatis_plus
Version: 2.2.1.RELEASE

1.2 Introduce dependencies

Note: After the introduction of MyBatis-Plus, please do not introduce MyBatis again to avoid problems caused by version differences.

Remember to install the lombok plugin in idea

<dependencies>
<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>
<!--mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.1</version>
</dependency>

<!--mysql依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--lombok用来简化实体类-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>

1.3 Database configuration

Add the relevant configuration of the MySQL database in the application.properties configuration file:
spring boot 2.0 (built-in jdbc5 driver)

#mysql数据库连接
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis_plus?characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=root

spring boot 2.1 and above (built-in jdbc8 driver)
Note: changes in driver and url

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis_plus?serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=root

note:

1. The url here uses the suffix?serverTimezone=GMT%2B8, because the 8.0 version of the jdbc driver needs to add this suffix, otherwise the test case will report the following error:
java.sql.SQLException: The server time zone value'Öйú±ê× ¼Ê±¼ä' is unrecognized or represents more

2. The driver-class-name here uses com.mysql.cj.jdbc.Driver. It is recommended to use this driver in jdbc 8, otherwise there will be WARN information when running test cases

1.4 Writing entity classes

Add the @MapperScan annotation to the Spring Boot startup class to scan the Mapper folder

@SpringBootApplication
@MapperScan("com.atguigu.demomptest.mapper")
public class DemomptestApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemomptestApplication.class, args);
    }

}

Add entity
Create package entity Write entity class User.java (Lombok simplified code is used here)

@Data
public class User {
    private Long id;
    private String name;
    private Integer age;
    private String email;
}

View compilation results

Add mapper,
create package mapper, write Mapper interface: UserMapper.java

@Repository
public interface UserMapper extends BaseMapper<User> {
}

Test
Add test class to perform functional test:

@SpringBootTest
class DemomptestApplicationTests {

    @Autowired
    private UserMapper userMapper;

    @Test
    public void findAll() {
        List<User> users = userMapper.selectList(null);
        System.out.println(users);
    }
}

Note:
IDEA reports an error at userMapper because the injected object cannot be found, because the class is dynamically created, but the program can be executed correctly.

In order to avoid errors, you can add @Repository annotations to the dao layer interface.
Through the above few simple steps, we have realized the CRUD function of the User table, even without writing XML files!
View the console output:

Insert picture description here