MyBatis-Plus掃盲啦?。?!
點擊上方藍色字體,選擇“標星公眾號”
優(yōu)質(zhì)文章,第一時間送達
一、MyBatis-Plus簡介
MyBatis-Plus官網(wǎng)
參考教程
MyBatis-Plus(簡稱 MP)是一個 MyBatis 的增強工具,在 MyBatis 的基礎(chǔ)上只做增強不做改變,為簡化開發(fā)、提高效率而生。
二、特性
無侵入:只做增強不做改變,引入它不會對現(xiàn)有工程產(chǎn)生影響,如絲般順滑
損耗?。簡蛹磿詣幼⑷牖?CURD,性能基本無損耗,直接面向?qū)ο蟛僮?br style="box-sizing: border-box;outline: 0px;overflow-wrap: break-word;">強大的 CRUD 操作:內(nèi)置通用 Mapper、通用 Service,僅僅通過少量配置即可實現(xiàn)單表大部分 CRUD 操作,更有強大的條件構(gòu)造器,滿足各類使用需求
支持 Lambda 形式調(diào)用:通過 Lambda 表達式,方便的編寫各類查詢條件,無需再擔心字段寫錯
支持主鍵自動生成:支持多達 4 種主鍵策略(內(nèi)含分布式唯一 ID 生成器 - Sequence),可自由配置,完美解決主鍵問題
支持 ActiveRecord 模式:支持 ActiveRecord 形式調(diào)用,實體類只需繼承 Model 類即可進行強大的 CRUD 操作
支持自定義全局通用操作:支持全局通用方法注入( Write once, use anywhere )
內(nèi)置代碼生成器:采用代碼或者 Maven 插件可快速生成 Mapper 、 Model 、 Service 、 Controller 層代碼,支持模板引擎,更有超多自定義配置等您來使用
內(nèi)置分頁插件:基于 MyBatis 物理分頁,開發(fā)者無需關(guān)心具體操作,配置好插件之后,寫分頁等同于普通 List 查詢
分頁插件支持多種數(shù)據(jù)庫:支持 MySQL、MariaDB、Oracle、DB2、H2、HSQL、SQLite、Postgre、SQLServer 等多種數(shù)據(jù)庫
內(nèi)置性能分析插件:可輸出 Sql 語句以及其執(zhí)行時間,建議開發(fā)測試時啟用該功能,能快速揪出慢查詢
內(nèi)置全局攔截插件:提供全表 delete 、 update 操作智能分析阻斷,也可自定義攔截規(guī)則,預(yù)防誤操作
三、快速開始
1、創(chuàng)建一張user表

//user表存在,刪除user表
DROP?TABLE?IF?EXISTS?user;
//創(chuàng)建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)
);//刪除user表數(shù)據(jù)
DELETE?FROM?user;
//user表插入數(shù)據(jù)
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]');2、創(chuàng)建Spring Boot工程
(1)依賴引入(pom.xml)
注意:引入 MyBatis-Plus 之后請不要再次引入 MyBatis 以及 MyBatis-Spring,以避免因版本差異導(dǎo)致的問題。
<dependencies>
????<dependency>
????????<groupId>org.springframework.bootgroupId>
????????<artifactId>spring-boot-starterartifactId>
????dependency>
????<dependency>
????????<groupId>org.springframework.bootgroupId>
????????<artifactId>spring-boot-starter-testartifactId>
????????<scope>testscope>
????????<exclusions>
????????????<exclusion>
????????????????<groupId>org.junit.vintagegroupId>
????????????????<artifactId>junit-vintage-engineartifactId>
????????????exclusion>
????????exclusions>
????dependency>
????
????<dependency>
????????<groupId>com.baomidougroupId>
????????<artifactId>mybatis-plus-boot-starterartifactId>
????????<version>3.0.5version>
????dependency>
????
????<dependency>
????????<groupId>mysqlgroupId>
????????<artifactId>mysql-connector-javaartifactId>
????dependency>
????
????
????????<dependency>
????????????<groupId>org.apache.velocitygroupId>
????????????<artifactId>velocity-engine-coreartifactId>
????????????<version>2.0version>
????????dependency>
????????
????
????<dependency>
????????<groupId>org.projectlombokgroupId>
????????<artifactId>lombokartifactId>
????dependency>
dependencies>
??
????<build>
????????<plugins>
????????????<plugin>
????????????????<groupId>org.springframework.bootgroupId>
????????????????<artifactId>spring-boot-maven-pluginartifactId>
????????????plugin>
????????plugins>
????????<resources>
????????????<resource>
????????????????<directory>src/main/javadirectory>
????????????????<includes>
????????????????????<include>**/*.xmlinclude>
????????????????includes>
????????????????<filtering>falsefiltering>
????????????resource>
????????resources>
????build>(2)書寫項目配置(application.properties)
#?服務(wù)端口
server.port=8001
#?服務(wù)名
spring.application.name=service-user
#?環(huán)境設(shè)置:dev、test、prod
spring.profiles.active=dev
#?mysql數(shù)據(jù)庫連接
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/MyBatis-plus?serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=root
#返回json的全局時間格式
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8
#mybatis日志
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
#配置mapper xml文件的路徑
mybatis-plus.mapper-locations=classpath:com/dashu/user/mapper/xml/*.xml(3) 代碼生成器
import?com.baomidou.mybatisplus.annotation.DbType;
import?com.baomidou.mybatisplus.annotation.IdType;
import?com.baomidou.mybatisplus.generator.AutoGenerator;
import?com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import?com.baomidou.mybatisplus.generator.config.GlobalConfig;
import?com.baomidou.mybatisplus.generator.config.PackageConfig;
import?com.baomidou.mybatisplus.generator.config.StrategyConfig;
import?com.baomidou.mybatisplus.generator.config.rules.DateType;
import?com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import?org.junit.Test;
/**
?* 代碼生成器
?*/
public?class?GetCode?{
????@Test
????public?void?main1()?{
????????// 1、創(chuàng)建代碼生成器
????????AutoGenerator mpg = new?AutoGenerator();
????????// 2、全局配置
????????GlobalConfig gc = new?GlobalConfig();
????????String projectPath = System.getProperty("user.dir");
????????System.out.println(projectPath);
????????gc.setOutputDir("項目絕對地址"?+ "/src/main/java");//生成路徑
????????gc.setAuthor("dashu");//作者名
????????gc.setOpen(false); //生成后是否打開資源管理器
????????gc.setFileOverride(false); //重新生成時文件是否覆蓋
????????/*
?????????* mp生成service層代碼,默認接口名稱第一個字母有 I
?????????* UcenterService
?????????* */
????????gc.setServiceName("%sService"); //去掉Service接口的首字母I
????????gc.setIdType(IdType.ID_WORKER_STR); //主鍵策略
????????gc.setDateType(DateType.ONLY_DATE);//定義生成的實體類中日期類型
????????gc.setSwagger2(true);//開啟Swagger2模式
????????mpg.setGlobalConfig(gc);
????????// 3、數(shù)據(jù)源配置
????????DataSourceConfig dsc = new?DataSourceConfig();
????????dsc.setUrl("jdbc:mysql://localhost:3306/MyBatis-plus?serverTimezone=GMT%2B8");
????????dsc.setDriverName("com.mysql.cj.jdbc.Driver");
????????dsc.setUsername("root");
????????dsc.setPassword("root");
????????dsc.setDbType(DbType.MYSQL);
????????mpg.setDataSource(dsc);
????????// 4、包配置
????????PackageConfig pc = new?PackageConfig();
????????pc.setModuleName("user"); //模塊名
????????pc.setParent("com.dashu");
????????pc.setController("controller");
????????pc.setEntity("entity");
????????pc.setService("service");
????????pc.setMapper("mapper");
????????mpg.setPackageInfo(pc);
????????// 5、策略配置
????????StrategyConfig strategy = new?StrategyConfig();
????????strategy.setInclude("user");//表名,根據(jù)表生成代碼
????????strategy.setNaming(NamingStrategy.underline_to_camel);//數(shù)據(jù)庫表映射到實體的命名策略
????????strategy.setTablePrefix(pc.getModuleName() + "_"); //生成實體時去掉表前綴
????????strategy.setColumnNaming(NamingStrategy.underline_to_camel);//數(shù)據(jù)庫表字段映射到實體的命名策略
????????strategy.setEntityLombokModel(true); // lombok 模型 @Accessors(chain = true) setter鏈式操作
????????strategy.setRestControllerStyle(true); //restful api風格控制器
????????strategy.setControllerMappingHyphenStyle(true); //url中駝峰轉(zhuǎn)連字符
????????mpg.setStrategy(strategy);
????????// 6、執(zhí)行
????????mpg.execute();
????}
}
四、MyBatis-Plus的簡單CURD
1、 insert
@Autowired
????private?UserMapper userMapper;
@Test
????public?void?insertUser(){
????????User user = new?User();
????????user.setName("張國榮");
????????user.setAge(18);
????????user.setEmail("[email protected]");
????????int?result = userMapper.insert(user);
????????System.out.println(result); //影響的行數(shù)
????????System.out.println(user); //id自動回填
????}2、 update
@Autowired
????private?UserMapper userMapper;
@Test
????public?void?testUpdateById(){
????????User user = new?User();
????????user.setId(1L);//id
????????user.setAge(20);
????????int?result = userMapper.updateById(user);
????????System.out.println(result);//影響的行數(shù)
????}3、 查詢
(1)根據(jù)id查詢記錄
@Test
public?void?testSelectById(){
????User user = userMapper.selectById(1L);
????System.out.println(user);
}(2)通過多個id批量查詢
@Test
public?void testSelectBatchIds(){
????List users = userMapper.selectBatchIds(Arrays.asList(1, 2, 3));
????users.forEach(System.out::println);
} (3)簡單的條件查詢
@Test
public?void?testSelectByMap(){
????HashMap map?= new?HashMap<>();
????map.put("name", "張國榮");
????map.put("age", 18);
????List users = userMapper.selectByMap(map);
????users.forEach(System.out::println);
} 注意:map中的key對應(yīng)的是數(shù)據(jù)庫中的列名。例如數(shù)據(jù)庫user_id,實體類是userId,這時map的key需要填寫user_id
4、 刪除
(1)根據(jù)id刪除記錄
@Test
public?void?testDeleteById(){
????int?result = userMapper.deleteById(8L);
????System.out.println(result);
}(2)批量刪除
@Test
????public?void?testDeleteBatchIds() {
????????int?result = userMapper.deleteBatchIds(Arrays.asList(8, 9, 10));
????????System.out.println(result);
????}(3)簡單的條件查詢刪除
@Test
public?void?testDeleteByMap()?{
????HashMap map?= new?HashMap<>();
????map.put("name", "張國榮");
????map.put("age", 18);
????int?result = userMapper.deleteByMap(map);
????System.out.println(result);
} 五、MyBatis-Plus條件構(gòu)造器
1、Wrapper

Wrapper :條件構(gòu)造抽象類,最頂端父類
AbstractWrapper :用于查詢條件封裝,生成 sql 的 where 條件
QueryWrapper :Entity 對象封裝操作類,不是用lambda語法
UpdateWrapper :Update 條件封裝,用于Entity對象更新操作
AbstractLambdaWrapper :Lambda 語法使用 Wrapper統(tǒng)一處理解析 lambda 獲取 column。
LambdaQueryWrapper :看名稱也能明白就是用于Lambda語法使用的查詢Wrapper
LambdaUpdateWrapper :Lambda 更新封裝Wrapper
2、構(gòu)造器方法
eq
eq(R column, Object?val)
eq(boolean?condition, R column, Object?val)等于 =
例: eq(“name”, “老王”)—>name = ‘老王’
ne
ne(R column, Object?val)
ne(boolean?condition, R column, Object?val)不等于 <>
例: ne(“name”, “老王”)—>name <> ‘老王’
gt
gt(R column, Object?val)
gt(boolean?condition, R column, Object?val)大于 >
例: gt(“age”, 18)—>age > 18
lt
lt(R column, Object?val)
lt(boolean?condition, R column, Object?val)小于 <
例: lt(“age”, 18)—>age < 18
le
le(R column, Object?val)
le(boolean?condition, R column, Object?val)小于等于 <=
例: le(“age”, 18)—>age <= 18
between
between(R column, Object?val1, Object?val2)
between(boolean?condition, R column, Object?val1, Object?val2)BETWEEN 值1 AND 值2
例: between(“age”, 18, 30)—>age between 18 and 30
like
like(R column, Object?val)
like(boolean?condition, R column, Object?val)LIKE ‘%值%’
例: like(“name”, “王”)—>name like ‘%王%’
isNull
isNull(R column)
isNull(boolean condition, R column)字段 IS NULL
例: isNull(“name”)—>name is nul
參考教程
五、MyBatis-Plus插件
user表添加version(版本)、isDelete(邏輯刪除)、createTime(創(chuàng)建時間)、updateTime
(更新時間)字段
1、自動填充
實體類上添加注解
@Data
public?class?User {
????......
?????
????//版本
????@TableField(fill = FieldFill.INSERT)
????private?Integer version;
????
????//邏輯刪除
????@TableField(fill = FieldFill.INSERT)
????private?boolean?isDelete;
????
????//創(chuàng)建時間
????@TableField(fill = FieldFill.INSERT)
????private?Date?createTime;
??//添加時間
????//@TableField(fill = FieldFill.UPDATE)
????@TableField(fill = FieldFill.INSERT_UPDATE)
????private?Date?updateTime;
}MyBatis-Plus元處理器
import?com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import?org.apache.ibatis.reflection.MetaObject;
import?org.slf4j.Logger;
import?org.slf4j.LoggerFactory;
import?org.springframework.stereotype.Component;
import?java.util.Date;
/**
?* 自動填充元處理器
?*/
@Component
public?class?MyMetaObjectHandler?implements?MetaObjectHandler?{
????private?static?final?Logger LOGGER = LoggerFactory.getLogger(MyMetaObjectHandler.class);
????@Override
????public?void?insertFill(MetaObject metaObject)?{
????????this.setFieldValByName("isDeleted", false, metaObject);//邏輯刪除
????????this.setFieldValByName("createTime", new?Date(), metaObject);
????????this.setFieldValByName("updateTime", new?Date(), metaObject);
????????this.setFieldValByName("version", 1, metaObject);//版本
????}
????@Override
????public?void?updateFill(MetaObject metaObject)?{
????????this.setFieldValByName("updateTime", new?Date(), metaObject);
????}
}2、樂觀鎖
主要適用場景:當要更新一條記錄的時候,希望這條記錄沒有被別人更新,也就是說實現(xiàn)線程安全的數(shù)據(jù)更新
樂觀鎖實現(xiàn)方式:
取出記錄時,獲取當前version
更新時,帶上這個version
執(zhí)行更新時, set version = newVersion where version = oldVersion
如果version不對,就更新失敗
實體類上添加注解
@Version
@TableField(fill = FieldFill.INSERT)
private Integer version;樂觀鎖插件
/**
?????* 樂觀鎖插件
?????*/
????@Bean
????public?OptimisticLockerInterceptor optimisticLockerInterceptor()?{
????????return?new?OptimisticLockerInterceptor();
????}3、邏輯刪除
物理刪除:真實刪除,將對應(yīng)數(shù)據(jù)從數(shù)據(jù)庫中刪除,之后查詢不到此條被刪除數(shù)據(jù)
邏輯刪除:假刪除,將對應(yīng)數(shù)據(jù)中代表是否被刪除字段狀態(tài)修改為“被刪除狀態(tài)”,之后在數(shù)據(jù)庫中仍舊能看到此條數(shù)據(jù)記錄
實體類上添加注解
@TableLogic
@TableField(fill = FieldFill.INSERT)
private boolean isDelete;邏輯刪除插件
@Bean
public?ISqlInjector sqlInjector()?{
????return?new?LogicSqlInjector();
}application.properties 加入配置
mybatis-plus.global-config.db-config.logic-delete-value=1
mybatis-plus.global-config.db-config.logic-not-delete-value=04、性能分析
性能分析攔截器,用于輸出每條 SQL 語句及其執(zhí)行時間
SQL 性能執(zhí)行分析,開發(fā)環(huán)境使用,超過指定時間,停止運行。有助于發(fā)現(xiàn)問題
插件
/**
?* SQL 執(zhí)行性能分析插件
?* 開發(fā)環(huán)境使用,線上不推薦。 maxTime 指的是 sql 最大執(zhí)行時長
?*/
@Bean
@Profile({"dev","test"})// 設(shè)置 dev test 環(huán)境開啟
public?PerformanceInterceptor performanceInterceptor() {
????PerformanceInterceptor performanceInterceptor = new?PerformanceInterceptor();
????performanceInterceptor.setMaxTime(3000);//ms,超過此處設(shè)置的ms則sql不執(zhí)行
????performanceInterceptor.setFormat(true);
????return?performanceInterceptor;
}5、分頁插件
分頁插件
/**
?* 分頁插件
?*/
@Bean
public?PaginationInterceptor paginationInterceptor()?{
????return?new?PaginationInterceptor();
}test實例
@Test
public void testSelectPage() {
????Page page = new?Page<>(1,5);
????userMapper.selectPage(page, null);
????page.getRecords().forEach(System.out::println);
????System.out.println(page.getCurrent());
????System.out.println(page.getPages());
????System.out.println(page.getSize());
????System.out.println(page.getTotal());
????System.out.println(page.hasNext());
????System.out.println(page.hasPrevious());
} 6、MyBatis-Plus插件類
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.PerformanceInterceptor;
import?org.mybatis.spring.annotation.MapperScan;
import?org.springframework.context.annotation.Bean;
import?org.springframework.context.annotation.Configuration;
import?org.springframework.context.annotation.Profile;
@Configuration
public?class?MybatisPlusConfig?{
????/**
?????* 樂觀鎖插件
?????*/
????@Bean
????public?OptimisticLockerInterceptor optimisticLockerInterceptor()?{
????????return?new?OptimisticLockerInterceptor();
????}
????/**
?????* 分頁插件
?????*/
????@Bean
????public?PaginationInterceptor paginationInterceptor()?{
????????return?new?PaginationInterceptor();
????}
????/**
?????* 邏輯刪除插件
?????* @return
?????*/
????@Bean
????public?ISqlInjector sqlInjector()?{
????????return?new?LogicSqlInjector();
????}
????/**
?????* SQL 執(zhí)行性能分析插件
?????* 開發(fā)環(huán)境使用,線上不推薦。 maxTime 指的是 sql 最大執(zhí)行時長
?????*/
????@Bean
????@Profile({"dev","test"})// 設(shè)置 dev test 環(huán)境開啟
????public?PerformanceInterceptor performanceInterceptor()?{
????????PerformanceInterceptor performanceInterceptor = new?PerformanceInterceptor();
????????performanceInterceptor.setMaxTime(3000);//ms,超過此處設(shè)置的ms則sql不執(zhí)行
????????performanceInterceptor.setFormat(true);
????????return?performanceInterceptor;
????}
}版權(quán)聲明:本文為博主原創(chuàng)文章,遵循?CC 4.0 BY-SA?版權(quán)協(xié)議,轉(zhuǎn)載請附上原文出處鏈接和本聲明。
本文鏈接:
https://blog.csdn.net/weixin_43521890/article/details/107682033
粉絲福利:108本java從入門到大神精選電子書領(lǐng)取
???
?長按上方二維碼?2 秒 加鋒哥微信好友,備注「1234」即可獲取資料以及 可以進入java1234官方微信群
感謝點贊支持下哈?
