将Mybatis升级为Mybatis-Plus的详细过程

2025-04-04 15:50

本文主要是介绍将Mybatis升级为Mybatis-Plus的详细过程,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

《将Mybatis升级为Mybatis-Plus的详细过程》本文详细介绍了在若依管理系统(v3.8.8)中将MyBatis升级为MyBatis-Plus的过程,旨在提升开发效率,通过本文,开发者可实现...

说明

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

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

流程

增加依赖

【首先修改父模块的pom.XML文件】

将Mybatis升级为Mybatis-Plus的详细过程

分别在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文件】

将Mybatis升级为Mybatis-Plus的详细过程

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

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

修改配置文件

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

将Mybatis升级为Mybatis-Plus的详细过程

将Mybatis升级为Mybatis-Plus的详细过程

注释掉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的

将Mybatis升级为Mybatis-Plus的详细过程

修改之后记得重新 install framework包,这样修改才会生效

将Mybatis升级为Mybatis-Plus的详细过程

代码生成

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

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

使用IDEA生成代码

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

将Mybatis升级为Mybatis-Plus的详细过程

将Mybatis升级为Mybatis-Plus的详细过程

将Mybatis升级为Mybatis-Plus的详细过程

将Mybatis升级为Mybatis-Plus的详细过程

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

将Mybatis升级为Mybatis-Plus的详细过程

注意

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

Controller文件

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

编程

将Mybatis升级为Mybatis-Plus的详细过程

虽然若依生成器生成了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.DeleteMajspping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springSsQHfpRXpframework.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.China编程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的资料请关注编程China编程(www.chinasem.cn)其它相关文章!

这篇关于将Mybatis升级为Mybatis-Plus的详细过程的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1154099

相关文章

IntelliJ IDEA 中配置 Spring MVC 环境的详细步骤及问题解决

《IntelliJIDEA中配置SpringMVC环境的详细步骤及问题解决》:本文主要介绍IntelliJIDEA中配置SpringMVC环境的详细步骤及问题解决,本文分步骤结合实例给大... 目录步骤 1:创建 Maven Web 项目步骤 2:添加 Spring MVC 依赖1、保存后执行2、将新的依赖

如何为Yarn配置国内源的详细教程

《如何为Yarn配置国内源的详细教程》在使用Yarn进行项目开发时,由于网络原因,直接使用官方源可能会导致下载速度慢或连接失败,配置国内源可以显著提高包的下载速度和稳定性,本文将详细介绍如何为Yarn... 目录一、查询当前使用的镜像源二、设置国内源1. 设置为淘宝镜像源2. 设置为其他国内源三、还原为官方

最详细安装 PostgreSQL方法及常见问题解决

《最详细安装PostgreSQL方法及常见问题解决》:本文主要介绍最详细安装PostgreSQL方法及常见问题解决,介绍了在Windows系统上安装PostgreSQL及Linux系统上安装Po... 目录一、在 Windows 系统上安装 PostgreSQL1. 下载 PostgreSQL 安装包2.

MySql match against工具详细用法

《MySqlmatchagainst工具详细用法》在MySQL中,MATCH……AGAINST是全文索引(Full-Textindex)的查询语法,它允许你对文本进行高效的全文搜素,支持自然语言搜... 目录一、全文索引的基本概念二、创建全文索引三、自然语言搜索四、布尔搜索五、相关性排序六、全文索引的限制七

python中各种常见文件的读写操作与类型转换详细指南

《python中各种常见文件的读写操作与类型转换详细指南》这篇文章主要为大家详细介绍了python中各种常见文件(txt,xls,csv,sql,二进制文件)的读写操作与类型转换,感兴趣的小伙伴可以跟... 目录1.文件txt读写标准用法1.1写入文件1.2读取文件2. 二进制文件读取3. 大文件读取3.1

Linux内核参数配置与验证详细指南

《Linux内核参数配置与验证详细指南》在Linux系统运维和性能优化中,内核参数(sysctl)的配置至关重要,本文主要来聊聊如何配置与验证这些Linux内核参数,希望对大家有一定的帮助... 目录1. 引言2. 内核参数的作用3. 如何设置内核参数3.1 临时设置(重启失效)3.2 永久设置(重启仍生效

如何在Mac上安装并配置JDK环境变量详细步骤

《如何在Mac上安装并配置JDK环境变量详细步骤》:本文主要介绍如何在Mac上安装并配置JDK环境变量详细步骤,包括下载JDK、安装JDK、配置环境变量、验证JDK配置以及可选地设置PowerSh... 目录步骤 1:下载JDK步骤 2:安装JDK步骤 3:配置环境变量1. 编辑~/.zshrc(对于zsh

使用Node.js制作图片上传服务的详细教程

《使用Node.js制作图片上传服务的详细教程》在现代Web应用开发中,图片上传是一项常见且重要的功能,借助Node.js强大的生态系统,我们可以轻松搭建高效的图片上传服务,本文将深入探讨如何使用No... 目录准备工作搭建 Express 服务器配置 multer 进行图片上传处理图片上传请求完整代码示例

C++ vector的常见用法超详细讲解

《C++vector的常见用法超详细讲解》:本文主要介绍C++vector的常见用法,包括C++中vector容器的定义、初始化方法、访问元素、常用函数及其时间复杂度,通过代码介绍的非常详细,... 目录1、vector的定义2、vector常用初始化方法1、使编程用花括号直接赋值2、使用圆括号赋值3、ve

PyInstaller打包selenium-wire过程中常见问题和解决指南

《PyInstaller打包selenium-wire过程中常见问题和解决指南》常用的打包工具PyInstaller能将Python项目打包成单个可执行文件,但也会因为兼容性问题和路径管理而出现各种运... 目录前言1. 背景2. 可能遇到的问题概述3. PyInstaller 打包步骤及参数配置4. 依赖