MyBatis-Plus (简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
1.1 特点
-
无侵入:只做增强不做改变,引入它不会对现有工程产生影响,如丝般顺滑
-
损耗小:启动即会自动注入基本 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 操作智能分析阻断,也可自定义拦截规则,预防误操作
1.2 支持数据库
MySQL,Oracle,DB2,H2,HSQL,SQLite,PostgreSQL,SQLServer,Phoenix,Gauss ,ClickHouse,Sybase,OceanBase,Firebird,Cubrid,Goldilocks,csiidb等。
1.3 jar包依赖
<dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-spring-boot3-starter</artifactId><version>3.5.5</version></dependency>
1.4使用前配置文件
spring:
datasource:
url: jdbc:mysql://localhost:3306/testPlus
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis-plus:
configuration://数据库日志
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
@Data @TableName("sys_role") public class SysRole { /*** @TableName注解,对应表名*///@TableField对应数据表字段@TableField("id") @TableId(type = IdType.AUTO)private Long id;@TableField("role_name")private String roleName;@TableField("role_code")private String roleCode;@TableField("description")private String description;@TableField("create_time")private Date createTime;@TableField("update_time")private Date updateTime;@TableField("is_deleted") @TableLogic(设置逻辑删除位)private Integer isDeleted;}
MyBatis-Plus 支持多种主键生成策略,包括自增ID、UUID、雪花算法(Snowflake)等。你可以在实体类的主键字段上使用
@TableId
注解来指定主键生成策略。以下是几种常用的主键生成策略的示例代码:
1.自增ID(IncrementIdGenerator):
@TableId(type = IdType.AUTO)
private Long id;
2.uuid(UUIDGenerator):
@TableId(type = IdType.UUID)
private String id;
3.雪花算法ID(SnowflakeIdGenerator):
@TableId(type = IdType.ASSIGN_ID)
private Long id;
4.手动指定ID(NoneIdGenerator):
@TableId(type = IdType.NONE)
private Long id;
在实际使用时,你可以根据业务需求选择合适的主键生成策略。如果使用雪花算法ID,则无需额外配置,MyBatis-Plus已经内置了该算法。如果选择手动指定ID,则在插入数据时需要自己保证ID的唯一性。
1.5Mapper层继承BaseMapper
@Mapper
public interface SysRoleMapper extends BaseMapper<SysRole> {@Select("select * from sys_role where role_name " +" like concat('%',#{roleName},'%') or role_code like concat('%',#{roleCode},'%') " +" and is_deleted = 0 ")List<SysRole> selectMany(SysRole sysRole);}
MyBatisPlus对数据库增删改大多数传值是pojo对象
SysRole sysRole = new SysRole();sysRole.setRoleName("weiqi");sysRole.setRoleCode("idDefault");int row = sysRoleMapper.insert(sysRole);
SysRole sysRole = new SysRole();sysRole.setId(1851852922369245185L);sysRole.setRoleName("udata");sysRole.setRoleCode("123");sysRole.setDescription("updata");int rowa=sysRoleMapper.updateById(sysRole);
int row = sysRoleMapper.deleteById(1851852922369245187L);
QueryWrapper<SysRole> sysRoleQueryWrapper = new QueryWrapper<>();sysRoleQueryWrapper.like("role_name","w");List<SysRole> sysRoleList=sysRoleMapper.selectList(sysRoleQueryWrapper);for (SysRole sysRole : sysRoleList){System.out.println("sysRole = " + sysRole);}
QueryWrapper<SysRole> sysRoleQueryWrapper = new QueryWrapper<>();sysRoleQueryWrapper.or(wrapper -> wrapper.like("role_name", "w").or().like("role_code", "1"));List<SysRole> sysRoleList=sysRoleMapper.selectList(sysRoleQueryWrapper);for (SysRole s : sysRoleList){System.out.println("sysRole = " + s);}
QueryWrapper<SysRole> sysRoleQueryWrapper = new QueryWrapper<>();sysRoleQueryWrapper.or(wrapper -> wrapper.like("role_name", "w").or().like("role_code", "1"));Page<SysRole> sysRolePage = new Page<>(2,1);IPage<SysRole> sysRoleIPage=sysRoleMapper.selectPage(sysRolePage,sysRoleQueryWrapper);System.out.println(sysRoleIPage.getRecords());//数据System.out.println(sysRoleIPage.getTotal());//总页数
1.6 条件构造器
Wrapper : 条件构造抽象类,最顶端父类
AbstractWrapper : 用于查询条件封装,生成 sql 的 where 条件
QueryWrapper : Entity 对象封装操作类,不是用lambda语法
UpdateWrapper : Update 条件封装,用于Entity对象更新操作
AbstractLambdaWrapper : Lambda 语法使用 Wrapper统一处理解析 lambda 获取 column。
LambdaQueryWrapper :看名称也能明白就是用于Lambda语法使用的查询Wrapper
LambdaUpdateWrapper : Lambda 更新封装Wrapper
注意:以下条件构造器的方法入参中的 column
均表示数据库字段名