MyBatis-Plus使用簡介
阿新 • • 發佈:2021-02-16
文章目錄
前言
本文只做MyBatis-Plus簡單使用總結,想進行高階學習的小夥伴,建議直接官網學習。MyBaits-Plus官網。個人也做了將專案從MyBatis升級為MyBatis-Plus的工作,可以說不需要進行任何改動,目前還沒有發現任何問題,大家可以放心食用。
一、MyBatis-Plus是什麼?
MyBatis-Plus (opens new window)(簡稱 MP)是一個 MyBatis (opens new window)的增強工具,在 MyBatis 的基礎上只做增強不做改變,為簡化開發、提高效率而生。
二、使用步驟
1.引入庫
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.1</version>
</dependency>
2.增加配置
mybatis-plus:
mapper-locations: classpath:mapper/**/ *.xml
type-aliases-package: cn.biosan.cloud.servicehealthadmin.dao
3.啟動類增加包掃描
4.構建Service
package cn.biosan.cloud.servicehealthadmin.service.demo;
import cn.biosan.cloud.servicehealthadmin.entity.SysValue;
import com.baomidou.mybatisplus.extension.service.IService;
/**
* All rights Reserved, Designed By www.biosan.cn
*
* @Description:
* @author: 牛二強
* @date: 2021/1/29 14:13
*/
public interface DemoService extends IService<SysValue> {
}
5.構建ServiceImpl
package cn.biosan.cloud.servicehealthadmin.service.demo.impl;
import cn.biosan.cloud.servicehealthadmin.dao.DemoMapper;
import cn.biosan.cloud.servicehealthadmin.entity.SysValue;
import cn.biosan.cloud.servicehealthadmin.service.demo.DemoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
/**
* All rights Reserved, Designed By www.biosan.cn
* * @Description:
* @author: 牛二強
* @date: 2021/1/29 14:14
*/
@Service
public class DemoServiceImpl extends ServiceImpl<DemoMapper, SysValue> implements DemoService {
}
6.功能測試
- 注入Service
@Autowired
private DemoService demoService;
- 使用通用IService提供的方法
@Test
public void plusTest1(){
QueryWrapper query = new QueryWrapper();
query.eq("VALUE","AKIDwmRl18QdAWgolWwty3IPNBtTu9UDpKem");
SysValue sysValue = demoService.getOne(query);
System.out.println(JSON.toJSONString(sysValue));
}
7.分頁的使用
-
使用自帶的IPage
增加配置類
package cn.biosan.cloud.servicehealthadmin.config;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.pagination.optimize.JsqlParserCountOptimize;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* All rights Reserved, Designed By www.biosan.cn
*
* @Description:
* @author: 牛二強
* @date: 2021/1/28 14:55
*/
@Configuration
public class MybatisPlusConfig {
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
// 設定請求的頁面大於最大頁後操作, true調回到首頁,false 繼續請求 預設false
// paginationInterceptor.setOverflow(false);
// 設定最大單頁限制數量,預設 500 條,-1 不受限制
// paginationInterceptor.setLimit(500);
// 開啟 count 的 join 優化,只針對部分 left join
paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize());
return paginationInterceptor;
}
}
demo測試
@Test
public void testIPage(){
IPage<SysValue> page = new Page<SysValue>(1,1);
QueryWrapper query = new QueryWrapper<>();
query.eq("VALUE", "11592158");
IPage iPage = demoService.page(page, query);
System.out.println(JSON.toJSONString(iPage));
//頁數
System.out.println("頁數 :" + iPage.getCurrent());
//分頁大小
System.out.println("分頁大小 :" + iPage.getSize());
//總條數
System.out.println("總條數 :" + iPage.getTotal());
//獲取結果
System.out.println(JSON.toJSONString(iPage.getRecords()));
}
- 使用外掛PageHelper
引入依賴
<!--pageHelper-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
<exclusions>
<exclusion>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</exclusion>
<exclusion>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
</exclusion>
</exclusions>
</dependency>
增加配置
pagehelper:
auto-dialect: mysql
reasonable: true
support-methods-arguments: true
page-size-zero: true
params: count=countSql
demo測試
@Test
public void testPageHelper(){
PageHelper.startPage(1,1);
QueryWrapper query = new QueryWrapper<>();
query.eq("VALUE", "11592158");
List<SysValue> list = demoService.list(query);
System.out.println(JSON.toJSONString(list));
PageInfo<SysValue> info = new PageInfo<>(list);
//頁數
System.out.println("頁數 :" + info.getPageNum());
//分頁大小
System.out.println("分頁大小 :" + info.getPageSize());
//總條數
System.out.println("總條數 :" + info.getTotal());
//總頁數
System.out.println("總頁數 :" + info.getPages());
//獲取結果
System.out.println(JSON.toJSONString(info.getList()));
}
據個人目前瞭解,mybatis-plus自帶的IPage不能直接獲取到總頁數,但是這個值對於我們頁面顯示其實也是不可缺少的。對於這個問題PageHelper就直接幫我們處理好了,而且根據個人使用體驗PageHelper也更加方便,因此比較推薦使用。
總結
以上就是今天要講的內容,本文僅僅簡單介紹了Mybatis-Plus的使用,而Mybatis-Plus提供了大量能使我們快速便捷地處理資料的函式和方法。如有興趣,可以自行到官網學習。