MyBatis-Plus快速使用

news/2025/2/11 3:34:45/

1.介绍

MyBatis-Plus 是一个基于 MyBatis 的增强工具,旨在简化开发,提高效率。它在 MyBatis 的基础上进行扩展,只做增强不做改变,不会对现有的 MyBatis 构架产生任何影响。

特性和优势

  1. 无侵入性:MyBatis-Plus 在 MyBatis 的基础上进行扩展,只做增强不做改变,引入 MyBatis-Plus 不会对现有的 MyBatis 构架产生任何影响。

  2. 依赖少:仅仅依赖 MyBatis 以及 MyBatis-Spring。

  3. 性能损耗小:启动即会自动注入基本 CRUD,性能基本无损耗。

  4. 通用 CRUD 操作:内置通用 Mapper、通用 Service,仅需少量配置即可实现单表大部分 CRUD 操作。

  5. 多种主键策略:支持多达 4 种主键策略(内含分布式唯一 ID 生成器),可自由配置。

  6. 支持 ActiveRecord:支持 ActiveRecord 形式调用,实体类只需继承 Model 类即可实现基本 CRUD 操作。

  7. 代码生成:采用代码或者 Maven 插件可快速生成 Mapper、Model、Service、Controller 层代码。

  8. 内置分页插件:基于 MyBatis 物理分页,开发者无需关心具体操作。

  9. 内置性能分析插件:可输出 SQL 语句及其执行时间,建议开发测试时启用。

  10. 内置全局拦截插件:提供全表 delete、update 操作智能分析阻断,预防误操作。

2.快速使用

1.创建spingboot工程导入依赖

<dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-spring-boot3-starter</artifactId><version>3.5.10.1</version>
</dependency>

 2.修改配置文件

#数据源
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/day1216
spring.datasource.username=root
spring.datasource.password=123456#日志
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

3.创建实体类

@Data
@TableName(value = "user")//如果类名与表名不同,使用该主机表示对应的表名
public class User {@TableId(value = "id")//该属性为主键列private Integer id;@TableField(value = "name")//普通属性与列名不一致时private String name;private Integer age;private String email;
}

创建mapper接口 

@Mapper//为该接口生成代理对象
public interface UserMapper extends BaseMapper<User> {
}

测试

@SpringBootTest
class DemoMp01ApplicationTests {@Autowiredprivate UserMapper userMapper;@Testvoid contextLoads() {User user = userMapper.selectById(1);System.out.println(user);}}

3.主键生成策略 

@TableId(value = "id",type = IdType.ASSIGN_ID)
  • IdType.AUTO:使用数据库自增 ID 作为主键。
  • IdType.NONE:无特定生成策略,如果全局配置中有 IdType 相关的配置,则会跟随全局配置。
  • IdType.INPUT:在插入数据前,由用户自行设置主键值。
  • IdType.ASSIGN_ID(默认):自动分配 ID,适用于 LongIntegerString 类型的主键。默认使用雪花算法通过 IdentifierGenerator 的 nextId 实现。@since 3.3.0
    • 分布式集群项目
  • IdType.ASSIGN_UUID:自动分配 UUID,适用于 String 类型的主键。默认实现为 IdentifierGenerator 的 nextUUID 方法。@since 3.3.0
    •        分布式        要求id必须是varchar类型

雪花算法(Snowflake Algorithm)是一种在分布式系统中生成唯一ID的方法

雪花算法的优点包括:

  • 在高并发的分布式系统中,能够保证ID的唯一性。

  • 基于时间戳,ID基本上是有序递增的。

  • 不依赖于第三方库或中间件,减少了系统复杂性。

  • 生成ID的效率非常高。

4.逻辑删除

  • 删除update user set deleted=1 where id = 1 and deleted=0
  • 查找select id,name,deleted from user where deleted=0

1.在数据库中增加一列逻辑列

2.创建实体类时,增加该列属性

3.在属性上加@TableLogic,表示是逻辑删除列

//批量删除
List<Integer> ids=new ArrayList<>();
ids.add(1);
ids.add(2);
ids.add(3);
ids.add(4);
userMapper.deleteBatchIds(ids);

5.修改时间,添加时间

1.在数据库里添加        修改时间列update_time,添加时间列create_time

2.修改实体类

@TableField(fill = FieldFill.INSERT)
private Date createTime;
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date updateTime;
@TableLogic//逻辑删除列
@TableField(fill = FieldFill.INSERT)
private Integer deleted;

3.实现MetaObjectHandler

@Component
public class MyMetaObjectHandler implements MetaObjectHandler {@Overridepublic void insertFill(MetaObject metaObject) {this.strictInsertFill(metaObject, "createTime", Date.class, new Date());this.strictUpdateFill(metaObject, "updateTime",  Date.class, new Date());this.strictInsertFill(metaObject, "deleted", Integer.class,0);}@Overridepublic void updateFill(MetaObject metaObject) {this.strictUpdateFill(metaObject, "updateTime",  Date.class, new Date());}
}

6.条件查询

@Test
public void testFind(){//Wrapper:mp把添加封装成该接口   查询条件QueryWrapper<User> queryWrapper=new QueryWrapper<>();queryWrapper.like("name", "小");//or,不使用默认andqueryWrapper.or();//>=queryWrapper.ge("age",19);List<User> list = userMapper.selectList(queryWrapper);System.out.println(list);}

 7.分页

分页配置类

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class MybatisPlusConfig {/*** 添加分页插件*/@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); // 如果配置多个插件, 切记分页最后添加// 如果有多数据源可以不配具体类型, 否则都建议配上具体的 DbTypereturn interceptor;}
}

 加依赖,版本与mybatis-plus对应

<dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-jsqlparser</artifactId><version>3.5.10.1</version>
</dependency>

 测试

    @Testpublic void testPage(){
//        P page,分页对象
//        @Param("ew") Wrapper<T> queryWrapper  条件对象IPage<User> page=new Page<>(2,3);userMapper.selectPage(page,null);System.out.println("总条数"+page.getTotal());System.out.println("总页码"+page.getPages());System.out.println("当前页的记录"+page.getRecords());}

 8.联表分页

private Integer did;//部门对象,如果属性在数据库中不存在应该忽略该属性
@TableField(exist = false)
private Dept dept;
@Data
@TableName(value = "tbl_dept")
@AllArgsConstructor
@NoArgsConstructor
public class Dept {@TableIdprivate Integer id;@TableField(value = "dept_name")private String deptName;@TableField(value = "dept_loc")private String loc;
}

public Page<User> findAll(Page page, @Param("ew") Wrapper<User> queryWrapper);

UserMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ghx.mapper.UserMapper"><resultMap id="baseMapper" type="com.ghx.entity.User" autoMapping="true"><id column="id" property="id"/><association property="dept" javaType="com.ghx.entity.Dept"><id column="dept_id" property="id"/><result column="dept_name" property="deptName"/><result column="dept_loc" property="loc"/></association></resultMap><select id="findAll" resultMap="baseMapper">select * from user u join tbl_dept d on u.did=d.dept_id where deleted=0<if test="ew != null and ew.sqlSegment != ''">AND ${ew.sqlSegment}</if></select>
</mapper>

 测试

    @Testpublic void testPage2(){Page<User> page=new Page<>(2,3);QueryWrapper<User> queryWrapper=new QueryWrapper<>();
//        queryWrapper.ge("age",20);userMapper.findAll(page,queryWrapper);System.out.println("总条数"+page.getTotal());System.out.println("总页数"+page.getPages());System.out.println("当前页的记录"+page.getRecords());}


http://www.ppmy.cn/news/1571042.html

相关文章

洛谷 P2095 营养膳食 C语言

P2095 营养膳食 - 洛谷 | 计算机科学教育新生态 题目描述 Mr.L 正在完成自己的增肥计划。 为了增肥&#xff0c;Mr.L 希望吃到更多的脂肪&#xff0c;然而也不能只吃高脂肪食品&#xff0c;那样的话就会导致缺少其他营养。 Mr.L 通过研究发现&#xff1a;真正的营养膳食规定…

C# ASP.NET程序与 Web services 编程介绍

.NET学习资料 .NET学习资料 .NET学习资料 在当今的软件开发领域&#xff0c;C# 和ASP.NET是构建强大而灵活的 Web 应用程序的重要工具&#xff0c;而 Web services 则为不同应用程序之间的通信和交互提供了便利。下面将详细介绍 C# ASP.NET程序与 Web services 编程。 一、C…

基于 Ollama+Docker+OpenWebUI 的本地化部署deepseek流程

搭建deepseek 安装Ollama Ollama官方下载地址 下载完成后双击打开Ollama进行安装,点击install 安装完成后系统会弹出下图提示代表安装成功并且已启动 验证安装 ollama -v安装完成后&#xff0c;cmd 打开命令行窗口&#xff0c;输入 “ollama -v” 测试&#xff0c;显示 olla…

网络爬虫js逆向之异步栈跟栈案例

【注意&#xff01;&#xff01;&#xff01;】 前言&#xff1a; 1. 本章主要讲解js逆向之异步栈跟栈的知识&#xff08;通过单步执行调试&#xff09; 2. 使用关键字搜定位加密入口 3. 本专栏通过多篇文章【文字案例】的形式系统化进行描述 4. 本文章全文进行了脱敏处理 5. 详…

动手写ORM框架 - GeeORM第一天 database/sql 基础

文章目录 1 初识 SQLite2 database/sql 标准库3 实现一个简单的 log 库4 核心结构 Session本文是7天用Go从零实现ORM框架GeeORM的第一篇。介绍了 SQLite 的基础操作(连接数据库,创建表、增删记录等)。使用 Go 语言标准库 database/sql 连接并操作 SQLite 数据库,并简单封装…

deepseek接入pycharm 进行AI编程

要将DeepSeek接入PyCharm进行AI编程,可以按照以下步骤操作: ### 1. 获取DeepSeek API访问权限 DeepSeek通常以API的形式对外提供服务,你需要在其官方网站注册账号,申请API访问权限。在申请通过后,会获得API密钥(API Key),这是后续调用API的关键凭证。 ### 2. 安装必要…

【C++学习篇】C++11

目录 ​编辑 1. 初始化列表{} 1.1 C98中的{} 1.2 C11中的{} 2. C11中的std::initializer_list 3. 右值引用和移动语义 3.1 左值和右值 3.2 左值引用和右值引用 3.3 引用延长生命周期 3.4 左值和右值的参数匹配 3.5 右值引⽤和移动语义的使⽤场景 3.5.1 左值引⽤…

npm无法加载文件 因为此系统禁止运行脚本

安装nodejs后遇到问题&#xff1a; 在项目里【node -v】可以打印出来&#xff0c;【npm -v】打印不出来&#xff0c;显示npm无法加载文件 因为此系统禁止运行脚本。 但是在winr&#xff0c;cmd里【node -v】,【npm -v】都也可打印出来。 解决方法&#xff1a; cmd里可以打印出…