今天我們來聊一個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í)行完成。
執(zhí)行完成后你會發(fā)現我們所需要的全部自動生成了
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類,這個非常簡單玖瘸。
以上就是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);
}