1.MyBatisPlus简介

image.png

2. SpringBoot整合Mybatis-Plus

1. Spring初始化向导创建工程

image.png

2. 引入依赖

  1. <!-- mybatis启动器-->
  2. <dependency>
  3. <groupId>com.baomidou</groupId>
  4. <artifactId>mybatis-plus-boot-starter</artifactId>
  5. <version>3.5.1</version>
  6. </dependency>
  7. <!-- 数据库依赖 -->
  8. <dependency>
  9. <groupId>mysql</groupId>
  10. <artifactId>mysql-connector-java</artifactId>
  11. <scope>runtime</scope>
  12. </dependency>

3. 编写代码

1. 配置application.yaml

spring:
  # 配置数据源信息
  datasource:
    # 配置数据源类型
    type: com.zaxxer.hikari.HikariDataSource
    # 配置连接数据库信息
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mybatis_plus?serverTimezone=GMT%2B8&characterEncoding=utf-8&useSSL=false
    username: root
    password: root
===========================
    连接地址url
    MySQL5.7版本的url:
    jdbc:mysql://localhost:3306/mybatis_plus?characterEncoding=utf-8&useSSL=false
    MySQL8.0版本的url:
    jdbc:mysql://localhost:3306/mybatis_plus?
    serverTimezone=GMT%2B8&characterEncoding=utf-8&useSSL=false

2. 启动类扫描

@SpringBootApplication
@MapperScan("com.atguigu.mybatisplusdemo1.mapper")
public class MybatisPlusDemo1Application {

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

}

3. 添加实体类

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

4. 添加mapper

BaseMapper是MyBatis-Plus提供的模板mapper,其中包含了基本的CRUD方法,泛型为操作的
实体类型
public interface UserMapper extends BaseMapper<User> {
}

3. 具体看文档

MyBatisPlus(SpringBoot版).pdf