將Mybatis升級(jí)為Mybatis-Plus的詳細(xì)過程
說明
若依管理系統(tǒng)是一個(gè)非常完善的管理系統(tǒng)模板,里面含有代碼生成的方法,可以幫助用戶快速進(jìn)行開發(fā),但是項(xiàng)目使用的是mybatis,對(duì)于熟悉使用mybatis-plus進(jìn)行開發(fā)的小伙伴們不是很便捷,本文主要講解如何在不影響系統(tǒng)現(xiàn)有功能的基礎(chǔ)上,將mybatis升級(jí)為mybatis-plus,以幫助小伙伴們更快速地開發(fā)。
我所使用的若依版本為:v3.8.8
流程
增加依賴
【首先修改父模塊的pom.xml文件】

分別在properties標(biāo)簽內(nèi)和dependencies標(biāo)簽內(nèi)增加內(nèi)容,所需增加的內(nèi)容如下面的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標(biāo)簽內(nèi)增加如下內(nèi)容即可,因?yàn)楦改K已經(jīng)管理了版本,這里不需要再聲明版本
<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
系統(tǒng)會(huì)自動(dòng)根據(jù)配置文件構(gòu)建mybatisplus相關(guān)的Bean,所以這里也不需要修改成mybatis-plus的

修改之后記得重新 install framework包,這樣修改才會(huì)生效

代碼生成
上面的操作雖然是將mybatis-plus引入了,但是有小伙伴疑問了,使用若依管理系統(tǒng)自帶的代碼生成器,生成的還是mybatis的代碼呀,那怎么辦呢?
為了解決小伙伴們心中的疑惑,我這里提供一種解決思路,那就是結(jié)合IDEA生成的代碼和若依代碼生成器的代碼。
使用IDEA生成代碼
首先安裝下面的插件,記得安裝1.5.5版本的插件,高版本的插件可能會(huì)有問題(我之前是安裝的高版本,出問題之后才回退的),具體安裝可以去閱讀其他博主的教程




生成成功,由下圖可知,除了controller外,其他代碼都已經(jīng)生成成功

注意
使用MybatisX生成的實(shí)體類是沒有邏輯刪除等注解的,如果需要使用邏輯刪除,或者自動(dòng)填充ID和時(shí)間,需要在實(shí)體類上面添加注解
Controller文件
Controller文件直接使用若依的代碼生成器生成即可,使用這種方式生成的好處是,若依會(huì)生成對(duì)應(yīng)的前端文件,這樣可以直接搭配使用,不需要再去修改生成的api

雖然若依生成器生成了Controller代碼,但是沒辦法直接將其進(jìn)行應(yīng)用,因?yàn)槲覀兦懊嫔傻腟ervice文件使用的是mybatis-plus,所以還需要對(duì)Controller文件進(jìn)行修改,修改的方式可以參考我下面的代碼,當(dāng)然這個(gè)代碼還是非常不完善的,比如數(shù)據(jù)校驗(yàn)?zāi)切┒紱]有
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;
/**
* 關(guān)注Controller
*
* @author dam
* @date 2023-08-08
*/
@RestController
@RequestMapping("/market/follow")
public class FollowController extends BaseController {
@Autowired
private IFollowService followService;
/**
* 查詢關(guān)注列表
*/
@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);
}
/**
* 導(dǎo)出關(guān)注列表
*/
@PreAuthorize("@ss.hasPermi('shm:follow:export')")
@Log(title = "關(guān)注", 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, "關(guān)注數(shù)據(jù)");
}
/**
* 獲取關(guān)注詳細(xì)信息
*/
@PreAuthorize("@ss.hasPermi('shm:follow:query')")
@GetMapping(value = "/{id}")
public AjaxResult getInfo(@PathVariable("id") Long id) {
return success(followService.getById(id));
}
/**
* 新增關(guān)注
*/
@PreAuthorize("@ss.hasPermi('shm:follow:add')")
@Log(title = "關(guān)注", businessType = BusinessType.INSERT)
@PostMapping
public AjaxResult add(@RequestBody Follow follow) {
// 設(shè)置商品主人
LoginUser loginUser = getLoginUser();
follow.setFollowerId(loginUser.getUserId());
return toAjax(followService.save(follow));
}
/**
* 修改關(guān)注
*/
@PreAuthorize("@ss.hasPermi('shm:follow:edit')")
@Log(title = "關(guān)注", businessType = BusinessType.UPDATE)
@PutMapping
public AjaxResult edit(@RequestBody Follow follow) {
return toAjax(followService.updateById(follow));
}
/**
* 刪除關(guān)注
*/
@PreAuthorize("@ss.hasPermi('shm:follow:remove')")
@Log(title = "關(guān)注", businessType = BusinessType.DELETE)
@DeleteMapping("/{ids}")
public AjaxResult remove(@PathVariable List<Long> ids) {
return toAjax(followService.removeByIds(ids));
}
}
以上就是將Mybatis升級(jí)為Mybatis-Plus的詳細(xì)過程的詳細(xì)內(nèi)容,更多關(guān)于Mybatis升級(jí)為Mybatis-Plus的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
淺談SpringBoot是如何實(shí)現(xiàn)日志的
這篇文章主要介紹了淺談SpringBoot是如何實(shí)現(xiàn)日志的,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
IDEA在一個(gè)工作空間中管理多個(gè)項(xiàng)目的詳細(xì)步驟
這篇文章主要介紹了IDEA在一個(gè)工作空間中管理多個(gè)項(xiàng)目的詳細(xì)步驟,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
springboot多環(huán)境配置文件及自定義配置文件路徑詳解
這篇文章主要介紹了springboot多環(huán)境配置文件及自定義配置文件路徑,文中給大家介紹了classpath的基本概念講解及自定義springboot配置文件路徑的相關(guān)知識(shí),需要的朋友可以參考下2023-02-02
Java Swing組件實(shí)現(xiàn)進(jìn)度監(jiān)視功能示例
這篇文章主要介紹了Java Swing組件實(shí)現(xiàn)進(jìn)度監(jiān)視功能,結(jié)合完整實(shí)例形式詳細(xì)分析了Java基于Swing組件實(shí)現(xiàn)進(jìn)度條顯示功能的具體操作技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2018-02-02
spring boot項(xiàng)目導(dǎo)入依賴后代碼報(bào)錯(cuò)問題的解決方法
這篇文章主要給大家介紹了關(guān)于spring boot項(xiàng)目導(dǎo)入依賴后代碼報(bào)錯(cuò)問題的解決方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用spring Boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
Spring Bean創(chuàng)建和循環(huán)依賴
這篇文章主要介紹了Spring Bean創(chuàng)建和循環(huán)依賴,講述了Spring容器中?Bean?的創(chuàng)建過程已經(jīng)主要的方法,另外也著重分析了循環(huán)依賴的問題,需要的小伙伴可以參考一下2022-05-05
SpringSecurity構(gòu)建基于JWT的登錄認(rèn)證實(shí)現(xiàn)
這篇文章主要介紹了SpringSecurity構(gòu)建基于JWT的登錄認(rèn)證實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
Spring?Security權(quán)限管理小結(jié)
SpringSecurity是一個(gè)權(quán)限管理框架,核心是認(rèn)證和授權(quán),前面已經(jīng)系統(tǒng)的給大家介紹過了認(rèn)證的實(shí)現(xiàn)和源碼分析,本文重點(diǎn)來介紹下權(quán)限管理,需要的朋友可以參考下2022-08-08
Spring中@Autowired自動(dòng)注入map詳解
這篇文章主要介紹了Spring中@Autowired自動(dòng)注入map詳解, spring是支持基于接口實(shí)現(xiàn)類的直接注入的,支持注入map,list等集合中,不用做其他的配置,直接注入,需要的朋友可以參考下2023-10-10

