將Mybatis升級(jí)為Mybatis-Plus的詳細(xì)過程
說明
若依管理系統(tǒng)是一個(gè)非常完善的管理系統(tǒng)模板,里面含有代碼生成的方法,可以幫助用戶快速進(jìn)行開發(fā),但是項(xiàng)目使用的是mybatis,對于熟悉使用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ì)生成對應(yīng)的前端文件,這樣可以直接搭配使用,不需要再去修改生成的api

雖然若依生成器生成了Controller代碼,但是沒辦法直接將其進(jìn)行應(yīng)用,因?yàn)槲覀兦懊嫔傻腟ervice文件使用的是mybatis-plus,所以還需要對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的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Java實(shí)戰(zhàn)之基于swing的QQ郵件收發(fā)功能實(shí)現(xiàn)
這篇文章主要介紹了Java實(shí)戰(zhàn)之基于swing的QQ郵件收發(fā)功能實(shí)現(xiàn),文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)java的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04
Spring配置數(shù)據(jù)源的三種方式(小結(jié))
本文主要介紹了Spring配置數(shù)據(jù)源的三種方式,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
從log4j切換到logback后項(xiàng)目無法啟動(dòng)的問題及解決方法
這篇文章主要介紹了從log4j切換到logback后項(xiàng)目無法啟動(dòng)的問題及解決方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-01-01
JavaCV攝像頭實(shí)戰(zhàn)之實(shí)現(xiàn)口罩檢測
這篇文章主要介紹了利用JavaCV實(shí)現(xiàn)口罩檢測,功能是檢測攝像頭內(nèi)的人是否帶了口罩,把檢測結(jié)果實(shí)時(shí)標(biāo)注在預(yù)覽窗口。感興趣的可以試一試2022-01-01
springboot實(shí)現(xiàn)全局異常處理的方法(住家飯系統(tǒng))
住家飯系統(tǒng)將異常類型分為客戶端異常(ClientException),系統(tǒng)異常(ServiceException),遠(yuǎn)程調(diào)用異常(RemoteException),本文給大家介紹springboot實(shí)現(xiàn)全局異常處理的方法,感興趣的朋友一起看看吧2025-05-05
Java實(shí)戰(zhàn)練習(xí)之撲克牌魔術(shù)
這篇文章主要介紹了Java實(shí)戰(zhàn)練習(xí)之撲克牌魔術(shù),文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)java的小伙伴們有很好地幫助,需要的朋友可以參考下2021-04-04
idea sql的xml文件出現(xiàn)紅色警告符的處理方式
這篇文章主要介紹了idea sql的xml文件出現(xiàn)紅色警告符處理方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04
SpringBoot統(tǒng)一處理功能實(shí)現(xiàn)的全過程
最近在做項(xiàng)目時(shí)需要對異常進(jìn)行全局統(tǒng)一處理,主要是一些分類入庫以及記錄日志等,下面這篇文章主要給大家介紹了關(guān)于SpringBoot統(tǒng)一功能處理實(shí)現(xiàn)的相關(guān)資料,文中通過圖文以及實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-03-03

