MyBatis-Plus
1.MyBatis-Plus简介
MyBatis 也是简化开发的。配置文件和Mappper.xml !
2.MyBatis-Plus特性
特性
- 无侵入:只做增强不做改变,引入它不会对现有工程产生影响,如丝般顺滑
- 损耗小:启动即会自动注入基本 CURD,性能基本无损耗,直接面向对象操作
- 强大的 CRUD 操作:内置通用 Mapper、通用 Service,仅仅通过少量配置即可实现单表大部分CRUD 操作,更有强大的条件构造器,满足各类使用需求. 继承一个类就好了!
- 支持 Lambda 形式调用:通过 Lambda 表达式,方便的编写各类查询条件,无需再担心字段写错
- 支持主键自动生成:支持多达 4 种主键策略(内含分布式唯一 ID 生成器 - Sequence),可自由配置,完美解决主键问题
- 支持 ActiveRecord 模式:支持 ActiveRecord 形式调用,实体类只需继承 Model 类即可进行强大的 CRUD 操作
- 支持自定义全局通用操作:支持全局通用方法注入( Write once, use anywhere )
- 内置代码生成器(pojo、dao、xml、service、…):采用代码或者 Maven 插件可快速生成Mapper 、 Model 、 Service 、 Controller 层代码,支持模板引擎,更有超多自定义配置等您来使用
- 内置分页插件:基于 MyBatis 物理分页,开发者无需关心具体操作,配置好插件之后,写分页等同于普通 List 查询
- 分页插件支持多种数据库:支持 MySQL、MariaDB、Oracle、DB2、H2、HSQL、SQLite、Postgre、SQLServer 等多种数据库
- 内置性能分析插件:可输出 Sql 语句以及其执行时间,建议开发测试时启用该功能,能快速揪出慢查询
- 内置全局拦截插件:提供全表 delete 、 update 操作智能分析阻断,也可自定义拦截规则,预防误操作
支持数据库
- mysql 、 mariadb 、 oracle 、 db2 、 h2 、 hsql 、 sqlite 、 postgresql 、 sqlserver
- 达梦数据库 、 虚谷数据库 、 人大金仓数据库
3.MyBatis-Plus简单使用
1.导入依赖
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.michael.learn</groupId>
<artifactId>mybatis-plus</artifactId>
<version>1.0-SNAPSHOT</version>
<name>mybatis-plus</name>
<description>mybatis-plus</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--导入mp的依赖
mybatis-plus 就不需要导入 mybatis!
-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!--
spring2.x 都是默认配置mysql8的驱动
<mysql.version>8.0.19</mysql.version>
8.0 以上的驱动,需要配置时区!
-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!-- IDEA中需要安装lombok插件! -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.配置数据源
构造数据sql
CREATE DATABASE mybatis_plus;
DROP TABLE IF EXISTS user02;
CREATE TABLE user02
(
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)
)ENGINE=InnoDB;
INSERT INTO user02 (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:
datasource:
url: jdbc:mysql://localhost:3306/mybatis_plus?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: Haishao123
3.编写实体类
package com.michal.learn.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User02 {
private Long id;
private String name;
private Integer age;
private String email;
}
4.实现接口集成BaseMapper
package com.michal.learn.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.michal.learn.pojo.User02;
import org.springframework.stereotype.Repository;
@Repository
public interface User02Mapper extends BaseMapper<User02> {
}
5.测试使用
package com.michal.learn;
import com.michal.learn.dao.User02Mapper;
import com.michal.learn.pojo.User02;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
@SpringBootTest
public class SimpleTest {
@Autowired
private User02Mapper user02Mapper;
//select
@Test
void testGetUserList(){
System.out.println("=========test the firt mybatis plus========");
List<User02> user02s = user02Mapper.selectList(null);
user02s.forEach(System.out::println);
}
//insert
@Test
void testAddUser(){
User02 user02 = new User02();
user02.setAge(18);
user02.setEmail("testAddUser@163.com");
user02.setName("michael");
System.out.println(user02);//User02(id=null, name=michael, age=18, email=testAddUser@163.com)
user02Mapper.insert(user02);//insert help us automatically add the id 1291552008862117889
System.out.println(user02); //User02(id=1291552008862117889, name=michael, age=18, email=testAddUser@163.com)
}
//update
@Test
void testUpdateUser(){
User02 user02 = new User02();
user02.setId(1L);
user02.setEmail("test@update.com");
System.out.println(user02); //User02(id=1, name=null, age=null, email=test@update.com)
user02Mapper.updateById(user02);//sql:UPDATE user02 SET email='test@update.com' WHERE id=1
System.out.println(user02);//User02(id=1, name=null, age=null, email=test@update.com)
}
//delete
@Test
void testDeleteUser(){
user02Mapper.deleteBatchIds(Arrays.asList(1L,2L,3L));
/* ==> Preparing: DELETE FROM user02 WHERE id IN ( ? , ? , ? )
==> Parameters: 1(Long), 2(Long), 3(Long)
<== Updates: 3*/
}
//delete by map
@Test
void testDeleteUserByMap(){
HashMap<String, Object> map = new HashMap<>();
map.put("name","Sandy");
map.put("age",21);
user02Mapper.deleteByMap(map);
/*==> Preparing: DELETE FROM user02 WHERE name = ? AND age = ?
==> Parameters: Sandy(String), 21(Integer)
<== Updates: 1*/
}
}