微笑的眼泪

V1

2022/08/29阅读:40主题:默认主题

MybatisPlus入门案例

开发环境

  • IDE:IDEA 2021.1.3
  • JDK:JDK8+
  • 构建工具:Maven 3.6.3
  • MySQL:MySQL 8.0.24
  • Navicat:Navicat Premium 15
  • Spring Boot:2.7.2
  • MyBatis-Plus:3.5.1

建库建表

打开Navicat运行以下SQL脚本进行建库建表

CREATE DATABASE `mybatis_plus` /*!40100 DEFAULT CHARACTER SET utf8mb4 */
use `mybatis_plus`
CREATE TABLE `user` ( 
    `id` bigint(20NOT NULL COMMENT '主键ID'
    `name` varchar(30DEFAULT NULL COMMENT '姓名'
    `age` int(11DEFAULT NULL COMMENT '年龄'
    `email` varchar(50DEFAULT NULL COMMENT '邮箱'
    PRIMARY KEY (`id`
ENGINE=InnoDB DEFAULT CHARSET=utf8;

插入几条测试数据

INSERT INTO user (id, name, age, email) VALUES 
(1, 'Jone', 18, 'test1@baomidou.com'), 
(2, 'Jack', 20, 'test2@baomidou.com'), 
(3, 'Tom', 28, 'test3@baomidou.com'), 
(4, 'Sandy', 21, 'test4@baomidou.com'), 
(5, 'Billie', 24, 'test5@baomidou.com');

创建工程

使用Spring Initializer快速初始化一个 Spring Boot 工程,选择 MySQL 驱动和 Lombok。

image-20220725235418242
image-20220725235418242

引入MyBatis-Plus的依赖

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.5.1</version>
</dependency>

安装Lombok插件

image-20220726000109656
image-20220726000109656

配置编码

配置application.yml文件

  • 驱动类 driver-class-name:
    • spring boot 2.0(内置jdbc5驱动),驱动类使用:driver-class-name: com.mysql.jdbc.Driver
    • spring boot 2.1及以上(内置jdbc8驱动),驱动类使用: driver-class-name: com.mysql.cj.jdbc.Driver
    • 否则运行测试用例的时候会有 WARN 信息
  • 连接地址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
    • 否则运行测试用例报告如下错误: java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more
  • 为了查看日志输出的结果,需要配置日志输出。
#配置端口
server:
  port: 80

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: 123456

#MyBatis-Plus相关配置
mybatis-plus:
  configuration:
    #配置日志
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

在 Spring Boot 启动类中添加 @MapperScan 注解,扫描 Mapper 文件夹

@SpringBootApplication
@MapperScan("指定Mapper接口所在的包")
public class MybatisPlusDemo1Application {
 public static void main(String[] args) {
  SpringApplication.run(MybatisPlusDemo1Application.classargs);
 }
}

编写实体类 User.java(此处使用了 Lombok 简化代码)

package com.dawn.mybatisplus.pojo;

import lombok.Data;

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

编写 Mapper 包下的 UserMapper接口

BaseMapper 是 MyBatis-Plus 提供的模板 mapper,其中包含了基本的 CRUD 方法,泛型为操作的实体类型

package com.dawn.mybatisplus.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.dawn.mybatisplus.pojo.User;

public interface UserMapper extends BaseMapper<User{
}

测试查询

编写一个测试类MyBatisPlusTest.java

package com.dawn.mybatisplus;

import com.dawn.mybatisplus.mapper.UserMapper;
import com.dawn.mybatisplus.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.util.List;

@SpringBootTest
class MyBatisPlusTest {

    @Resource
    private UserMapper userMapper;

    @Test
    void testSelectList(){
        //通过条件构造器查询一个list集合,若没有条件,则可以设置null为参数
        List<User> users = userMapper.selectList(null);
        users.forEach(System.out::println);
    }

}

注意:

IDEA在 userMapper 处报错,因为找不到注入的对象,因为类是动态创建的,但是程序可以正确的执行。

为了避免报错,可以在mapper接口上添加 @Repository 注解或者使用 @Resource 注解进行注入。

控制台打印查询结果

image-20220726001527686
image-20220726001527686

分类:

后端

标签:

后端

作者介绍

微笑的眼泪
V1