> 文章列表 > 1.mybatis-plus入门及使用

1.mybatis-plus入门及使用

1.mybatis-plus入门及使用

1.什么是MybatisPlus

MyBatis-Plus 官网

为什么要学MybatisPlus?

MybatisPlus可以节省大量时间,所有的CRUD代码都可以自动化完成MyBatis-Plus是一个MyBatis的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。

特性

  • 无侵入:只做增强不做改变,引入它不会对现有工程产生影响,如丝般顺滑

  • 损耗小:启动即会自动注入基本 CURD,性能基本无损耗,直接面向对象操作

  • 强大的 CRUD 操作:内置通用 Mapper、通用 Service,仅仅通过少量配置即可实现单表大部分 CRUD 操作,更有强大的条件构造器,满足各类使用需求

  • 支持 Lambda 形式调用:通过 Lambda 表达式,方便的编写各类查询条件,无需再担心字段写错

  • 支持主键自动生成:支持多达 4 种主键策略(内含分布式唯一 ID 生成器 - Sequence),可自由配置,完美解决主键问题

  • 支持 ActiveRecord 模式:支持 ActiveRecord 形式调用,实体类只需继承 Model 类即可进行强大的 CRUD 操作

  • 支持自定义全局通用操作:支持全局通用方法注入( Write once, use anywhere )

  • 内置代码生成器:采用代码或者 Maven 插件可快速生成 Mapper 、 Model 、 Service 、 Controller 层代码,支持模板引擎,更有超多自定义配置等您来使用

  • 内置分页插件:基于 MyBatis 物理分页,开发者无需关心具体操作,配置好插件之后,写分页等同于普通 List 查询

  • 分页插件支持多种数据库:支持 MySQL、MariaDB、Oracle、DB2、H2、HSQL、SQLite、Postgre、SQLServer 等多种数据库

  • 内置性能分析插件:可输出 SQL 语句以及其执行时间,建议开发测试时启用该功能,能快速揪出慢查询

  • 内置全局拦截插件:提供全表 delete 、 update 操作智能分析阻断,也可自定义拦截规则,预防误操作

支持数据库

任何能使用 MyBatis 进行 CRUD, 并且支持标准 SQL 的数据库,具体支持情况如下,如果不在下列表查看分页部分教程 PR 您的支持。

  • MySQL,Oracle,DB2,H2,HSQL,SQLite,PostgreSQL,SQLServer,Phoenix,Gauss ,ClickHouse,Sybase,OceanBase,Firebird,Cubrid,Goldilocks,csiidb,informix,TDengine,redshift
  • 达梦数据库,虚谷数据库,人大金仓数据库,南大通用(华库)数据库,南大通用数据库,神通数据库,瀚高数据库,优炫数据库

框架结构

framework

2.快速入门

2.1.创建数据库mybatis_plus

2.2.创建user表

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

2.3.插入数据

DELETE FROM user;INSERT INTO user (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');

2.4.初始化工程

快速初始化一个空的spring boot 项目

2.5.添加依赖

引入 Spring Boot Starter 父工程:

<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.6.3</version><relativePath/>
</parent>

引入依赖:

    <dependencies><!--spring boot启动器--><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><!--mybatis-plus启动器--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.5.1</version></dependency><!--lombok简化实体类开发--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><!--mysql驱动--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>

2.6.配置文件application.yml

spring:# 数据库datasource:type: com.zaxxer.hikari.HikariDataSourcedriver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/mybatis_plus?useSSL=false&serverTimezone=Asia/Shanghai&characterEncoding=utf-8username: rootpassword: rootmybatis-plus:configuration:# 日志log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

2.7.实体类

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

2.8.mapper接口

@Repository
public interface UserMapper extends BaseMapper<User> {
}

2.9.启动类

@SpringBootApplication
@MapperScan("org.qh.mapper")
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}
}

2.10.测试类

@SpringBootTest
public class UserMapperTest {@Resourceprivate UserMapper userMapper;/* 根据id查询*/@Testpublic void selectById(){User user = userMapper.selectById(1);System.out.println("user = " + user);}/* 查询所有*/@Testpublic void selectList(){List<User> users = userMapper.selectList(null);users.forEach(System.out::println);}/* 根据map集合中的条件查询*/@Testpublic void selectByMap(){Map<String,Object> map=new HashMap<>();map.put("name","Tom");map.put("age",28);List<User> users = userMapper.selectByMap(map);users.forEach(System.out::println);}/* 根据多个id进行批量查询*/@Testpublic void selectBatchIds(){List<Long> ids = Arrays.asList(1L, 2L, 3L);List<User> users = userMapper.selectBatchIds(ids);users.forEach(System.out::println);}/* 添加并返回id*/@Testpublic void insert(){User user = new User();user.setName("李四");user.setAge(24);user.setEmail("lisi@qq.com");int result = userMapper.insert(user);System.out.println("result = " + result);//获取id - getId() , 雪花算法System.out.println("id = " + user.getId());}/* 根据id删除*/@Testpublic void deleteById(){int result = userMapper.deleteById(1643510434159525889L);System.out.println("result = " + result);}/* 根据map集合中的条件删除*/@Testpublic void deleteByMap(){Map<String,Object> map=new HashMap<>();map.put("name","张三");map.put("age",23);int result = userMapper.deleteByMap(map);System.out.println("result = " + result);}/* 根据多个id实现批量删除*/@Testpublic void deleteBatchIds(){List<Long> ids = Arrays.asList(1L, 2L);int result = userMapper.deleteBatchIds(ids);System.out.println("result = " + result);}/* 更新*/@Testpublic void updateById(){User user = new User();user.setId(3L);user.setName("李四");user.setEmail("lisi@qq.com");int result = userMapper.updateById(user);System.out.println("result = " + result);}}