Mybatis-Plus3.x的創(chuàng)建步驟及使用教程
MyBatis-Plus(簡稱 MP)是一個(gè) MyBatis 的增強(qiáng)工具,在 MyBatis 的基礎(chǔ)上只做增強(qiáng)不做改變,為 簡化開發(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>
引入相關(guān)其他依賴
<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)依賴
<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相關(guān)配置
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 #邏輯已刪除值(默認(rèn)為 1) logic-not-delete-value: 0 #邏輯未刪除值(默認(rèn)為 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 內(nèi)置的條件封裝器 Wrapper ,所以 不填寫就是無任何條件
3.若需要自定義DAO接口,則需要在yml中讀取mapper文件,
mybatis-plus:mapperlocations: classpath:mappers/*.xml
二、通用crud接口
CRUD是指在做計(jì)算處理時(shí)的增加(Create)、檢索(Retrieve)、更新(Update)和刪除(Delete)幾個(gè)單詞的首字母簡寫。CRUD主要被用在描述軟件系統(tǒng)中數(shù)據(jù)庫或者持久層的基本操作功能。
service層
如 UserService 用戶業(yè)務(wù)接口有自定義的方法,可創(chuàng)建 UserService 并繼承 IService ,接口實(shí)現(xiàn)類 UserServiceImpl 可繼承 ServiceImpl<BaseMapper<t>, T> ,實(shí)現(xiàn) UserService 接口并 實(shí)現(xiàn)接口定義方法。具體代碼如下:
繼承 IService 接口
三、分頁插件:
1.分頁插件
配置插件
//Spring boot方式 @EnableTransactionManagement @Configuration public class MybatisPlusConfig { /** * 分頁插件定義 * @return */ @Bean public PaginationInterceptor paginationInterceptor() { PaginationInterceptor paginationInterceptor = new PaginationInterceptor(); // 設(shè)置請求的頁面大于最大頁后操作, true調(diào)回到首頁,false 繼續(xù)請求 默認(rèn)false // paginationInterceptor.setOverflow(false); // 設(shè)置最大單頁限制數(shù)量,默認(rèn) 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("當(dāng)前頁:" + page.getCurrent()); System.out.println("是否有上一頁:" + page.hasPrevious()); System.out.println("是否有下一頁:" + page.hasNext()); System.out.println("查詢結(jié)果:" + page.getRecords()); }
四、邏輯刪除
1. application.yml 加入配置(如果你的默認(rèn)值和mp默認(rèn)的一樣,該配置可無):
#以下為mybatis-plus配置 mybatis-plus: global-config: db-config: logic-delete-field: flag #全局邏輯刪除字段值 3.3.0開始支持,詳情看下面。 logic-delete-value: 1 # 邏輯已刪除值(默認(rèn)為 1) logic-not-delete-value: 0 # 邏輯未刪除值(默認(rèn)為 0)
3. 在數(shù)據(jù)庫中加入邏輯刪除的字段,并且對應(yīng)實(shí)體類中,該映射字段需要加入注解 @TableLogic
@Data @TableName(value = "user") // 對應(yīng)數(shù)據(jù)庫表名 public class User { ......省略 /** * 邏輯刪除字段 */ @TableField(value="delete_flag") @TableLogic private Integer deleteFlag; }
到此這篇關(guān)于Mybatis-Plus3.x的使用的文章就介紹到這了,更多相關(guān)Mybatis-Plus3.x使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot下mybatis-plus開啟打印sql日志的配置指南
這篇文章主要給大家介紹了關(guān)于springboot下mybatis-plus開啟打印sql日志的配置指南的相關(guān)資料,還介紹了關(guān)閉打印的方法,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-03-03java實(shí)現(xiàn)連連看游戲課程設(shè)計(jì)
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)連連看游戲課程設(shè)計(jì),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05Java中生成微信小程序太陽碼的實(shí)現(xiàn)方案
這篇文章主要介紹了Java中生成微信小程序太陽碼的實(shí)現(xiàn)方案,本文講解了如何生成微信小程序太陽碼,通過微信提供的兩種方案都可以實(shí)現(xiàn),在實(shí)際的項(xiàng)目中建議采用第二種方案,需要的朋友可以參考下2022-05-05java 中模擬TCP傳輸?shù)目蛻舳撕头?wù)端實(shí)例詳解
這篇文章主要介紹了java 中模擬TCP傳輸?shù)目蛻舳撕头?wù)端實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-03-03Tk.mybatis零sql語句實(shí)現(xiàn)動態(tài)sql查詢的方法(4種)
有時(shí)候,查詢數(shù)據(jù)需要根據(jù)條件使用動態(tài)查詢,這時(shí)候需要使用動態(tài)sql,本文主要介紹了Tk.mybatis零sql語句實(shí)現(xiàn)動態(tài)sql查詢的方法,感興趣的可以了解一下2021-12-12SpringCloud微服務(wù)熔斷器Hystrix使用詳解
這篇文章主要介紹了Spring Cloud Hyxtrix的基本使用,它是Spring Cloud中集成的一個(gè)組件,在整個(gè)生態(tài)中主要為我們提供服務(wù)隔離,服務(wù)熔斷,服務(wù)降級功能,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07Python單元測試_使用裝飾器實(shí)現(xiàn)測試跳過和預(yù)期故障的方法
下面小編就為大家?guī)硪黄狿ython單元測試_使用裝飾器實(shí)現(xiàn)測試跳過和預(yù)期故障的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06