mybatis-plus快速入门:
简介:
MyBatis-Plus (opens new window)(简称 MP)是一个 MyBatis (opens new window)的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。(官方文档介绍)
特性:
无侵入、损耗小、强大的 CRUD 操作等,官网上都有介绍,在这就不再赘述
mybatis-plus地址:https://mp.baomidou.com/guide
快速入门:
环境要求:java开发环境(jdk,maven,mysql等)及对应的ide
技术要求:熟悉Spring Boot,Maven
现有一张表Student表,其表结构如下:
数据库表对应sql如下:
DROP TABLE IF EXISTS student;
CREATE TABLE student
(
id BIGINT(20) NOT NULL COMMENT '主键ID',
name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
age INT(11) NULL DEFAULT NULL COMMENT '年龄',
class_name VARCHAR(30) NULL DEFAULT NULL COMMENT '班级',
PRIMARY KEY (id)
);
对应数据库data数据sql如下:
DELETE FROM student;
INSERT INTO student (id, name, age, class_name) VALUES
(1, 'Tom', 18, 'class1'),
(2, 'Jack', 19, 'class1'),
(3, 'Angle', 21, 'class2'),
(4, 'sam', 20, 'class2'),
(5, 'Bil', 18, 'class3');
初始化工程:
创建一个空的Spring Boot工程(工程将以mysql数据库进行演示)
可以使用Spring Initializer快速初始化一个SpringBoot工程: https://start.spring.io/
添加依赖:
引入Spring Boot Starter 父工程:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
引入 spring-boot-starter、spring-boot-starter-test、mybatis-plus-boot-starter、mysql依赖:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
配置
在 application.yml
配置文件中添加 Mysql 数据库的相关配置:
spring:
datasource:
name: dev
url: 数据库地址
username: 数据库名
password: 数据库密码
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
#mybatis plus 设置
mybatis-plus:
db-config:
#主键类型 0:"数据库ID自增",1:"该类型为未设置主键类型", 2:"用户输入ID",3:"全局唯一ID (数字类型唯一ID)", 4:"全局唯一ID UUID",5:"字符串全局唯一ID (idWorker 的字符串表示)";
# AUTO(0),NONE(1),INPUT(2),ASSIGN_ID(3),ASSIGN_UUID(4),
id-type: AUTO
# 默认数据库表下划线命名
table-underline: true
这是我的项目目录:
编码
编写实体类及Mapper类:Student.java(此处使用了Lombok插件简化了代码),StudentMapper.java
Student实体类 :
@Data
public class Student extends Model<Student> {
private Long id;
private String name;
private Integer age;
private String className;
}
StudentMapper类 :
public interface StudentMapper extends BaseMapper<Student> {
}
在 Spring Boot 启动类中添加 @MapperScan
注解,扫描 Mapper 文件夹:
@SpringBootApplication
@MapperScan("com.example.demo.mapper")//mapper包路径
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
开始测试
编写测试类,进行功能测试:
@SpringBootTest
@RunWith(SpringRunner.class)
class DemoApplicationTests {
@Autowired
private StudentMapper studentMapper;
@Test
public void testSelect() {
System.out.println(("----- selectAll method test ------"));
List<Student> studentList = studentMapper.selectList(null);
System.out.println("学生人数:"+studentList .size());
studentList.forEach(System.out::println);
}
@Test
public void testInster() {
System.out.println(("----- insert method test ------"));
Student student=new Student();
student.setAge(19);
student.setClassName("class3");
student.setName("zhu");
studentMapper.insert(student);
List<Student> studentList = studentMapper.selectList(null);
System.out.println("学生人数:"+studentList.size());
studentList.forEach(System.out::println);
}
}
执行测试方法testSelect控制台输出:
学生人数:5
Student(id=1, name=Tom, age=18, className=class1)
Student(id=2, name=Jack, age=19, className=class1)
Student(id=3, name=Angle, age=21, className=class2)
Student(id=4, name=sam, age=20, className=class2)
Student(id=5, name=Bil, age=18, className=class3)
执行测试方法testInster控制台输出:
学生人数:6
Student(id=1, name=Tom, age=18, className=class1)
Student(id=2, name=Jack, age=19, className=class1)
Student(id=3, name=Angle, age=21, className=class2)
Student(id=4, name=sam, age=20, className=class2)
Student(id=5, name=Bil, age=18, className=class3)
Student(id=6, name=zhu, age=19, className=class3)
小结
通过以上几个简单的步骤,我们就实现了 Student表的 CRUD 功能,甚至连 XML 文件都不用编写!
从以上步骤中,我们可以看到集成MyBatis-Plus
非常的简单,只需要引入 starter 工程,并配置 mapper 扫描路径即可。