MyBatis-Plus讲解以及简单操作(二) ——使用 SpringBoot 快速使用 MyBatis-Plus

MyBatis-Plus讲解以及简单操作(二) ——使用 SpringBoot 快速使用 MyBatis-Plus

(1)准备工作
  需要 Java 开发环境(JDK)以及相应的开发工具(IDE)。
  需要 maven(用来下载相关依赖的 jar 包)。
  需要 SpringBoot。
  可以使用 IDEA 安装一个 mybatis-plus 插件。
MyBatis-Plus讲解以及简单操作(二) ——使用 SpringBoot 快速使用 MyBatis-Plus_第1张图片

(2)创建一个 SpringBoot 项目。
  方式一:去官网 https://start.spring.io/ 初始化一个,然后导入 IDE 工具即可。
  方式二:直接使用 IDE 工具创建一个。 Spring Initializer。

MyBatis-Plus讲解以及简单操作(二) ——使用 SpringBoot 快速使用 MyBatis-Plus_第2张图片
MyBatis-Plus讲解以及简单操作(二) ——使用 SpringBoot 快速使用 MyBatis-Plus_第3张图片

(3)添加 MyBatis-Plus 依赖(mybatis-plus-boot-starter)

com.baomidou mybatis-plus-boot-starter 3.3.1.tmp

(4)为了测试开发,此处使用 mysql 8,需要引入 mysql 相关依赖。
  为了简化代码,引入 lombok 依赖(减少 getter、setter 等方法)。

mysql mysql-connector-java 8.0.18 org.projectlombok lombok 1.18.10

(5)完整依赖文件(pom.xml)


4.0.0

org.springframework.boot
spring-boot-starter-parent
2.2.6.RELEASE


com.lyh.test
test-mybatis-plus
0.0.1-SNAPSHOT
test-mybatis-plus
测试 – 测试 MyBatis-Plus 功能


    1.8



    
        com.baomidou
        mybatis-plus-boot-starter
        3.3.1.tmp
    
    
        mysql
        mysql-connector-java
        8.0.18
    
    
        org.projectlombok
        lombok
        1.18.10
    

    
        org.springframework.boot
        spring-boot-starter
    

    
        org.springframework.boot
        spring-boot-starter-test
        test
        
            
                org.junit.vintage
                junit-vintage-engine
            
        
    



    
        
            org.springframework.boot
            spring-boot-maven-plugin
        
    

(6)使用一个表进行测试。

仅供参考,可以定义 创建时间、修改时间等字段。

DROP DATABASE IF EXISTS testMyBatisPlus;

CREATE DATABASE testMyBatisPlus;

USE testMyBatisPlus;

DROP TABLE IF EXISTS user;

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)
);

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]’);

MyBatis-Plus讲解以及简单操作(二) ——使用 SpringBoot 快速使用 MyBatis-Plus_第4张图片

(7)在 application.yml 文件中配置 mysql 数据源信息。

spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 123456
url: jdbc:mysql://localhost:3306/testMyBatisPlus?useUnicode=true&characterEncoding=utf8
MyBatis-Plus讲解以及简单操作(二) ——使用 SpringBoot 快速使用 MyBatis-Plus_第5张图片

(8)编写表对应的 实体类。

package entity;

import lombok.Data;

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

MyBatis-Plus讲解以及简单操作(二) ——使用 SpringBoot 快速使用 MyBatis-Plus_第6张图片

(9)编写操作实体类的 Mapper 类。
  直接继承 BaseMapper,这是 mybatis-plus 封装好的类。

package mapper;

import bean.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

public interface UserMapper extends BaseMapper {
}

MyBatis-Plus讲解以及简单操作(二) ——使用 SpringBoot 快速使用 MyBatis-Plus_第7张图片
MyBatis-Plus讲解以及简单操作(二) ——使用 SpringBoot 快速使用 MyBatis-Plus_第8张图片

(10)实体类、Mapper 类都写好了,就可以使用了。
  Step1:先得在启动类里扫描 Mapper 类,即添加 @MapperScan 注解

package com.lyh.test.testmybatisplus;

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

@MapperScan(“mapper”)
@SpringBootApplication
public class TestMybatisPlusApplication {

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

}

Step2:写一个测试类测试一下。

package com.lyh.test.testmybatisplus;

import bean.User;
import mapper.UserMapper;
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 TestMybatisPlusApplicationTests {

@Autowired
private UserMapper userMapper;

@Test
public void testSelect() {
    System.out.println(("----- selectAll method test ------"));
    List userList = userMapper.selectList(null);
    for(User user:userList) {
        System.out.println(user);
    }
}

}

MyBatis-Plus讲解以及简单操作(二) ——使用 SpringBoot 快速使用 MyBatis-Plus_第9张图片

(11)总结:
  通过以上简单操作,就能对 user 表进行 CRUD 操作,不需要去编写 xml 文件。
注:
  若遇到 @Autowired 标记的变量出现 红色下划线,但是不影响 正常运行。
  可以进入 Settings,找到 Inspection,并选择其中的 Spring Core -> Code -> Autowiring for Bean Class,将 Error 改为 Warning,即可。

本文章属于转载并加上本文作者的理解重新构思的文章,书写自己对MyBatis-Plus的理解。原作者写的很详细
原文:
https://www.cnblogs.com/l-y-h/p/12859477.html#_label0_1

你可能感兴趣的:(Mybatis-Plus,SpringBoot,Mybatis,java)