1、mp的简介(Mybatis-plus)
MyBatis-Plus (opens new window)(简称 MP)是一个 MyBatis (opens new window)的增强工具,在 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 操作智能分析阻断,也可自定义拦截规则,预防误操作
2、如何使用
导入依赖
<dependencies> <!-- swagger2依赖引入--> <dependency> <groupId>io.github.jianzhichun</groupId> <artifactId>spring-boot-starter-swagger2</artifactId> <version>0.0.1</version> </dependency> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>swagger-bootstrap-ui</artifactId> <version>1.9.6</version> </dependency> <!-- 数据库驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!-- mp依赖--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.3.2</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <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> </dependency> </dependencies>
分页配置(只适用于3.4以上的mp)
package com.aaa.config;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@MapperScan("scan.your.mapper.package")
public class MybatisPlusConfig {
/**
* 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题(该属性会在旧插件移除后一同移除)
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));
return interceptor;
}
}
CRUD单元测试
package com.aaa.sbm;
import com.aaa.dao.StudentDao;
import com.aaa.pojo.Student;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;
@SpringBootTest
class SbmApplicationTests {
@Autowired
private StudentDao studentDao;
//根据id查询
@Test
void testById() {
Student student = studentDao.selectById(1);
System.out.println(student);
}
//添加操作
@Test
void testSave(){
Student student=new Student(); //mp自动为主键生成值,默认按照雪花算法[得到的值100%不会重复] 能否修改主键的生成策略, 如果想用主键自增必须要求数据库中的表是主键递增模式
student.setStudentId(9527);
student.setName("王一");
student.setAge(19);
student.setSex("男");
student.setClassId(2);
student.setHeadImg("aaa.jpg");
int insert = studentDao.insert(student);
System.out.println("影响的行数"+insert);
}
//根据主键删除
@Test
void testDelete(){
// int i = userDao.deleteById(1670635267880386564L);
// System.out.println("删除影响的行数:"+i);
// 批量删除
// List<Integer> ids= Arrays.asList(1,2,5,7,9);
// int i = userDao.deleteBatchIds(ids);
// System.out.println("删除影响的行数:"+i);
UpdateWrapper<Student> wrapper=new UpdateWrapper<>();//修改的条件构造器。
// wrapper.eq("name","王一"); 相等
// wrapper.like("name","霍"); 模糊查询
// wrapper.gt(); 大于 wrapper.lt() 小于 wrapper.ne() 不等于
// wrapper.ge(); 大于等于 wrapper.le() 小于等于
wrapper.likeRight("name","霍"); //右边匹配
wrapper.or(); //默认多个条件使用and连接。 如果想使用or 则调用or()
wrapper.between("age",18,20);
studentDao.delete(wrapper);//Wrapper对象:它是条件构造器。
}
//查询
@Test
void testSelect(){
// //根据id批量查询
// List<Integer> ids = Arrays.asList(1,2,3,4,5,6);
// List<Student> students = studentDao.selectBatchIds(ids);
// System.out.println(students);
//根据条件查询
// QueryWrapper<Student> wrapper = new QueryWrapper<>();
// wrapper.select("name","age"); //指定查询的列
wrapper.orderByDesc(); //降序
wrapper.groupBy(); //分组 select 分组列,聚合函数 from 表名 group by 列
// List<Student> students1 = studentDao.selectList(wrapper); //根据条件查询,如果传递的条件对象为null,则查询全部
// System.out.println(students1);
//根据条件查询一条记录 登录:账号和密码
// QueryWrapper<Student> wrapper = new QueryWrapper<>();
// wrapper.eq("name","王一");
// Student student = studentDao.selectOne(wrapper);
// System.out.println(student);
//分页查询 配置分页拦截器======
IPage<Student> page = new Page<>(2,3);
IPage<Student> page1 = studentDao.selectPage(page,null);
System.out.println("总条数:"+page1.getTotal());
System.out.println("总页数:"+page1.getPages());
System.out.println("当前页的记录:"+page1.getRecords());
}
//根据id修改
@Test
void testUpdate(){
Student student=new Student();
student.setStudentId(9527);
student.setName("王一");
student.setAge(19);
student.setSex("男");
student.setClassId(2);
student.setHeadImg("aaa.jpg");
int insert = studentDao.updateById(student);
System.out.println("影响的行数"+insert);
}
}