若依管理系统后端将 Mybatis 升级为 Mybatis-Plus

简介: 若依管理系统后端将 Mybatis 升级为 Mybatis-Plus

说明

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

流程

增加依赖

【首先修改父模块的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));
    }
}
目录
相关文章
|
2月前
|
Java 数据库连接 Maven
后端框架学习-----mybatis(使用mybatis框架遇到的问题)
这篇文章总结了在使用MyBatis框架时可能遇到的几个常见问题及其解决方法,包括配置文件注册、接口绑定、方法名匹配、返回类型匹配、Maven资源导出、时区设置和字符编码问题。
|
6天前
|
SQL XML Java
springboot整合mybatis-plus及mybatis-plus分页插件的使用
这篇文章介绍了如何在Spring Boot项目中整合MyBatis-Plus及其分页插件,包括依赖引入、配置文件编写、SQL表创建、Mapper层、Service层、Controller层的创建,以及分页插件的使用和数据展示HTML页面的编写。
springboot整合mybatis-plus及mybatis-plus分页插件的使用
|
2月前
|
Java 数据库连接 Spring
后端框架入门超详细 三部曲 Spring 、SpringMVC、Mybatis、SSM框架整合案例 【爆肝整理五万字】
文章是关于Spring、SpringMVC、Mybatis三个后端框架的超详细入门教程,包括基础知识讲解、代码案例及SSM框架整合的实战应用,旨在帮助读者全面理解并掌握这些框架的使用。
后端框架入门超详细 三部曲 Spring 、SpringMVC、Mybatis、SSM框架整合案例 【爆肝整理五万字】
|
2月前
|
安全 Java 数据库连接
后端框架的学习----mybatis框架(3、配置解析)
这篇文章详细介绍了MyBatis框架的核心配置文件解析,包括环境配置、属性配置、类型别名设置、映射器注册以及SqlSessionFactory和SqlSession的生命周期和作用域管理。
后端框架的学习----mybatis框架(3、配置解析)
|
2月前
|
Web App开发 前端开发 关系型数据库
基于SpringBoot+Vue+Redis+Mybatis的商城购物系统 【系统实现+系统源码+答辩PPT】
这篇文章介绍了一个基于SpringBoot+Vue+Redis+Mybatis技术栈开发的商城购物系统,包括系统功能、页面展示、前后端项目结构和核心代码,以及如何获取系统源码和答辩PPT的方法。
|
2月前
|
Java 数据库连接 数据库
后端框架学习-----mybatis(4)
这篇文章介绍了如何解决在使用MyBatis时数据库字段名和Java实体类属性名不一致的问题,通过使用`<resultMap>`元素来映射字段和属性,确保能够正确地查询和映射数据。
|
2月前
|
Java 数据库连接 mybatis
基于SpringBoot+MyBatis的餐饮点餐系统
本文介绍了一个基于SpringBoot和MyBatis开发的餐饮点餐系统,包括系统的主控制器`IndexController`的代码实现,该控制器负责处理首页、点餐、登录、注册、订单管理等功能,适用于毕业设计项目。
49 0
基于SpringBoot+MyBatis的餐饮点餐系统
|
2月前
|
Java 数据库连接 mybatis
后端框架的学习----mybatis框架(9、多对一处理和一对多处理)
这篇文章介绍了在MyBatis框架中如何处理多对一和一对多的关联查询,通过定义`<resultMap>`和使用`<association>`与`<collection>`元素来实现对象间的关联映射。
|
2月前
|
Java 数据库连接 测试技术
后端框架的学习----mybatis框架(8、lombok)
这篇文章介绍了如何在MyBatis框架中使用lombok库来简化Java实体类的编写,包括在IDEA中安装Lombok插件、在项目中导入lombok依赖以及在实体类上使用Lombok提供的注解。
|
2月前
|
Java 数据库连接 数据库
后端框架的学习----mybatis框架(6、日志)
这篇文章介绍了如何在MyBatis框架中使用日志功能,包括配置MyBatis的日志实现、使用log4j作为日志工具,以及如何通过配置文件控制日志级别和输出格式。
下一篇
无影云桌面