当前位置: 首页 > news >正文

【实操】Mybatis-plus2.x升级到3.x

第一步:修改pom.xml文件

  <dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>2.2.0</version></dependency>

修改为

        <dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.5.5</version></dependency>

第二步:更改引用的文件路径

2.x引入的路径如下

import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.service.IService;
import com.baomidou.mybatisplus.enums.IdType;
import com.baomidou.mybatisplus.annotations.TableId;
import com.baomidou.mybatisplus.annotations.TableField;
import com.baomidou.mybatisplus.annotations.TableName;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import com.baomidou.mybatisplus.enums.FieldFill;
import com.baomidou.dynamic.datasource.DS;

需要修改为,和上方一一对应进行全局修改即可

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.enums.SqlLike;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.dynamic.datasource.annotation.DS;

第三步:修改分页插件配置

在2.x中分页插件配置大概如下所示

    /*** 分页插件*/@Beanpublic PaginationInterceptor paginationInterceptor() {return new PaginationInterceptor();}

修改为如下

   @Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();// 分页插件interceptor.addInnerInterceptor(paginationInnerInterceptor());// 乐观锁插件interceptor.addInnerInterceptor(optimisticLockerInnerInterceptor());return interceptor;}/*** 分页插件,自动识别数据库类型*/public PaginationInnerInterceptor paginationInnerInterceptor() {PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();// 分页合理化paginationInnerInterceptor.setOverflow(true);return paginationInnerInterceptor;}/*** 乐观锁插件*/public OptimisticLockerInnerInterceptor optimisticLockerInnerInterceptor() {return new OptimisticLockerInnerInterceptor();}

第四步:代码中Wrapper修改

需要对EntityWrapper 根据需要修改为QueryWrapper UpdateWrapper

第五步:修改方法

selectPage改为page
insert改为save
selectById改为getById
selectList改为list
setSqlSelect改为select
selectOne改为getOne
selectCount改为count 返回修改为Long类型
deleteById改为removeById
delete改为remove
selectObjs改为listObjs
andNew()改为.and(QueryWrapper->QueryWrapper.)
selectBatchIds改为listByIds
selectMap改为getMap
selectByMap改为listByMap
selectById改为getById
deleteBatchIds改为removeBatchByIds
deleteByMap改为removeByMap
deleteById改为removeById
insertOrUpdate改为saveOrUpdate
insertBatch改为saveBatch
selectMaps改为listMaps
insertOrUpdateBatch改为saveOrUpdateBatch
where改为apply
addFilterIfNeed改为apply
类似or(StringUtils.isNotBlank(deptId), "dept_id in (" + deptId + ")"))改为.or(StringUtils.isNotBlank(deptId), wrapper -> wrapper.apply("dept_id in (" + deptId + ")")))orderBy需要根据需要修改为orderByAsc、orderByDesc,需要单独处理,无法批量修改
orderBy("xxx", false)改为orderByDesc("xxx")
orderBy("xxx", true)改为orderByAsc("xxx")Pagination page改为Page page
IdType.UUID改为IdType.ASSIGN_UUID

第六步:修改关键字

在 MyBatis Plus 3.x 中,不再提供自动识别关键字进行处理的功能。处理数据库关键字的方法有以下几种:

  1. 不同数据库对关键字的处理方式不同,因此很难维护。在数据库设计时,建议避免使用关键字作为字段名或表名。

  2. 如果必须使用关键字,可以通过在字段或表名前后添加反引号(`)来进行处理【引号用的主键区第一排第一个按键(tab键上面的按键)】,如下所示:

@TableField(value = "`status`")
private Boolean status;

还有就是@TableId一个实体类中不能存在多个,不然会报错。

第七步:去除banner

配置如下

mybatis-plus:global-config:banner: false

第八步:调整数据库连接配置

2.x配置

spring:datasource:type: com.alibaba.druid.pool.DruidDataSourcedynamic:master:type: ${spring.datasource.type}driverClassName: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/demo?serverTimezone=GMT%2B8&characterEncoding=utf8&useSSL=false&allowMultiQueries=trueusername: rootpassword: root

调整为

spring:datasource:type: com.alibaba.druid.pool.DruidDataSourcedynamic:primary: masterdatasource:master:type: ${spring.datasource.type}driverClassName: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/demo?serverTimezone=GMT%2B8&characterEncoding=utf8&useSSL=false&allowMultiQueries=trueusername: rootpassword: root

 

 


http://www.mrgr.cn/news/95367.html

相关文章:

  • 蓝桥杯 之 数论
  • Halcon算子 二维码识别、案例
  • 对敏捷研发的反思,是否真是灵丹妙药?
  • STM32八股【1】-----启动流程和startup文件理解
  • 『 C++ 』线程与原子操作:高效并发编程的利器
  • 深度解读DeepSeek:源码解读 DeepSeek-V3
  • STM32八股【2】-----ARM架构
  • 面试康复训练-SQL语句
  • 如何为在线游戏选择合适的游戏盾?
  • 【数据结构】栈(Stack)、队列(Queue)、双端队列(Deque) —— 有码有图有真相
  • Maven安装与环境配置
  • 经典笔试题 小于 n 的最大整数 贪心 回溯 剪枝 全排列
  • 【yolo】使用 Netron 可视化深度学习模型:从 YOLOv1 到 YOLOv8 的探索
  • 【C++11】左值引用、右值引用、移动语义和完美转发
  • CentOS 7 64位安装Docker
  • 【UI设计】一些好用的免费图标素材网站
  • 【Agent】Dify Docker 安装问题 INTERNAL SERVER ERROR
  • sgpt 终端使用指南
  • 西门子200smart之modbus_TCP(做主站与第三方设备)通讯
  • Mysql表的增删改查