MyBatis-Plus 快速入门

1.建库建表

a.创建一个 test 的数据库;

b.在 test 数据库中,执行以下建表语句,创建 customer 表,并初始化相关数据。

CREATE TABLE `customer` (
  `id` bigint(20) NOT NULL,
  `name` varchar(255) COLLATE utf8mb4_bin NOT NULL,
  `age` int(11) NOT NULL,
  `email` varchar(255) COLLATE utf8mb4_bin NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;

INSERT INTO `customer` VALUES (1, '张三', 24, 'zhangsan@qq.com');

2.引入依赖

a.创建一个简单的 Spring Boot 工程,其目录结构如下图

在这里插入图片描述

b.引入 Lombok、Spring Web、MyBatis-Framework、MySQL Driver、mybatis-plus-boot-starter 依赖,其 pom.xml 文件内容如下

<?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.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>customer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>customer</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.4</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

3.配置

a.在 Spring Boot 工程启动类配置 MapperScan

package com.example.customer;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.example.customer.mapper")
public class CustomerApplication {

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

}

b.配置数据源等

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test1?serverTimezone=UTC&characterEncoding=utf-8
    username: root
    password: root
server:
  port: 9090
logging:
  level:
    com.example.customer.mapper: debug

4.编码

a.创建 Customer 实体类

package com.example.customer.entity;

import lombok.Data;

@Data
public class Customer {

    private Long id;
    private String name;
    private Integer age;
    private String email;

}

b.创建 CustomerMapper 接口,继承 BaseMapper

package com.example.customer.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.customer.entity.Customer;

public interface CustomerMapper extends BaseMapper<Customer> {
}

5.测试

a.抽取任意方法测试

package com.example.customer;

import com.example.customer.entity.Customer;
import com.example.customer.mapper.CustomerMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest
class CustomerApplicationTests {

    @Autowired
    private CustomerMapper customerMapper;

    @Test
    void contextLoads() {
        List<Customer> customers = customerMapper.selectList(null);
        System.out.println("customers = " + customers);
    }

}

b.查看日志,可以看到执行的 SQL 语句

2021-04-18 00:05:31.781  INFO 6076 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2021-04-18 00:05:32.642  INFO 6076 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2021-04-18 00:05:32.657 DEBUG 6076 --- [           main] c.e.c.mapper.CustomerMapper.selectList   : ==>  Preparing: SELECT id,name,age,email FROM customer
2021-04-18 00:05:32.701 DEBUG 6076 --- [           main] c.e.c.mapper.CustomerMapper.selectList   : ==> Parameters: 
2021-04-18 00:05:32.743 DEBUG 6076 --- [           main] c.e.c.mapper.CustomerMapper.selectList   : <==      Total: 1
customers = [Customer(id=1, name=张三, age=24, email=zhangsan@qq.com)]
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值