Mybatis-Plus3.x的創(chuàng)建步驟及使用教程
MyBatis-Plus(簡稱 MP)是一個 MyBatis 的增強工具,在 MyBatis 的基礎上只做增強不做改變,為 簡化開發(fā)、提高效率而生。
一、引入
創(chuàng)建步驟:
1.創(chuàng)建Spring Boot工程
2.添加依賴
引入 Spring Boot Starter 父工程:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/>
</parent>
引入相關其他依賴
<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相關依賴
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.0</version>
</dependency>
<!-- 引入mysql依賴 -->
<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相關配置
mybatis-plus:
type-aliases-package: com.hz.entity #類型別名所在的包
#控制臺打印sql語句
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開始支持,詳情看下面。
logic-delete-value: 1 #邏輯已刪除值(默認為 1)
logic-not-delete-value: 0 #邏輯未刪除值(默認為 0)
#數(shù)據(jù)庫鏈接
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 啟動類中添加 @MapperScan 注解

編碼:
在mapper類中繼承Basemapper

測試:

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

三、分頁插件:
1.分頁插件
配置插件
//Spring boot方式
@EnableTransactionManagement
@Configuration
public class MybatisPlusConfig {
/**
* 分頁插件定義
* @return
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new
PaginationInterceptor();
// 設置請求的頁面大于最大頁后操作, true調回到首頁,false 繼續(xù)請求 默認false
// paginationInterceptor.setOverflow(false);
// 設置最大單頁限制數(shù)量,默認 500 條,-1 不受限制
// paginationInterceptor.setLimit(500);
return paginationInterceptor;
}
}
使用分頁查詢
@Test
public void testPageList() {
Page<User> page = new Page<User>(1, 2);
userMapper.selectPage(page, null);
// 輸出page對象分頁查詢信息
System.out.println("總條數(shù):" + page.getTotal());
System.out.println("每頁顯示條數(shù):" + page.getSize());
System.out.println("總頁數(shù):" + page.getPages());
System.out.println("當前頁:" + page.getCurrent());
System.out.println("是否有上一頁:" + page.hasPrevious());
System.out.println("是否有下一頁:" + page.hasNext());
System.out.println("查詢結果:" + page.getRecords());
}
四、邏輯刪除
1. application.yml 加入配置(如果你的默認值和mp默認的一樣,該配置可無):
#以下為mybatis-plus配置
mybatis-plus:
global-config:
db-config:
logic-delete-field: flag #全局邏輯刪除字段值 3.3.0開始支持,詳情看下面。
logic-delete-value: 1 # 邏輯已刪除值(默認為 1)
logic-not-delete-value: 0 # 邏輯未刪除值(默認為 0)
3. 在數(shù)據(jù)庫中加入邏輯刪除的字段,并且對應實體類中,該映射字段需要加入注解 @TableLogic
@Data
@TableName(value = "user") // 對應數(shù)據(jù)庫表名
public class User {
......省略
/**
* 邏輯刪除字段
*/
@TableField(value="delete_flag")
@TableLogic
private Integer deleteFlag;
}
到此這篇關于Mybatis-Plus3.x的使用的文章就介紹到這了,更多相關Mybatis-Plus3.x使用內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
詳解Elastic Search搜索引擎在SpringBoot中的實踐
本篇文章主要介紹了Elastic Search搜索引擎在SpringBoot中的實踐,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-01-01
Spring AOP如何實現(xiàn)注解式的Mybatis多數(shù)據(jù)源切換詳解
這篇文章主要給大家介紹了關于Spring AOP如何實現(xiàn)注解式的Mybatis多數(shù)據(jù)源切換的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-11-11

