mybatis-plus
一、 mybatis-plus
1.介绍mybatis-plus
MyBatis-Plus (opens new window)(简称 MP)是一个 MyBatis (opens new window)的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。 但是对于联表操作还必须使用mybatis |
---|
1.2 mybatis-plus特征
(1)无侵入:只做增强不做改变,引入它不会对现有工程产生影响,如丝般顺滑
(2)损耗小:启动即会自动注入基本 CURD,性能基本无损耗,直接面向对象操作
(3)强大的 CRUD 操作:内置通用 Mapper、通用 Service,仅仅通过少量配置即可实现单表大部分 CRUD 操作,更有强大的条件构造器,满足各类使用需求
(4)支持 Lambda 形式调用:通过 Lambda 表达式,方便的编写各类查询条件,无需再担心字段写错
(5)支持主键自动生成:支持多达 4 种主键策略(内含分布式唯一 ID 生成器 - Sequence),可自由配置,完美解决主键问题
(6)支持 ActiveRecord 模式:支持 ActiveRecord 形式调用,实体类只需继承 Model 类即可进行强大的 CRUD 操作
(7)支持自定义全局通用操作:支持全局通用方法注入( Write once, use anywhere )
(8)内置代码生成器:采用代码或者 Maven 插件可快速生成 Mapper 、 Model 、 Service 、 Controller 层代码,支持模板引擎,更有超多自定义配置等您来使用
(9)内置分页插件:基于 MyBatis 物理分页,开发者无需关心具体操作,配置好插件之后,写分页等同于普通 List 查询
(10)分页插件支持多种数据库:支持 MySQL、MariaDB、Oracle、DB2、H2、HSQL、SQLite、Postgre、SQLServer 等多种数据库
(11)内置性能分析插件:可输出 Sql 语句以及其执行时间,建议开发测试时启用该功能,能快速揪出慢查询
(12)内置全局拦截插件:提供全表 delete 、 update 操作智能分析阻断,也可自定义拦截规则,预防误操作
1.3 如何使用mybatis-plus
1.创建一个springboot工程并加入相关的依赖
<!--①引入相关的依赖-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
2.配置文件
spring.datasource.druid.username=root
spring.datasource.druid.password=123456
spring.datasource.druid.url=jdbc:mysql://localhost:3306/springboot?serverTimezone=Asia/Shanghai
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.druid.max-active=5
#配置映射文件所在的路径
mybatis.mapper-locations=classpath:/mapper/*.xml
3.实体类
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private Integer id;
private String name;
private Integer age;
private Integer sex;
}
4.接口
//③ 创建一个接口并继承BaseMapper
public interface UserMapper extends BaseMapper<User> {
}
5.在主启动类上mapper的扫描
package com.jsc.mybatisplus;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
//包扫描
@MapperScan("com.jsc.mybatisplus.mapper")
public class MybatisPlusApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisPlusApplication.class, args);
}
}
6.测试
@SpringBootTest
class MybatisPlusApplicationTests {
@Resource
private UserMapper userMapper;
//查询
@Test
void contextLoads() {
User user = userMapper.selectById(1);
System.out.println(user);
}
//添加
@Test
public void testInsert(){
User user = new User(null,"dfkgkdfgjdf",17,"111@qq.com",0,null,null);
int row = userMapper.insert(user);
System.out.println(row);
}
二、crud
2.1 增加inset
entuty:
package com.jsc.mybatisplus.entity;
import com.baomidou.mybatisplus.annotation.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
//必须数据库也是递增
@TableId(type = IdType.AUTO)
private Long id;
private String name;
private int age;
private String email;
@TableLogic
private Integer deleted;
@TableField(fill = FieldFill.INSERT)
private Date createTime;
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date updateTime;
public User(Long id, String name, int age, String email, Integer deleted) {
this.id = id;
this.name = name;
this.age = age;
this.email = email;
this.deleted = deleted;
}
}
测试:
@Test
public void testInsert(){
User user = new User(null,"dfkgkdfgjdf",17,"111@qq.com",0,null,null);
int row = userMapper.insert(user);
System.out.println(row);
}
2.2 删除delete
说明:
只对自动注入的sql起效:
插入: 不作限制
查找: 追加where条件过滤掉已删除数据,且使用 wrapper.entity 生成的where条件会忽略该字段
更新: 追加where条件防止更新到已删除数据,且使用 wrapper.entity 生成的where条件会忽略该字段
删除: 转变为 更新
在表中添加一个逻辑字段
测试
@Test
public void testDelete(){
int i = userMapper.deleteById(1);
System.out.println(i);
}
2.3 修改update
自动填充
(1)insertFill和 updateFill方法 该类为配置类 类上方加入注解 @ Configuration
@TableField(fill = FieldFill.INSERT)
private Date createTime;
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date updateTime;
(2创建一个自动配置类
//表示该类为配置类
@Configuration
public class MybatisPlusConfig implements MetaObjectHandler {
public static void main(String[] args) {
System.out.println(new Date());
}
@Override
//为哪个字段做自动填充
public void insertFill(MetaObject metaObject) {
this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
this.strictInsertFill(metaObject, "updateTime", Date.class, new Date());
}
@Override
public void updateFill(MetaObject metaObject) {
this.strictUpdateFill(metaObject, "updateTime", Date.class, new Date());
}
}
测试:
(1)增加:
@Test
public void testInser(){
User user = new User(null, "焦世超", 22, "110@qq.com", 0);
int insert = userMapper.insert(user);
System.out.println(insert);
}
(2) 修改
@Test
public void testUpdate(){
User user = new User(3L,"22222",17,"110@qq.com",0);
int i = userMapper.updateById(user);
}
2.4 条件查询
Wrapper:条件的包装类。QueryWrapper UpdateWrapper LambdaQueryWrapper LambdaUpdateWrapper
QueryWrapper<User> wrapper=new QueryWrapper<>();需要new QueryWrapper方法
@Test
public void testSelectByCondication(){
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.between("age",12,20);
List<User> users = userMapper.selectList(wrapper);
System.out.println(users);
}
2.5分页查询
1.引入分页插件
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
2.分页方法
@Test
public void testSelectByPage(){
/**
* Page:当前页码 每页显示的条数
*/
Page<User> page=new Page<>(2,2);
Page<User> page1 = userMapper.selectPage(page, null);
System.out.println("当前的总页码: "+page1.getPages());
System.out.println("总条数: "+page1.getTotal());
System.out.println("当前页的记录: "+page1.getRecords());
}