【若依管理系统】如何将 Mybatis 升级为 Mybatis-Plus

本文指导如何在若依管理系统中将Mybatis升级为Mybatis-Plus,包括添加依赖、修改配置、处理代码生成和Controller整合,以提高开发效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


🌟场快订” 场馆预定平台是我个人匠心打造的全栈免费开源项目,使用 Spring Cloud + Uniapp 开发,包含高并发设计(缓存击穿、缓存穿透处理)、大数据量查询优化、分库分表、IP 流量管控、分布式事务、分布式 ID、幂等处理、WebSocket 双向通信、消息队列异步执行、延时队列等内容,此外还包括域名购买与解析项目打包上线HTTP升级HTTPS等手把手教程,项目代码简洁,部分代码使用设计模式重构,非常适用于学习后端技术、毕业设计、相关计算机竞赛,感兴趣的朋友可以从以下链接进行学习:

📦 源码仓库:https://gitee.com/HelloDam/venue-reservation
📚 技术专栏:https://blog.csdn.net/laodanqiu/category_12877044.html
📱 在线体验:https://hellodam.website(建议手机访问)

在这里插入图片描述


🎯 本文详细介绍了在若依管理系统(v3.8.8)中将MyBatis升级为MyBatis-Plus的过程,旨在提升开发效率。主要内容包括:修改父模块与子模块的pom.xml文件以引入MyBatis-Plus依赖,调整application.yml配置文件以适配新框架,注释掉原有MyBatis配置类中的Bean定义,以及结合IDEA插件和若依代码生成器生成兼容MyBatis-Plus的代码。通过本文,开发者可实现系统功能无损升级,同时享受MyBatis-Plus带来的便捷特性,如代码简化和性能优化。

📖说明📖

若依管理系统是一个非常完善的管理系统模板,里面含有代码生成的方法,可以帮助用户快速进行开发,但是项目使用的是mybatis,对于熟悉使用mybatis-plus进行开发的小伙伴们不是很便捷,本文主要讲解如何在不影响系统现有功能的基础上,将mybatis升级为mybatis-plus,以帮助小伙伴们更快速地开发。

我所使用的若依版本为:v3.8.8

💱流程💱

增加依赖

【首先修改父模块的pom.xml文件】
在这里插入图片描述

分别在properties标签内和dependencies标签内增加内容,所需增加的内容如下面的xml

    <properties>
        <mybatis-plus.version>3.2.0</mybatis-plus.version>
    </properties>

    <!-- 依赖声明 -->
    <dependencyManagement>
        <dependencies>

            <!-- mybatis-plus -->
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-boot-starter</artifactId>
                <version>${mybatis-plus.version}</version>
            </dependency>

        </dependencies>
    </dependencyManagement>

【其次修改common包下的pom.xml文件】
在这里插入图片描述

直接在dependencies标签内增加如下内容即可,因为父模块已经管理了版本,这里不需要再声明版本

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

修改配置文件

需要修改admin包下的application.yml文件

在这里插入图片描述
在这里插入图片描述
注释掉mybatis的配置之后(mybatis-plus是兼容mybatis的,小伙伴们放心注释就行),增加mybatis-plus的配置,配置如下

mybatis-plus:
  mapper-locations: classpath*:mapper/**/*Mapper.xml
  type-aliases-package: com.ruoyi.**.domain
  global-config:
    db-config:
      id-type: auto
  configuration:
    map-underscore-to-camel-case: true
    cache-enabled: false
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

注释掉MybatisConfig里面的Bean

系统会自动根据配置文件构建mybatisplus相关的Bean,所以这里也不需要修改成mybatis-plus的

在这里插入图片描述
修改之后记得重新 install framework包,这样修改才会生效

在这里插入图片描述

🧑‍💻代码生成🧑‍💻

上面的操作虽然是将mybatis-plus引入了,但是有小伙伴疑问了,使用若依管理系统自带的代码生成器,生成的还是mybatis的代码呀,那怎么办呢?

为了解决小伙伴们心中的疑惑,我这里提供一种解决思路,那就是结合IDEA生成的代码和若依代码生成器的代码

使用IDEA生成代码

首先安装下面的插件,记得安装1.5.5版本的插件,高版本的插件可能会有问题(我之前是安装的高版本,出问题之后才回退的),具体安装可以去阅读其他博主的教程

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

生成成功,由下图可知,除了controller外,其他代码都已经生成成功

在这里插入图片描述

注意

使用MybatisX生成的实体类是没有逻辑删除等注解的,如果需要使用逻辑删除,或者自动填充ID和时间,需要在实体类上面添加注解

Controller文件

Controller文件直接使用若依的代码生成器生成即可,使用这种方式生成的好处是,若依会生成对应的前端文件,这样可以直接搭配使用,不需要再去修改生成的api

在这里插入图片描述

虽然若依生成器生成了Controller代码,但是没办法直接将其进行应用,因为我们前面生成的Service文件使用的是mybatis-plus,所以还需要对Controller文件进行修改,修改的方式可以参考我下面的代码,当然这个代码还是非常不完善的,比如数据校验那些都没有

package com.shm.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.entity.Follow;
import com.ruoyi.common.core.domain.model.LoginUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.shm.service.IFollowService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 关注Controller
 *
 * @author dam
 * @date 2023-08-08
 */
@RestController
@RequestMapping("/market/follow")
public class FollowController extends BaseController {
    @Autowired
    private IFollowService followService;

    /**
     * 查询关注列表
     */
    @PreAuthorize("@ss.hasPermi('shm:follow:list')")
    @GetMapping("/list")
    public TableDataInfo list(Follow follow) {
        startPage();
        List<Follow> list = followService.list(new QueryWrapper<Follow>(follow));
        return getDataTable(list);
    }

    /**
     * 导出关注列表
     */
    @PreAuthorize("@ss.hasPermi('shm:follow:export')")
    @Log(title = "关注", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Follow follow) {
        List<Follow> list = followService.list(new QueryWrapper<Follow>(follow));
        ExcelUtil<Follow> util = new ExcelUtil<Follow>(Follow.class);
        util.exportExcel(response, list, "关注数据");
    }

    /**
     * 获取关注详细信息
     */
    @PreAuthorize("@ss.hasPermi('shm:follow:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(followService.getById(id));
    }

    /**
     * 新增关注
     */
    @PreAuthorize("@ss.hasPermi('shm:follow:add')")
    @Log(title = "关注", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Follow follow) {
        // 设置商品主人
        LoginUser loginUser = getLoginUser();
        follow.setFollowerId(loginUser.getUserId());
        return toAjax(followService.save(follow));
    }

    /**
     * 修改关注
     */
    @PreAuthorize("@ss.hasPermi('shm:follow:edit')")
    @Log(title = "关注", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Follow follow) {
        return toAjax(followService.updateById(follow));
    }

    /**
     * 删除关注
     */
    @PreAuthorize("@ss.hasPermi('shm:follow:remove')")
    @Log(title = "关注", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(followService.removeByIds(ids));
    }
}

### 若依项目从MyBatis迁移到MyBatis-Plus教程 #### 修改依赖项 为了将若依项目的MyBatis替换为MyBatis-Plus,首先需要更新`pom.xml`文件中的依赖关系。移除旧版本的MyBatis依赖,并引入新的MyBatis-Plus依赖。 ```xml <!-- 移除 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>x.x.x</version> </dependency> <!-- 添加 --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>y.y.y</version> </dependency> ``` #### 配置调整 接着,在application.yml或properties配置文件里做相应改动来适应新框架的要求[^1]: ```yaml # application.yml mybatis-plus: mapper-locations: classpath*:mapper/*.xml typeAliasesPackage: com.ruoyi.project.system.domain ``` #### Mapper接口改造 原本基于XML定义SQL语句的方式可以简化成继承自`BaseMapper<T>`的形式,从而减少大量重复代码的工作量。例如原生的UserDao.java可变为如下形式[^3]: ```java package com.ruoyi.project.system.mapper; import org.apache.ibatis.annotations.Mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.ruoyi.project.system.domain.SysUser; @Mapper public interface SysUserMapper extends BaseMapper<SysUser> { } ``` #### 处理分页插件冲突 由于PageHelper是专门为MyBatis设计的第三方组件,当两者共存时可能会引发不兼容的情况。建议采用内置于MyBatis-Plus内的分页解决方案——`PaginationInterceptor`替代之。 ```java @Configuration @EnableTransactionManagement @MapperScan("com.ruoyi.project.*.mapper") public class MyBatisConfig { @Bean public PaginationInterceptor paginationInterceptor() { return new PaginationInterceptor(); } } ``` #### 测试验证 完成上述更改之后进行全面的功能测试以确认迁移过程未破坏现有业务逻辑;同时注意观察日志输出确保所有查询均能正常执行。
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HelloDam

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值