天天看點

MyBatis-Plus

MyBatis 最好的搭檔,正如魂鬥羅如此,效率翻倍。如果你中意Mybatis,那麼一定不能錯過MyBatis-Plus。

概述

為什麼要學習它呢?

MyBatisPlus可以節省我們大量工作時間,所有的CRUD代碼它都可以自動化完成!

類似元件:JPA、 tk-mapper、MyBatisPlus

簡介

官網: 簡介 | MyBatis-Plus (baomidou.com) 簡化 Mybatis開發

願景

我們的願景是成為 MyBatis 最好的搭檔,就像 魂鬥羅 中的 1P、2P,基友搭配,效率翻倍。

特性
  • 無侵入:隻做增強不做改變,引入它不會對現有工程産生影響,如絲般順滑
  • 損耗小:啟動即會自動注入基本 CURD,性能基本無損耗,直接面向對象操作,BaseMapper
  • 強大的 CRUD 操作:内置通用 Mapper、通用 Service,僅僅通過少量配置即可實作單表大部分 CRUD 操作,更有強大的條件構造器,滿足各類使用需求
  • 支援 Lambda 形式調用:通過 Lambda 表達式,友善的編寫各類查詢條件,無需再擔心字段寫錯
  • 支援主鍵自動生成:支援多達 4 種主鍵政策(内含分布式唯一 ID 生成器 - Sequence),可自由配置,完美解決主鍵問題
  • 支援 ActiveRecord 模式:支援 ActiveRecord 形式調用,實體類隻需繼承 Model 類即可進行強大的 CRUD 操作
  • 支援自定義全局通用操作:支援全局通用方法注入( Write once, use anywhere )
  • 内置代碼生成器:采用代碼或者 Maven 插件可快速生成 Mapper 、 Model 、 Service 、 Controller 層代碼,支援模闆引擎,更有超多自定義配置等您來使用
  • 内置分頁插件:基于 MyBatis 實體分頁,開發者無需關心具體操作,配置好插件之後,寫分頁等同于普通 List 查詢
  • 分頁插件支援多種資料庫:支援 MySQL、MariaDB、Oracle、DB2、H2、HSQL、SQLite、Postgre、SQLServer 等多種資料庫
  • 内置性能分析插件:可輸出 Sql 語句以及其執行時間,建議開發測試時啟用該功能,能快速揪出慢查詢
  • 内置全局攔截插件:提供全表 delete 、 update 操作智能分析阻斷,也可自定義攔截規則,預防誤操作

快速入門

位址: 快速開始 | MyBatis-Plus (baomidou.com)

步驟:

  1. 導入對應的依賴
  2. 研究依賴如何配置
  3. 代碼如何編寫?
  4. 提高擴充技術能力
步驟
  1. 建立資料庫

    mybatis_plus

  2. 建立user表
DROP TABLE IF EXISTS user;

CREATE TABLE user
(
	id BIGINT(20) NOT NULL COMMENT '主鍵ID',
	name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
	age INT(11) NULL DEFAULT NULL COMMENT '年齡',
	email VARCHAR(50) NULL DEFAULT NULL COMMENT '郵箱',
	PRIMARY KEY (id)
);
-- 真實開發中, version(樂觀鎖),deleted(邏輯删除),gmt_create,gmt_modified

INSERT INTO user (id, name, age, email) VALUES
(1, 'Jone', 18, '[email protected]'),
(2, 'Jack', 20, '[email protected]'),
(3, 'Tom', 28, '[email protected]'),
(4, 'Sandy', 21, '[email protected]'),
(5, 'Billie', 24, '[email protected]');
           
  1. 初始化項目

    建立Spring Boot項目

  2. 添加依賴
<!--Mybatis_Plus-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.0.5</version>
        </dependency>
           

說明︰我們使用mybatis-plus 可以節省我們大量的代碼,盡量不要同時導入mybatis和mybatis-plus ! 版本的差異!

  1. 連接配接資料庫
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis_plus?useSSL=false&useUnicode=true*characterEncoding=utf-8&serverTimezone=GMT%2B8
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

##mysql8需要增加時區的配置   serverTimezone=GMT%2B8
           
  1. 傳統方式pojo-dao(連接配接mybatis,配置mapper.xml檔案 ) -service-controller
  2. 使用了mybatis_plus之後
  • pojo
public class User {

    //對應資料庫中的主鍵(uuid、自增id、雪花算法、redis. zookeeper )
    private  Long id;
    private  String name;
    private  Integer age;
    private  String email;
	//有參構造 無參構造 set與get方法  toString()方法
}
           
  • mapper接口
// 在對應的mapper上面繼承基本的類 BaseMapper
@Mapper
public interface UserMapper extends BaseMapper<User> {  // 泛型

    //所有的CRUD操作都已經編寫完成
    //不需要向以前的配置檔案
}

//主類掃描接口
@MapperScan("com.qd.mybatis_plus.mapper")
           
  • 使用
@SpringBootTest
class MybatisPlusApplicationTests {

    //繼承了Base Mapper,所有的方法都來自父類
    @Autowired
    private UserMapper userMapper;
    @Test
    void contextLoads() {
        //查詢全部
        //參數是一個Wrapper,條件構造器
        List<User> users = userMapper.selectList(null);
        users.forEach(System.out::println);
    }
}
           
問題來了 ?
  1. SQL誰幫我們寫的? MyBatis-Plus
  2. 方法哪裡來的? MyBatis-Plus都寫好了

配置日志

問題:我們所有的sql是不可見的,我們希望知道他是怎麼執行的 ,我們必須要配置日志

## 配置日志   預設的控制台日志
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
           

CRUD擴充

Insert插入

//測試插入
    @Test
    public void testInsert() {
        User user = new User();
        user.setName("前度");
        user.setAge(3);
        user.setEmail("[email protected]");

        int i = userMapper.insert(user);   //幫我們自動生成id
        System.out.println(i);   //受影響的行數  1
        System.out.println(user);
    }
           
資料庫插入的id的預設值為:全局的唯一id

主鍵生成政策

預設 ID_WORKER 全局唯一id

參考文章: 分布式系統唯一ID生成方案彙總 - nick hao - 部落格園 (cnblogs.com)

雪花算法:

​ snowflake是Twitter開源的分布式ID生成算法,結果是一個long型的ID。其核心思想是:使用41bit作為毫秒數,10bit作為機器的ID(5個bit是資料中心:北京,上海等,5個bit的機器ID),12bit作為毫秒内的流水号(意味着每個節點在每毫秒可以産生 4096 個 ID),最後還有一個符号位,永遠是0。可以保證幾乎全球唯一!

主鍵自增 配置
  1. 在實體類字段上添加

    @TableId(type = IdType.AUTO)

  2. 資料庫字段要為自增!
  3. 再次測試即可
其餘的源碼解釋
public enum IdType {
    AUTO(0),     //資料庫id自增
    NONE(1),   //未設定主鍵  必須自己配置id
    INPUT(2),    //手段輸入
    ID_WORKER(3),   //預設的全局唯一id
    UUID(4),   //全局唯一id  uuid
    ID_WORKER_STR(5);  // ID_WORKER的字元串表示法
}
           

更新操作

//測試更新
    public  void  testUpdate(){

        User user = new User();
        user.setName("前度666");
        user.setAge(3);
        user.setEmail("[email protected]");
        //updateById 但是參數是一個對象!
        //可以通過條件拼接動态sql
        userMapper.updateById(user);
    }
           

所有的sql都是自動幫你動态配置的!

自動填充

建立時間、修改時間!這些個操作一遍都是自動化完成的,我們不希望手動更新!

阿裡巴巴開發手冊∶所有的資料庫表:

gmt_create

gmt_modified

所有表必備 需要自動化

方法一:資料庫級别 (不建議使用)
  1. 在表中新增字段

    create_time

    ,

    update_time

MyBatis-Plus
  1. 同步實體類
private Data createTime;
    private Data updateTime;
           
  1. 更新檢視即可
方法二: 代碼級别
  1. 删除資料庫的預設值,更新操作
MyBatis-Plus
  1. 在實體類字段屬性增加注解
//字段添加填充内容
    @TableField(fill = FieldFill.INSERT)  //查詢
    private Date createTime;
    @TableField(fill = FieldFill.INSERT_UPDATE) //查詢與更新
    private Date updateTime;
           
  1. 編寫處理器處理注解即可

    官網位址: 自動填充功能 | MyBatis-Plus (baomidou.com)

  • 添加

    handler

    包 含有

    MyMetaObjectHandler

    類 去實作

    MetaObjectHandler

  • @Component //一定不要忘記把處理器注冊到IOC容器中!!!
@Component   //一定不要忘記把處理器注冊到IOC容器中!!!
public class MyMetaObjectHandler implements MetaObjectHandler {

    private final static Date DATE_TIME = new Date();

    //插入時候填充政策
    @Override
    public void insertFill(MetaObject metaObject) {

        //setFieldValByName(String fieldName, Object fieldVal, MetaObject metaObject) {
        this.setFieldValByName("createTime", DATE_TIME, metaObject);
        this.setFieldValByName("updateTime", DATE_TIME, metaObject);
    }

    //更新時候填充政策
    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("updateTime", DATE_TIME, metaObject);
    }
}
           

4 .測試

MyBatis-Plus

樂觀鎖處理

樂觀鎖 : 十分樂觀,它總是認為不會出現問題,無論幹什麼不去上鎖 ! 如果出現了問題,再次更新值測

悲觀鎖 :十分悲觀,它總是認為總是出現問題,無論幹什麼都會上鎖!再去操作!

version、new version

當要更新一條記錄的時候,希望這條記錄沒有被别人更新

樂觀鎖實作方式:

  • 取出記錄時,擷取目前version
  • 更新時,帶上這個version
  • 執行更新時, set version = newVersion where version = oldVersion
  • 如果version不對,就更新失敗
-- 樂觀鎖 : 1 先查詢,獲得版本号 version = 1

-- A線程
update user set name = "前度" , version = version + 1
where id = 2 and version = 1 ;

-- B線程 搶先完成,這個時候版本号被修改  version = 2
update user set name = "前度" , version = version + 1
where id = 2 and version = 1 ;
           

測試

樂觀鎖插件: 樂觀鎖 | MyBatis-Plus (baomidou.com)

  1. 給資料庫增加 version 字段
MyBatis-Plus
  1. 實體類加對應的字段
@Version   //樂觀鎖 Version注解
    private  Integer version;
           
  1. 注冊元件

    config

    MyBatisPlusConfig

@MapperScan("com.qd.mybatis_plus.mapper")  //掃描
@EnableTransactionManagement   //開啟事務
@Configuration //配置類
public class MyBatisPlusConfig {

    //注冊樂觀鎖插件
    @Bean
    public OptimisticLockerInterceptor optimisticLockerInterceptor() {
        return new OptimisticLockerInterceptor();
    }
}
           
//測試樂觀鎖插件  成功
    @Test
    public void testOptimisticLockerInterceptor1() {
        //查詢使用者修改
        User user = userMapper.selectById(1L);
        //修改使用者資訊
        user.setAge(20);
        //執行更新
        userMapper.updateById(user);
    }

	//----------------------------------------------------------

    //測試樂觀鎖插件  失敗   多線程下
    @Test
    public void testOptimisticLockerInterceptor2() {
        //線程1
        User user = userMapper.selectById(1L);
        user.setAge(21);

        //模拟另外一個線程執行插隊操作
        User user2 = userMapper.selectById(1L);
        user2.setAge(22);
        userMapper.updateById(user2);

        //可以使用自旋鎖多次嘗試送出
        userMapper.updateById(user);  //如果沒有樂觀鎖,就會覆寫插隊線程的值  
    }  

	//結果仍為22
           
MyBatis-Plus

查詢操作

//測試查詢
    @Test
    public void testSelectById() {
        User user = userMapper.selectById(1L);
        System.out.println(user);

    }

    //測試批量查詢
    @Test
    public void testSelectById02() {
        List<User> users = userMapper.selectBatchIds(Arrays.asList(1, 2, 3));
        users.forEach(System.out::println);
    }

    //測試條件之一查詢 map操作
    @Test
    public void testSelectById03() {
        HashMap<String, Object> map = new HashMap<>();
        //自定義查詢
        map.put("name", "前度");
        map.put("age", 18);   //多個條件
        List<User> users = userMapper.selectByMap(map);
        users.forEach(System.out::println);
    }
           

分頁查詢

分頁在網站這一塊可太重要了,實作分頁的方法有:

  • 原始的 limit 進行分頁
  • 用 pageHelper 第三方插件分頁
  • MybatisPlus 已經内置分頁插件
如何使用分頁插件呢?

官網: 分頁 | MyBatis-Plus (baomidou.com)

  1. 配置攔截器元件
//分頁插件
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }
           
  1. 直接使用 page 對象即可
//測試分頁查詢
    @Test
    public void testPage() {
        //參數一: 目前頁
        //參數二: 頁面大小
        //使用了分頁插件之後,分頁操作非常簡單
        Page<User> page = new Page<>(2, 5); //第1頁 每頁5條
        userMapper.selectPage(page, null);
        page.getRecords().forEach(System.out::println);
        page.getTotal();//獲得總數
    }
           
MyBatis-Plus

删除操作

  • 根據 id 删除記錄
//測試删除
    @Test
    public void testDeleteById() {
        userMapper.deleteById(1402192921201025026L);
    }
    
    //測試通過id批量删除
    @Test
    public void testDeleteById02() {
        userMapper.deleteBatchIds(Arrays.asList(1L, 2L, 3L));
    }

    //測試通過map條件删除
    @Test
    public void testDeleteById03() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("name", "前端");
        userMapper.deleteByMap(map);
    }

           

我們在工作中會遇到一些問題; 邏輯删除

邏輯删除

實體删除 : 從資料庫中直接移除

邏輯删除 : 在資料庫中沒有移除,而是通過一個變量來讓他失效 ! deleted=0 => deleted=1

管理者可以檢視被删除的記錄!防止資料的丢失,類似于資源回收筒!

  1. 在資料表中增加

    deleted

    字段
MyBatis-Plus
  1. 實體類中增加屬性
@TableLogic //邏輯删除
    private Integer deleted;
           
  1. 配置邏輯删除元件

    官網: 邏輯删除 | MyBatis-Plus (baomidou.com)

//邏輯删除元件
    @Bean
    public ISqlInjector sqlInjector() {
        return new LogicSqlInjector();
    }

//注意:高版本無需配置元件 直接下一步即可
           
  1. 配置

    yaml

##配置邏輯删除
mybatis-plus.global-config.db-config.logic-delete-value=1
mybatis-plus.global-config.db-config.logic-not-delete-value=0
           
//測試删除
    @Test
    public void testDeleteById() {
        userMapper.deleteById(1402192921201025026L);
    }
           
MyBatis-Plus

可以發現,資料仍在資料庫中 并且在查詢中會字段拼接該參數 查詢不到邏輯删除的字段

PS:以上CRUD操作及其擴充,必須精通,會大大提高效率

多表操作

這裡不作過多配置 xml 的使用 請前往下面兩篇部落格學習

  1. mybatis plus 一對多,多表聯查的使用小記 - 黑化肥會揮發嗎 - 部落格園 (cnblogs.com)
  2. mybatis-plus配置xml進行多表查詢 - 簡書 (jianshu.com)

條件構造器

wrapper

十分重要 我們寫一些複雜的sql就可以使用它來替代!

官網: 條件構造器 | MyBatis-Plus (baomidou.com)

  • 測試一
@Test
    public void contextLoads() {

        //查詢name不為空 并且郵箱不為空  年齡大于等于3歲的使用者
        QueryWrapper<User> Wrapper = new QueryWrapper<>();
        Wrapper.isNotNull("name")
                .isNotNull("email")
                .ge("age", 20);  //大于等于
        userMapper.selectList(Wrapper).forEach(System.out::println);
    }
           
  • 測試二
@Test
    public void contextLoads02() {

        //查詢name為前度
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("name", "前度");
        System.out.println(userMapper.selectOne(wrapper));  //查詢一個  查詢多個用list,map
    }
           
  • 測試三
@Test
    public void contextLoads03() {

        //查詢年齡在20到30之間的使用者
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.between("age", 20, 30);  //區間
        Integer count = userMapper.selectCount(wrapper);  //查詢結果數
        System.out.println(count);
        List<Map<String, Object>> maps = userMapper.selectMaps(wrapper);
        maps.forEach(System.out::println);
    }
           
  • 測試四
@Test
    public void contextLoads04() {

        //子查詢
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.inSql("id", "select id from user where id<3");
        List<Object> objects = userMapper.selectObjs(wrapper);
        objects.forEach(System.out::println);
    }
           
  • 測試五
@Test
    public void contextLoads05() {

        //通過id進行排序
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        List<User> users = userMapper.selectList(wrapper);
        users.forEach(System.out::println);
    }
           

更多使用方法直接檢視官方文檔即可

代碼字段生成器

run前開啟MySQL 服務!!! 有些配置需要具體更改建議寫在 test
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;
import java.util.List;

public class AutoGenerateTest {
    public static void main(String[] args) {
        // 建立generator對象
        AutoGenerator mpg = new AutoGenerator();

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir"); // 項目根目錄不可修改
        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setAuthor("qiandu");  // 作者資訊
        gc.setOpen(false);  // 打開檔案夾
        gc.setFileOverride(false);// 是否覆寫
        gc.setServiceName("%sService"); // Service層接口 去字首
        gc.setIdType(IdType.AUTO);
        gc.setDateType(DateType.ONLY_DATE);
        gc.setSwagger2(false);  // 設定Swagger2文檔
        mpg.setGlobalConfig(gc);

        // 設定資料源
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDbType(DbType.MYSQL);  // 枚舉類型 選擇資料庫
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("root");
        dsc.setUrl("jdbc:mysql://localhost:3306/ssmdb?serverTimezone=Asia/Shanghai");
        // jdbc:mysql://localhost:3306/資料庫名稱?serverTimezone=Asia/Shanghai
        mpg.setDataSource(dsc);

        // 設定包資訊
        PackageConfig pc = new PackageConfig();
        pc.setParent("com.qd");  //包
        pc.setEntity("pojo");
        pc.setMapper("mapper");
        pc.setService("service");
        pc.setServiceImpl("service.impl");
        pc.setController("controller");
        mpg.setPackageInfo(pc);

        // 修改*mapper.xml檔案
        // 注入自定義配置,可以在 VM 中使用 cfg.abc 【可無】
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
            }
        };
        List<FileOutConfig> focList = new ArrayList<>();
        focList.add(new FileOutConfig("/templates/mapper.xml.vm") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 生成子產品化mapper檔案
                return projectPath + "/src/main/resources/mapper/" + tableInfo.getEntityName() + "/" +
                        tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
            }
        });
        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);

        // 政策配置 與資料庫相關
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig.setInclude("tb_goods");  // 表名 改!!!
        strategyConfig.setTablePrefix("tb_"); // 去除表字首tb_
        strategyConfig.setNaming(NamingStrategy.underline_to_camel); // 表名映射實體類
        strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel); // 字段映射實體類
        strategyConfig.setEntityLombokModel(true); // 加載Lombok
        strategyConfig.setLogicDeleteFieldName("deleted"); // 邏輯删除  具體看情況!
        // 自動填配置
        TableFill gmtCreate = new TableFill("gmt_create", FieldFill.INSERT);
        TableFill gmtModified = new TableFill("gmt_modified", FieldFill.INSERT_UPDATE);
        ArrayList<TableFill> tableFills = new ArrayList<>();
        tableFills.add(gmtCreate);
        tableFills.add(gmtModified);
        strategyConfig.setTableFillList(tableFills);
        // 樂觀鎖配置
        strategyConfig.setVersionFieldName("version");
        // 設定controller
        strategyConfig.setRestControllerStyle(true); // restful風格
        strategyConfig.setRestControllerStyle(true); // 下劃線命名
        mpg.setStrategy(strategyConfig);
        // 運作
        mpg.execute();
        System.out.println("====================【 PS:将會在mapper層與資源目錄共同生成mapper檔案,請保留合适的檔案!!!】====================");
    }
}
           

配置類集合

import com.baomidou.mybatisplus.core.injector.ISqlInjector;
import com.baomidou.mybatisplus.extension.injector.LogicSqlInjector;
import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.pagination.optimize.JsqlParserCountOptimize;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@MapperScan("com.qd.mybatis_plus.mapper")  //掃描
@EnableTransactionManagement   //開啟事務
@Configuration //配置類
public class MyBatisConfig {

    //注冊樂觀鎖插件
    @Bean
    public OptimisticLockerInterceptor optimisticLockerInterceptor() {
        return new OptimisticLockerInterceptor();
    }

    //分頁元件
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }

    //邏輯删除元件
    @Bean
    public ISqlInjector sqlInjector() {
        return new LogicSqlInjector();
    }
}
           

整合SpringBoot項目

1、導包

<!--freemarker模闆引擎-->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

<!--mysql資料庫驅動-->
<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>8.0.26</version>
</dependency>

<!--c3p0  druid連接配接池-->
<dependency>
    <groupId>com.mchange</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.5.2</version>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.1.10</version>
</dependency>

<!--mybatis-plus依賴-->
<dependency>
  <groupId>com.baomidou</groupId>
  <artifactId>mybatis-plus-boot-starter</artifactId>
  <version>3.4.2</version>
</dependency>

<!--mybatis-plus代碼生成器-->
<dependency>
  <groupId>com.baomidou</groupId>
  <artifactId>mybatis-plus-generator</artifactId>
  <version>3.4.1</version>
</dependency>
<dependency>
  <groupId>org.apache.velocity</groupId>
  <artifactId>velocity-engine-core</artifactId>
  <version>2.0</version>
</dependency>

<!--web依賴-->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!--lombok依賴-->
<dependency>
  <groupId>org.projectlombok</groupId>
  <artifactId>lombok</artifactId>
  <optional>true</optional>
  <version>1.18.20</version>
</dependency>

<!--測試test依賴-->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
  <scope>test</scope>
</dependency>

<!--導入aop-->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-aop</artifactId>
</dependency>
           

建構排除

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

或

 <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                        <exclude>
                            <groupId>com.baomidou</groupId>
                            <artifactId>mybatis-plus-generator</artifactId>
                        </exclude>
                        <exclude>
                            <groupId>org.apache.velocity</groupId>
                            <artifactId>velocity-engine-core</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
           

繼續閱讀