Mybatis-Plus
1. 概述
MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
快速入门:使用指南
2. 特性
无侵入 :只做增强不做改变,引入它不会对现有工程产生影响,如丝般顺滑
损耗小 :启动即会自动注入基本 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 操作智能分析阻断,也可自定义拦截规则,预防误操作
Springboot整合Mybatis-Plus
3. pom.xml中导入依赖
< dependency>
< groupId> com.baomidou</ groupId>
< artifactId> mybatis-plus-boot-starter</ artifactId>
< version> 3.4.3</ version>
</ dependency>
4. 实体类User
package com. jt. pojo ;
import com. baomidou. mybatisplus. annotation. IdType ;
import com. baomidou. mybatisplus. annotation. TableField ;
import com. baomidou. mybatisplus. annotation. TableId ;
import com. baomidou. mybatisplus. annotation. TableName ;
import lombok. Data ;
import lombok. experimental. Accessors ;
import java. io. Serializable ;
@Data
@Accessors ( chain = true )
@TableName ( "demo_user" )
public class User implements Serializable {
@TableId ( type = IdType . AUTO, value = "id" )
private Integer id;
@TableField ( "name" )
private String name;
@TableField ( "age" )
private Integer age;
@TableField ( "sex" )
private String sex;
}
5. 启动类中,配置包扫描
package com. jt ;
import org. mybatis. spring. annotation. MapperScan ;
import org. springframework. boot. SpringApplication ;
import org. springframework. boot. autoconfigure. SpringBootApplication ;
@SpringBootApplication
@MapperScan ( "com.jt.mapper" )
public class RunApp {
public static void main ( String [ ] args) {
SpringApplication . run ( RunApp . class , args) ;
}
}
6. Mapper接口
package com. jt. mapper ;
import com. baomidou. mybatisplus. core. mapper. BaseMapper ;
import com. jt. pojo. User ;
import org. apache. ibatis. annotations. Mapper ;
import java. util. List ;
public interface UserMapper extends BaseMapper < User > {
}
7. 查看BaseMapper提供的通用CRUD
具体方法见下文方法汇总
测试
8. 插入 ---- insert
package com. jt ;
import com. baomidou. mybatisplus. core. conditions. query. QueryWrapper ;
import com. jt. mapper. UserMapper ;
import com. jt. pojo. User ;
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
public class TestMP {
@Autowired
private UserMapper userMapper;
@Test
public void testMp ( ) {
User user = new User ( ) ;
user. setName ( "阿富汗" ) . setAge ( 40 ) . setSex ( "男" ) ;
Integer row = userMapper. insert ( user) ;
System . out. println ( "影响的行数:" + row) ;
}
9. 更新 ---- update
根据id更新: int | updateById(@Param("et") T entity);
@Test
public void update01 ( ) {
User user = new User ( ) ;
user. setId ( 231 ) . setName ( "李四" ) ;
Integer row = userMapper. updateById ( user) ;
System . out. println ( "影响的行数:" + row) ;
}
使用update
更新:int | update(@Param("et") T entity, @Param("ew") Wrapper<T> updateWrapper);
@Test
public void update02 ( ) {
User user = new User ( ) ;
user. setName ( "王五" ) .