將Mybatis升級為Mybatis-Plus的詳細過程
說明
若依管理系統(tǒng)是一個非常完善的管理系統(tǒng)模板,里面含有代碼生成的方法,可以幫助用戶快速進行開發(fā),但是項目使用的是mybatis
,對于熟悉使用mybatis-plus
進行開發(fā)的小伙伴們不是很便捷,本文主要講解如何在不影響系統(tǒng)現有功能的基礎上,將mybatis
升級為mybatis-plus
,以幫助小伙伴們更快速地開發(fā)。
我所使用的若依版本為: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
系統(tǒng)會自動根據配置文件構建mybatisplus相關的Bean,所以這里也不需要修改成mybatis-plus的
修改之后記得重新 install framework包
,這樣修改才會生效
代碼生成
上面的操作雖然是將mybatis-plus
引入了,但是有小伙伴疑問了,使用若依管理系統(tǒng)自帶的代碼生成器,生成的還是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的資料請關注腳本之家其它相關文章!
相關文章
springboot多環(huán)境配置文件及自定義配置文件路徑詳解
這篇文章主要介紹了springboot多環(huán)境配置文件及自定義配置文件路徑,文中給大家介紹了classpath的基本概念講解及自定義springboot配置文件路徑的相關知識,需要的朋友可以參考下2023-02-02Spring Bean創(chuàng)建和循環(huán)依賴
這篇文章主要介紹了Spring Bean創(chuàng)建和循環(huán)依賴,講述了Spring容器中?Bean?的創(chuàng)建過程已經主要的方法,另外也著重分析了循環(huán)依賴的問題,需要的小伙伴可以參考一下2022-05-05