SpringBoot整合mybatis-plus全流程

今天我們來聊一個JAVA程序猿必須會的一個技能----> mybatis-plus
我們經常需要寫接口窃这,寫接口就得寫controller瞳别,service,impl ,mapper祟敛,pojo疤坝,xml ,特別是pojo馆铁,如果表字段多寫起來是很要命的跑揉,所以,我們就可以用mybatis-plus來解決這個問題了埠巨。
mybatis-plus官方鏈接 https://mp.baomidou.com/guide

步入正題

首先引入JAR包历谍,我選用的是3.1.0版本,需注意JAR因版本引出的問題。我這里把一些附帶的JAR也貼上來了辣垒,使用lombok的時候IDE注意安裝lombok插件望侈,并且將hrisey Plugin插件關閉,不然在操作@Data注解的對象是IDE會編譯報紅線(運行正常)

        <!-- mybatis plus 代碼生成器 -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-extension</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.46</version>
        </dependency>
        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.28</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.47</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.4</version>
            <scope>provided</scope>
        </dependency>

JAR引入成功后就添加yml配置了

mybatis-plus:
  configuration:
    map-underscore-to-camel-case: true
    auto-mapping-behavior: full
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  mapper-locations: classpath*:mapper/**/*Mapper.xml
  global-config:
    # 邏輯刪除配置
    db-config:
      # 刪除前
      logic-not-delete-value: 1
      # 刪除后
      logic-delete-value: 0

配置添加成功后開始編寫操作類

package com.farm.fowl;


import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
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.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

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

/**
 * @author : YYF
 * @date : 2021-04-11 17:45
 **/
public class CodeGenerator {
    //數據庫連接參數
    public static String driver = "com.mysql.jdbc.Driver";
    public static String url = "jdbc:mysql://localhost:3306/farm?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true";
    public static String username = "yinyifan";
    public static String password = "123456";
    //父級別包名稱,具體按自己項目路徑填
    public static String parentPackage = "com.farm";
    //代碼生成的目標路徑,具體按自己項目路徑填
    public static String generateTo = "/src/main/java";
    //mapper.xml的生成路徑
    public static String mapperXmlPath = "/src/main/resources/mapper";
    //控制器的公共基類勋桶,用于抽象控制器的公共方法脱衙,為空表示沒有父類
    public static String baseControllerClassName = "com.farm.fowl.controller.base.BaseController";
    //業(yè)務層的公共基類,用于抽象公共方法
    public static String baseServiceClassName;
    //作者名
    public static String author = "yyf";
    //模塊名稱例驹,用于組成包名
    public static String modelName = "fowl";
    //Mapper接口的模板文件捐韩,不用寫后綴 .ftl
    public static String mapperTempalte = "/ftl/mapper.java";

    /**
     * <p>
     * 讀取控制臺內容
     * </p>
     */
    public static String scanner(String tip) throws  Exception {
        Scanner scanner = new Scanner(System.in);
        StringBuilder help = new StringBuilder();
        help.append("請輸入" + tip + ":");
        System.out.println(help.toString());
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotEmpty(ipt)) {
                return ipt;
            }
        }
       throw new MybatisPlusException("請輸入正確的" + tip + "!");
    }

    /**
     * RUN THIS
     */
    public static void main(String[] args) {
        // 代碼生成器
        AutoGenerator mpg = new AutoGenerator();

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath + generateTo);
        gc.setAuthor(author);
        gc.setOpen(false);
        //設置時間類型為Date
        gc.setDateType(DateType.TIME_PACK);
        //開啟swagger
        //gc.setSwagger2(true);
        //設置mapper.xml的resultMap
        gc.setBaseResultMap(true);
        mpg.setGlobalConfig(gc);

        // 數據源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl(url);
        // dsc.setSchemaName("public");
        dsc.setDriverName(driver);
        dsc.setUsername(username);
        dsc.setPassword(password);
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setEntity("dao.model");
        //pc.setModuleName(scanner("模塊名"));
        pc.setModuleName(modelName);
        pc.setParent(parentPackage);
        mpg.setPackageInfo(pc);

        // 自定義配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                // to do nothing
            }
        };
        List<FileOutConfig> focList = new ArrayList<>();
        focList.add(new FileOutConfig("/templates/mapper.xml.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定義輸入文件名稱
                return projectPath + mapperXmlPath
                        + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
            }
        });
        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);
        mpg.setTemplate(new TemplateConfig().setXml(null));
        mpg.setTemplate(new TemplateConfig().setMapper(mapperTempalte));

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        //字段駝峰命名
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        //設置實體類的lombok
        strategy.setEntityLombokModel(true);
        //設置controller的父類
        if (baseControllerClassName != null) {
            strategy.setSuperControllerClass(baseControllerClassName);
        }
        //設置服務類的父類
        if (baseServiceClassName != null) {
            strategy.setSuperServiceImplClass(baseServiceClassName);
        }
        // strategy.
        //設置實體類屬性對應表字段的注解
        strategy.setEntityTableFieldAnnotationEnable(true);
        //設置表名
        String tableName = null;
        try {
            tableName = scanner("表名, all全部表");
        } catch (Exception e) {
            e.getMessage();
        }
        if (!"all".equalsIgnoreCase(tableName)) {
            strategy.setInclude(tableName);
        }

        strategy.setTablePrefix(pc.getModuleName() + "_");
        strategy.setRestControllerStyle(true);
        mpg.setStrategy(strategy);

        // 選擇 freemarker 引擎需要指定如下加眠饮,注意 pom 依賴必須有奥帘!
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    }
}

然后我們還要準備一個模板,模板放在項目的 /resources/ftl/ 下仪召,名稱為mapper.java.ftl 這樣就能跟上面CodeGenerator工具類的 public static String mapperTempalte = "/ftl/mapper.java" 對應上了寨蹋。

package ${package.Mapper};

import ${package.Entity}.${entity};
import ${superMapperClassPackage};
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

/**
* <p>
    * ${table.comment!} Mapper 接口
    * </p>
*
* @author ${author}
* @since ${date}
*/
<#if kotlin>
    interface ${table.mapperName} : ${superMapperClass}<${entity}>
<#else>
    @Mapper
    @Repository
    public interface ${table.mapperName} extends ${superMapperClass}<${entity}> {

    }
</#if>

現在我們來測試一下
首先創(chuàng)建一張表,DDL如下

CREATE TABLE `gift` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `gift_no` int(10) NOT NULL COMMENT '禮物編號',
  `gift_name` varchar(50) DEFAULT NULL COMMENT '禮物名',
  `gift_type` int(2) DEFAULT NULL COMMENT '禮物類型,0-雞,1-鴨,5-娃娃,10-其他',
  `gift_number` int(11) DEFAULT NULL COMMENT '禮物發(fā)出數量',
  `status` int(1) DEFAULT NULL COMMENT '狀態(tài) 0-正常,1-失效',
  `price` decimal(10,2) DEFAULT NULL COMMENT '單個禮物金額',
  `remark` varchar(100) DEFAULT NULL COMMENT '備注信息',
  `create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '記錄時間',
  `update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '最后更新時間',
  PRIMARY KEY (`id`),
  UNIQUE KEY `IDX_NO_TYPE` (`gift_no`,`gift_type`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=2438 DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT COMMENT='禮品表';

現在運行剛寫好的CodeGenerator類,在控制臺輸入剛創(chuàng)建的表名扔茅。然后按回車鍵已旧。等待執(zhí)行完成。


MP1.png

執(zhí)行完成后你會發(fā)現我們所需要的全部自動生成了


MP2.png

BaseMapper已經幫我們寫好了基本的CURD召娜,我們直接在controller里面操作service的父類ServiceImpl中方法即可运褪。
我們來測試一下

先往gift表中插入一條數據

INSERT INTO `gift` (`gift_no`, `gift_name`, `gift_type`, `gift_number`, `status`, `price`, `remark`, `create_time`, `update_time`) VALUES ('1', '五七塘鴨', '1', '1', '1', '100.00', '', '2021-04-11 16:00:14', '2021-04-11 16:00:14');

編寫controller類,這個非常簡單玖瘸。


MP3.png
MP4.png

以上就是SpringBoot整合MP(mybatis-plus)整個流程秸讹。
最后貼一下BaseMapper已經實現好的方法

/*
 * Copyright (c) 2011-2020, hubin (jobob@qq.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * https://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.baomidou.mybatisplus.core.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import org.apache.ibatis.annotations.Param;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/*

               :`
                    .:,
                     :::,,.
             ::      `::::::
             ::`    `,:,` .:`
             `:: `::::::::.:`      `:';,`
              ::::,     .:::`   `@++++++++:
               ``        :::`  @+++++++++++#
                         :::, #++++++++++++++`
                 ,:      `::::::;'##++++++++++
                 .@#@;`   ::::::::::::::::::::;
                  #@####@, :::::::::::::::+#;::.
                  @@######+@:::::::::::::.  #@:;
           ,      @@########':::::::::::: .#''':`
           ;##@@@+:##########@::::::::::: @#;.,:.
            #@@@######++++#####'::::::::: .##+,:#`
            @@@@@#####+++++'#####+::::::::` ,`::@#:`
            `@@@@#####++++++'#####+#':::::::::::@.
             @@@@######+++++''#######+##';::::;':,`
              @@@@#####+++++'''#######++++++++++`
               #@@#####++++++''########++++++++'
               `#@######+++++''+########+++++++;
                `@@#####+++++''##########++++++,
                 @@######+++++'##########+++++#`
                @@@@#####+++++############++++;
              ;#@@@@@####++++##############+++,
             @@@@@@@@@@@###@###############++'
           @#@@@@@@@@@@@@###################+:
        `@#@@@@@@@@@@@@@@###################'`
      :@#@@@@@@@@@@@@@@@@@##################,
      ,@@@@@@@@@@@@@@@@@@@@################;
       ,#@@@@@@@@@@@@@@@@@@@##############+`
        .#@@@@@@@@@@@@@@@@@@#############@,
          @@@@@@@@@@@@@@@@@@@###########@,
           :#@@@@@@@@@@@@@@@@##########@,
            `##@@@@@@@@@@@@@@@########+,
              `+@@@@@@@@@@@@@@@#####@:`
                `:@@@@@@@@@@@@@@##@;.
                   `,'@@@@##@@@+;,`
                        ``...``

 _ _     /_ _ _/_. ____  /    _
/ / //_//_//_|/ /_\  /_///_/_\      Talk is cheap. Show me the code.
     _/             /
 */

/**
 * Mapper 繼承該接口后,無需編寫 mapper.xml 文件雅倒,即可獲得CRUD功能
 * <p>這個 Mapper 支持 id 泛型</p>
 *
 * @author hubin
 * @since 2016-01-23
 */
public interface BaseMapper<T> {

    /**
     * 插入一條記錄
     *
     * @param entity 實體對象
     */
    int insert(T entity);

    /**
     * 根據 ID 刪除
     *
     * @param id 主鍵ID
     */
    int deleteById(Serializable id);

    /**
     * 根據 columnMap 條件璃诀,刪除記錄
     *
     * @param columnMap 表字段 map 對象
     */
    int deleteByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);

    /**
     * 根據 entity 條件,刪除記錄
     *
     * @param wrapper 實體對象封裝操作類(可以為 null)
     */
    int delete(@Param(Constants.WRAPPER) Wrapper<T> wrapper);

    /**
     * 刪除(根據ID 批量刪除)
     *
     * @param idList 主鍵ID列表(不能為 null 以及 empty)
     */
    int deleteBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);

    /**
     * 根據 ID 修改
     *
     * @param entity 實體對象
     */
    int updateById(@Param(Constants.ENTITY) T entity);

    /**
     * 根據 whereEntity 條件蔑匣,更新記錄
     *
     * @param entity        實體對象 (set 條件值,可以為 null)
     * @param updateWrapper 實體對象封裝操作類(可以為 null,里面的 entity 用于生成 where 語句)
     */
    int update(@Param(Constants.ENTITY) T entity, @Param(Constants.WRAPPER) Wrapper<T> updateWrapper);

    /**
     * 根據 ID 查詢
     *
     * @param id 主鍵ID
     */
    T selectById(Serializable id);

    /**
     * 查詢(根據ID 批量查詢)
     *
     * @param idList 主鍵ID列表(不能為 null 以及 empty)
     */
    List<T> selectBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);

    /**
     * 查詢(根據 columnMap 條件)
     *
     * @param columnMap 表字段 map 對象
     */
    List<T> selectByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);

    /**
     * 根據 entity 條件劣欢,查詢一條記錄
     *
     * @param queryWrapper 實體對象封裝操作類(可以為 null)
     */
    T selectOne(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

    /**
     * 根據 Wrapper 條件棕诵,查詢總記錄數
     *
     * @param queryWrapper 實體對象封裝操作類(可以為 null)
     */
    Integer selectCount(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

    /**
     * 根據 entity 條件,查詢全部記錄
     *
     * @param queryWrapper 實體對象封裝操作類(可以為 null)
     */
    List<T> selectList(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

    /**
     * 根據 Wrapper 條件凿将,查詢全部記錄
     *
     * @param queryWrapper 實體對象封裝操作類(可以為 null)
     */
    List<Map<String, Object>> selectMaps(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

    /**
     * 根據 Wrapper 條件校套,查詢全部記錄
     * <p>注意: 只返回第一個字段的值</p>
     *
     * @param queryWrapper 實體對象封裝操作類(可以為 null)
     */
    List<Object> selectObjs(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

    /**
     * 根據 entity 條件,查詢全部記錄(并翻頁)
     *
     * @param page         分頁查詢條件(可以為 RowBounds.DEFAULT)
     * @param queryWrapper 實體對象封裝操作類(可以為 null)
     */
    IPage<T> selectPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

    /**
     * 根據 Wrapper 條件牧抵,查詢全部記錄(并翻頁)
     *
     * @param page         分頁查詢條件
     * @param queryWrapper 實體對象封裝操作類
     */
    IPage<Map<String, Object>> selectMapsPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
}
?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末笛匙,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子灭忠,更是在濱河造成了極大的恐慌膳算,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,734評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件弛作,死亡現場離奇詭異涕蜂,居然都是意外死亡,警方通過查閱死者的電腦和手機映琳,發(fā)現死者居然都...
    沈念sama閱讀 92,931評論 3 394
  • 文/潘曉璐 我一進店門机隙,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人萨西,你說我怎么就攤上這事有鹿。” “怎么了谎脯?”我有些...
    開封第一講書人閱讀 164,133評論 0 354
  • 文/不壞的土叔 我叫張陵葱跋,是天一觀的道長。 經常有香客問我源梭,道長娱俺,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,532評論 1 293
  • 正文 為了忘掉前任废麻,我火速辦了婚禮荠卷,結果婚禮上,老公的妹妹穿的比我還像新娘烛愧。我一直安慰自己油宜,他們只是感情好,可當我...
    茶點故事閱讀 67,585評論 6 392
  • 文/花漫 我一把揭開白布怜姿。 她就那樣靜靜地躺著慎冤,像睡著了一般。 火紅的嫁衣襯著肌膚如雪沧卢。 梳的紋絲不亂的頭發(fā)上粪薛,一...
    開封第一講書人閱讀 51,462評論 1 302
  • 那天,我揣著相機與錄音搏恤,去河邊找鬼违寿。 笑死,一個胖子當著我的面吹牛熟空,可吹牛的內容都是我干的藤巢。 我是一名探鬼主播,決...
    沈念sama閱讀 40,262評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼息罗,長吁一口氣:“原來是場噩夢啊……” “哼掂咒!你這毒婦竟也來了?” 一聲冷哼從身側響起迈喉,我...
    開封第一講書人閱讀 39,153評論 0 276
  • 序言:老撾萬榮一對情侶失蹤绍刮,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后挨摸,有當地人在樹林里發(fā)現了一具尸體孩革,經...
    沈念sama閱讀 45,587評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,792評論 3 336
  • 正文 我和宋清朗相戀三年得运,在試婚紗的時候發(fā)現自己被綠了膝蜈。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,919評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡熔掺,死狀恐怖饱搏,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情置逻,我是刑警寧澤推沸,帶...
    沈念sama閱讀 35,635評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站券坞,受9級特大地震影響鬓催,放射性物質發(fā)生泄漏。R本人自食惡果不足惜报慕,卻給世界環(huán)境...
    茶點故事閱讀 41,237評論 3 329
  • 文/蒙蒙 一深浮、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧眠冈,春花似錦飞苇、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,855評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至雇盖,卻和暖如春忿等,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背崔挖。 一陣腳步聲響...
    開封第一講書人閱讀 32,983評論 1 269
  • 我被黑心中介騙來泰國打工贸街, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留庵寞,地道東北人。 一個月前我還...
    沈念sama閱讀 48,048評論 3 370
  • 正文 我出身青樓薛匪,卻偏偏與公主長得像捐川,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子逸尖,可洞房花燭夜當晚...
    茶點故事閱讀 44,864評論 2 354

推薦閱讀更多精彩內容