ruoyi-plus-server(一):引入Mybatis-Plus


背景:著名開源管理系統ruoyi-vue是基於SpringBoot,Spring Security,JWT,Vue & Element 的前后端分離權限管理系統(https://gitee.com/y_project/RuoYi-Vue)

需求:站在巨人的肩膀上打造一個更符合自己使用習慣的管理系統

源碼:https://gitee.com/baohaipeng/ruoyi-plus-server

方法:

   一、下載資源,項目跑起來

    1、下載地址:https://gitee.com/y_project/RuoYi-Vue.git

    2、按照官方提供的sql文件,創建數據庫

    3、打開ruoyi項目修改數據庫連接(application-druid.yml),修改redis等其他配置后,啟動無誤代表項目源碼正常、數據庫正常

    4、為示區別,修改包名(com.ruoyi——>com.bhp.ruoyiplus),此時不能直接啟動,還需修改以下兩處

     ①typeAliasesPackage

      

 

       ②ApplicationConfig

      

 

      備注:可以在這里直接改為com.bhp.ruoyiplus.project.**.mapper,也可以把這里的@MapperScan刪掉,加在啟動類。由於后期我想將該包發布成依賴供其他項目使用,為避免沖突,我選擇刪掉該處注解,在啟動類上進行掃描

      

 

   二、引入lombok,對象代碼更干凈

    1、版本:1.16.10

    2、依賴

<dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.16.10</version>
</dependency>

 

    三、引入MP,sql操作更簡便

    1、官方文檔:https://mp.baomidou.com/

    2、版本:3.3.0

    3、依賴

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

 

   四、project目錄下新建demo模塊

    1、數據庫腳本

CREATE TABLE `demo`  (
  `id` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `des` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

INSERT INTO `demo` VALUES ('1', 'demomom', '');

 

    2、目錄結構

      

 

    3、模塊代碼

     ①Demo

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;


/**
 * @ClassName Demo
 * @Description TODO
 * @Author 北海派
 * @Date 2020/5/18 19:24
 * @Version 1.0
 **/
@Data
@TableName("demo")
public class Demo{

    @TableId(value = "id", type = IdType.UUID)
    private String id;
    @TableField("name")
    private String name;
    @TableField("des")
    private String des;
}

      ②DemoMapper

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.bhp.ruoyiplus.project.demo.domain.Demo;

/**
 * @ClassName DemoMapper
 * @Description TODO
 * @Author 北海派
 * @Date 2020/5/18 19:30
 * @Version 1.0
 **/
public interface DemoMapper extends BaseMapper<Demo> {
}

      ③DemoService

import com.baomidou.mybatisplus.extension.service.IService;
import com.bhp.ruoyiplus.project.demo.domain.Demo;

/**
 * @ClassName DemoService
 * @Description TODO
 * @Author 北海派
 * @Date 2020/5/18 19:32
 * @Version 1.0
 **/
public interface DemoService extends IService<Demo> {
}

        ④DemoServiceImpl

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bhp.ruoyiplus.project.demo.domain.Demo;
import com.bhp.ruoyiplus.project.demo.mapper.DemoMapper;
import com.bhp.ruoyiplus.project.demo.service.DemoService;
import org.springframework.stereotype.Service;

/**
 * @ClassName DemoServiceImpl
 * @Description TODO
 * @Author 北海派
 * @Date 2020/5/18 19:32
 * @Version 1.0
 **/
@Service
public class DemoServiceImpl  extends ServiceImpl<DemoMapper, Demo> implements DemoService {
}

      ⑤DemoController

import com.bhp.ruoyiplus.framework.web.controller.BaseController;
import com.bhp.ruoyiplus.project.demo.domain.Demo;
import com.bhp.ruoyiplus.project.demo.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName DemoController
 * @Description TODO
 * @Author 北海派
 * @Date 2020/5/18 19:22
 * @Version 1.0
 **/
@RestController
@RequestMapping("/demo")
public class DemoController extends BaseController {

    @Autowired
    private DemoService demoService;

    @GetMapping()
    public Demo get(){
        Demo demo = demoService.getById("1");
        return demo;
    }
}

     4、重啟項目,用Postman獲取驗證碼、登錄獲取token、訪問接口,報如下錯誤

      

       原因:項目使用的mybatis及其相關配置MP中自帶的mybatis沖突,需要排除項目中的依賴及配置

     ①修改POM文件,刪除如下依賴

      

      ②修改配置文件,刪除application.yml中的mybatis配置,增加MP配置      

# MyBatis配置
#mybatis:
#    # 搜索指定包別名
#    typeAliasesPackage: com.bhp.ruoyiplus.project.**.domain
#    # 配置mapper的掃描,找到所有的mapper.xml映射文件
#    mapperLocations: classpath*:mybatis/**/*Mapper.xml
#    # 加載全局的配置文件
#    configLocation: classpath:mybatis/mybatis-config.xml

# MyBatis-Plus配置
mybatis-plus:
  mapper-locations: classpath*:mybatis/**/*Mapper.xml
  #實體掃描
  typeAliasesPackage: com.bhp.ruoyiplus.project.**.domain

      ③刪除MybatisConfig配置類

      

         ④重啟項目,再次訪問接口,響應正常

      

      ⑤修改DemoController,測試框架統一響應,與框架分頁響應是否正常,經測試接口均正常

import com.bhp.ruoyiplus.framework.web.controller.BaseController;
import com.bhp.ruoyiplus.framework.web.domain.AjaxResult;
import com.bhp.ruoyiplus.framework.web.page.TableDataInfo;
import com.bhp.ruoyiplus.project.demo.domain.Demo;
import com.bhp.ruoyiplus.project.demo.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName DemoController
 * @Description TODO
 * @Author 北海派
 * @Date 2020/5/18 19:22
 * @Version 1.0
 **/
@RestController
@RequestMapping("/demo")
public class DemoController extends BaseController {

    @Autowired
    private DemoService demoService;

    @GetMapping()
    public AjaxResult get() {
        Demo demo = demoService.getById("1");
        if (null==demo){
            return AjaxResult.error("無數據");
        }
        return AjaxResult.success(demo);
    }

    @GetMapping("/list")
    public TableDataInfo getList() {
        startPage();
        return getDataTable(demoService.list());
    }
}

 

備注:因為該接口使用的是MP的內置方法,如果響應正常,說明MP已經引入成功,並且分頁使用也正常。MP本身也帶有分頁插件,此處選擇繼續使用框架提供的pagehelper,目前這個地方不是很關鍵,后期需要時,會進行變更。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM