Mybatis-Plus3.x的創(chuàng)建步驟及使用教程
MyBatis-Plus(簡(jiǎn)稱(chēng) MP)是一個(gè) MyBatis 的增強(qiáng)工具,在 MyBatis 的基礎(chǔ)上只做增強(qiáng)不做改變,為 簡(jiǎn)化開(kāi)發(fā)、提高效率而生。
一、引入
創(chuàng)建步驟:
1.創(chuàng)建Spring Boot工程
2.添加依賴(lài)
引入 Spring Boot Starter 父工程:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/>
</parent>
引入相關(guān)其他依賴(lài)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
mybatis-plus相關(guān)依賴(lài)
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.0</version>
</dependency>
<!-- 引入mysql依賴(lài) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>配置application.yml:
添加mysql相關(guān)配置
mybatis-plus:
type-aliases-package: com.hz.entity #類(lèi)型別名所在的包
#控制臺(tái)打印sql語(yǔ)句
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
map-underscore-to-camel-case: false #駝峰映射
global-config:
db-config:
logic-delete-field: delFlag #全局邏輯刪除字段值 3.3.0開(kāi)始支持,詳情看下面。
logic-delete-value: 1 #邏輯已刪除值(默認(rèn)為 1)
logic-not-delete-value: 0 #邏輯未刪除值(默認(rèn)為 0)
#數(shù)據(jù)庫(kù)鏈接
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/demo?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC
username: root
driver-class-name: com.mysql.cj.jdbc.Driver
password: 123456
#靜態(tài)資源
resources:
static-locations: classpath:/templates,classpath:/static/
在 Spring Boot 啟動(dòng)類(lèi)中添加 @MapperScan 注解

編碼:
在mapper類(lèi)中繼承Basemapper

測(cè)試:

注意:
1.數(shù)據(jù)庫(kù)字段若為駝峰命名,則需要開(kāi)啟
mybatis-plus:configuration:map-underscoreto-camel-case: false #駝峰映射
2. UserMapper 中的 selectList() 方法的參數(shù)為 MP 內(nèi)置的條件封裝器 Wrapper ,所以 不填寫(xiě)就是無(wú)任何條件
3.若需要自定義DAO接口,則需要在yml中讀取mapper文件,
mybatis-plus:mapperlocations: classpath:mappers/*.xml
二、通用crud接口
CRUD是指在做計(jì)算處理時(shí)的增加(Create)、檢索(Retrieve)、更新(Update)和刪除(Delete)幾個(gè)單詞的首字母簡(jiǎn)寫(xiě)。CRUD主要被用在描述軟件系統(tǒng)中數(shù)據(jù)庫(kù)或者持久層的基本操作功能。
service層
如 UserService 用戶業(yè)務(wù)接口有自定義的方法,可創(chuàng)建 UserService 并繼承 IService ,接口實(shí)現(xiàn)類(lèi) UserServiceImpl 可繼承 ServiceImpl<BaseMapper<t>, T> ,實(shí)現(xiàn) UserService 接口并 實(shí)現(xiàn)接口定義方法。具體代碼如下:
繼承 IService 接口

三、分頁(yè)插件:
1.分頁(yè)插件
配置插件
//Spring boot方式
@EnableTransactionManagement
@Configuration
public class MybatisPlusConfig {
/**
* 分頁(yè)插件定義
* @return
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new
PaginationInterceptor();
// 設(shè)置請(qǐng)求的頁(yè)面大于最大頁(yè)后操作, true調(diào)回到首頁(yè),false 繼續(xù)請(qǐng)求 默認(rèn)false
// paginationInterceptor.setOverflow(false);
// 設(shè)置最大單頁(yè)限制數(shù)量,默認(rèn) 500 條,-1 不受限制
// paginationInterceptor.setLimit(500);
return paginationInterceptor;
}
}
使用分頁(yè)查詢(xún)
@Test
public void testPageList() {
Page<User> page = new Page<User>(1, 2);
userMapper.selectPage(page, null);
// 輸出page對(duì)象分頁(yè)查詢(xún)信息
System.out.println("總條數(shù):" + page.getTotal());
System.out.println("每頁(yè)顯示條數(shù):" + page.getSize());
System.out.println("總頁(yè)數(shù):" + page.getPages());
System.out.println("當(dāng)前頁(yè):" + page.getCurrent());
System.out.println("是否有上一頁(yè):" + page.hasPrevious());
System.out.println("是否有下一頁(yè):" + page.hasNext());
System.out.println("查詢(xún)結(jié)果:" + page.getRecords());
}
四、邏輯刪除
1. application.yml 加入配置(如果你的默認(rèn)值和mp默認(rèn)的一樣,該配置可無(wú)):
#以下為mybatis-plus配置
mybatis-plus:
global-config:
db-config:
logic-delete-field: flag #全局邏輯刪除字段值 3.3.0開(kāi)始支持,詳情看下面。
logic-delete-value: 1 # 邏輯已刪除值(默認(rèn)為 1)
logic-not-delete-value: 0 # 邏輯未刪除值(默認(rèn)為 0)
3. 在數(shù)據(jù)庫(kù)中加入邏輯刪除的字段,并且對(duì)應(yīng)實(shí)體類(lèi)中,該映射字段需要加入注解 @TableLogic
@Data
@TableName(value = "user") // 對(duì)應(yīng)數(shù)據(jù)庫(kù)表名
public class User {
......省略
/**
* 邏輯刪除字段
*/
@TableField(value="delete_flag")
@TableLogic
private Integer deleteFlag;
}
到此這篇關(guān)于Mybatis-Plus3.x的使用的文章就介紹到這了,更多相關(guān)Mybatis-Plus3.x使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot下mybatis-plus開(kāi)啟打印sql日志的配置指南
這篇文章主要給大家介紹了關(guān)于springboot下mybatis-plus開(kāi)啟打印sql日志的配置指南的相關(guān)資料,還介紹了關(guān)閉打印的方法,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-03-03
java實(shí)現(xiàn)連連看游戲課程設(shè)計(jì)
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)連連看游戲課程設(shè)計(jì),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
Java中生成微信小程序太陽(yáng)碼的實(shí)現(xiàn)方案
這篇文章主要介紹了Java中生成微信小程序太陽(yáng)碼的實(shí)現(xiàn)方案,本文講解了如何生成微信小程序太陽(yáng)碼,通過(guò)微信提供的兩種方案都可以實(shí)現(xiàn),在實(shí)際的項(xiàng)目中建議采用第二種方案,需要的朋友可以參考下2022-05-05
java 中模擬TCP傳輸?shù)目蛻舳撕头?wù)端實(shí)例詳解
這篇文章主要介紹了java 中模擬TCP傳輸?shù)目蛻舳撕头?wù)端實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-03-03
Tk.mybatis零sql語(yǔ)句實(shí)現(xiàn)動(dòng)態(tài)sql查詢(xún)的方法(4種)
有時(shí)候,查詢(xún)數(shù)據(jù)需要根據(jù)條件使用動(dòng)態(tài)查詢(xún),這時(shí)候需要使用動(dòng)態(tài)sql,本文主要介紹了Tk.mybatis零sql語(yǔ)句實(shí)現(xiàn)動(dòng)態(tài)sql查詢(xún)的方法,感興趣的可以了解一下2021-12-12
SpringCloud微服務(wù)熔斷器Hystrix使用詳解
這篇文章主要介紹了Spring Cloud Hyxtrix的基本使用,它是Spring Cloud中集成的一個(gè)組件,在整個(gè)生態(tài)中主要為我們提供服務(wù)隔離,服務(wù)熔斷,服務(wù)降級(jí)功能,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07
Python單元測(cè)試_使用裝飾器實(shí)現(xiàn)測(cè)試跳過(guò)和預(yù)期故障的方法
下面小編就為大家?guī)?lái)一篇Python單元測(cè)試_使用裝飾器實(shí)現(xiàn)測(cè)試跳過(guò)和預(yù)期故障的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06

