(1)准备工作
需要 Java 开发环境(JDK)以及相应的开发工具(IDE)。
需要 maven(用来下载相关依赖的 jar 包)。
需要 SpringBoot。
可以使用 IDEA 安装一个 mybatis-plus 插件。
(2)创建一个 SpringBoot 项目。
方式一:去官网 https://start.spring.io/ 初始化一个,然后导入 IDE 工具即可。
方式二:直接使用 IDE 工具创建一个。 Spring Initializer。
(3)添加 MyBatis-Plus 依赖(mybatis-plus-boot-starter)
com.baomidou mybatis-plus-boot-starter 3.3.1.tmp(4)为了测试开发,此处使用 mysql 8,需要引入 mysql 相关依赖。
为了简化代码,引入 lombok 依赖(减少 getter、setter 等方法)。
(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]’);
(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
(8)编写表对应的 实体类。
package entity;
import lombok.Data;
@Data
public class User {
private Long id;
private String name;
private int age;
private String email;
}
(9)编写操作实体类的 Mapper 类。
直接继承 BaseMapper,这是 mybatis-plus 封装好的类。
package mapper;
import bean.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
public interface UserMapper extends BaseMapper {
}
(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);
}
}
}
(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