01-MyBatis-Plus概述与入门
一、MyBatis-Plus概述与入门
1、简介
- MyBatis-Plus (opens new window)(简称 MP)是一个 MyBatis (opens new window)的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
- 官网
2、特性
- 无侵入:只做增强不做改变,不会对现有工程产生影响
- 强大的 CRUD 操作:内置通用 Mapper,少量配置即可实现单表CRUD 操作(如果只做单表增删查改不需要你写任何的sql)
- 支持 Lambda:编写查询条件无需担心字段写错
- 支持主键自动生成
- 内置分页插件
- ....
3、入门案例
3.1、操作步骤
-
①、创建新项目mybatis-plus-test(具体过程这里就不再过多演示)
-
②、添加相关的起步依赖
-
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.coolman</groupId> <artifactId>mybatis-plus-test</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <!--继承父类--> <parent> <artifactId>spring-boot-dependencies</artifactId> <groupId>org.springframework.boot</groupId> <version>2.5.6</version> </parent> <!--依赖管理--> <dependencies> <!--SpringBoot 启动依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!--SpringBoot整合test--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <!--MyBatis-Plus驱动--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.2</version> </dependency> <!--druid连接池--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.23</version> </dependency> <!--mysql驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.22</version> <scope>runtime</scope> </dependency> <!--lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies> </project>
-
-
③、制作实体类与表结构
-
直接导入SQL脚本即可,如下所示
-
CREATE DATABASE IF NOT EXISTS mybatisplus_db CHARACTER SET utf8; USE mybatisplus_db; CREATE TABLE `user` ( id BIGINT(20) PRIMARY KEY AUTO_INCREMENT, `name` VARCHAR(32) NOT NULL, gender CHAR(1), `password` VARCHAR(32) NOT NULL, age INT(3) NOT NULL , tel VARCHAR(32) NOT NULL ); INSERT INTO `user` VALUES(NULL,'Rose','女','123456',12,'12345678910'); INSERT INTO `user` VALUES(NULL,'Jack','男','123456',8,'12345678910'); INSERT INTO `user` VALUES(NULL,'Jerry','男','123456',15,'12345678910'); INSERT INTO `user` VALUES(NULL,'NewBoy','男','123456',19,'12345678910'); INSERT INTO `user` VALUES(NULL,'Kate','女','123456',28,'12345678910'); INSERT INTO `user` VALUES(NULL,'张晓','女','123456',22,'12345678910'); INSERT INTO `user` VALUES(NULL,'张大炮','男','123456',16,'12345678910'); SELECT * FROM `user`;
-
同时创建相对应的实体类
-
package com.coolman.model; import lombok.Data; @Data public class User { private Long id; private String name; private String gender; private String password; private Integer age; private String tel; }
-
-
-
④、设置jdbc参数(配置yml文件)
-
## 配置spring spring: # 数据源配置 datasource: username: root password: 123 driver-class-name: com.mysql.cj.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource url: jdbc:mysql://localhost:3306/mybatisplus_db?serverTimezone=Asia/Shanghai # 配置mybatis-plus mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 显示SQL语句的配置
-
-
⑤、定义数据接口,继承BaseMapper
-
package com.coolman.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.coolman.model.User; /* 1、Dao接口以后只需要继承BaseMapper接口,那么单表的增删改查就全部都实现了 2、继承BaseMapper接口的时候一定要指定操作哪个实体类 3、alt + 7 可以查看到该类的所有成员 */ public interface UserMapper extends BaseMapper<User> { }
-
PS:编写好Mapper接口后,还需要在启动类上添加@MapperScan注解扫描Mapper包
-
-
⑥、测试类中注入dao接口,测试功能
-
1、新建测试类,并在测试类上添加
@SpringBootTest
注解 -
2、注入UserMapper对象
-
3、selectList查询所有用户,参数为null
-
package com.coolman.test; import com.coolman.mapper.UserMapper; import com.coolman.model.User; import lombok.extern.slf4j.Slf4j; 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 @Slf4j public class MyBatisPlusTest { // 注入mapper层对象 @Autowired(required = false) private UserMapper userMapper; @Test public void test01() { // selectList查询所有用户,参数为null List<User> users = userMapper.selectList(null); users.forEach(user -> { log.info(user + ""); }); } }
-
-
运行结果
3.2、小结
- 使用MyBatis-Plus的步骤
- 1、导入依赖
- 2、编写实体类,实体类类名默认是对应的表名
- 3、在
application.yml
文件上配置连接池参数 - 4、编写Mapper接口并继承BaseMapper,并且要指定BaseMapper接口操作的实体类
- 5、在启动类添加Mapper包扫描注解
- 6、编写代码进行测试