Springboot接入MyBatisPlus的實(shí)現(xiàn)
1、什么是MyBatisPlus?
Mybatis-Plus(簡(jiǎn)稱MP)是一個(gè) Mybatis 的增強(qiáng)工具,在 Mybatis 的基礎(chǔ)上只做增強(qiáng)不做改變,為簡(jiǎn)化開發(fā)、提高效率而生。
通過(guò)封裝一些基礎(chǔ)通用的curd方法,我們不用再在xml文件中編寫sql語(yǔ)句,就可以直接調(diào)用api進(jìn)行對(duì)數(shù)據(jù)庫(kù)的操作。
2、MyBatisPlus環(huán)境準(zhǔn)備
2.1 創(chuàng)建一個(gè)springboot項(xiàng)目,在pom文件下添加如下依賴:
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>2.2 創(chuàng)建一個(gè)包用來(lái)存放 mapper 文件
2.3 在 Spring Boot 主類上面使用 @MapperScan 注解掃描我們自定義的 mapper
2.4 自定義自己的 mapper,該 mapper 將繼承 com.baomidou.mybatisplus.core.mapper.BaseMapper
2.5 在我們的服務(wù)中使用 @Autowired 注解自動(dòng)注入自定義的 mapper
3、具體使用
3.1 基礎(chǔ)使用
@Mapper
public interface OnlinePriceMapper extends BaseMapper<OnlinePrice> {
}mapper中沒有定義任何方法
package com.online.analyze.service.impl;
import com.online.analyze.mapper.OnlinePriceMapper;
import com.online.analyze.model.OnlinePrice;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.Date;
/**
* @author lijianxi
* @date 2022年03月18日 11:13 上午
*/
@SpringBootTest
public class MapperTest {
@Autowired
OnlinePriceMapper onlinePriceMapper;
@Test
public void testMapper() {
OnlinePrice onlinePrice = new OnlinePrice();
onlinePrice.setId(3999222L);
onlinePrice.setAddDate(new Date());
onlinePrice.setDescription("test");
onlinePrice.setSummary("test");
onlinePrice.setProcessMethod("修改數(shù)據(jù)");
//新增
int result = onlinePriceMapper.insert(onlinePrice);
if (result > 0) {
System.out.println("插入成功");
}
//查詢
OnlinePrice price = onlinePriceMapper.selectById(3999222L);
System.out.println(price.getDescription() + "查詢成功");
//更新
onlinePrice.setDescription("修改后");
onlinePriceMapper.updateById(onlinePrice);
System.out.println(onlinePriceMapper.selectById(3999222L).getDescription() + "修改成功");
//刪除
if (onlinePriceMapper.deleteById(3999222L) > 0) {
System.out.println("刪除成功");
}
}
}通過(guò)注入mapper,就可以使用 mapper 的 insert(插入)、selectById(根據(jù)ID查找)、updateById(根據(jù)ID更新)和 deleteById(根據(jù)ID刪除)等簡(jiǎn)單的 CRUD 操作
常用的增刪改查方法可以查看BaseMapper接口
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package com.baomidou.mybatisplus.core.mapper;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Param;
public interface BaseMapper<T> extends Mapper<T> {
int insert(T entity);
int deleteById(Serializable id);
int deleteByMap(@Param("cm") Map<String, Object> columnMap);
int delete(@Param("ew") Wrapper<T> queryWrapper);
int deleteBatchIds(@Param("coll") Collection<? extends Serializable> idList);
int updateById(@Param("et") T entity);
int update(@Param("et") T entity, @Param("ew") Wrapper<T> updateWrapper);
T selectById(Serializable id);
List<T> selectBatchIds(@Param("coll") Collection<? extends Serializable> idList);
List<T> selectByMap(@Param("cm") Map<String, Object> columnMap);
T selectOne(@Param("ew") Wrapper<T> queryWrapper);
Integer selectCount(@Param("ew") Wrapper<T> queryWrapper);
List<T> selectList(@Param("ew") Wrapper<T> queryWrapper);
List<Map<String, Object>> selectMaps(@Param("ew") Wrapper<T> queryWrapper);
List<Object> selectObjs(@Param("ew") Wrapper<T> queryWrapper);
<E extends IPage<T>> E selectPage(E page, @Param("ew") Wrapper<T> queryWrapper);
<E extends IPage<Map<String, Object>>> E selectMapsPage(E page, @Param("ew") Wrapper<T> queryWrapper);
}3.2 wapper構(gòu)建
除了通過(guò)已定義的方法來(lái)查詢還可以通過(guò) Wrapper 構(gòu)建查詢條件
@Test
public void testMapper(){
//查詢id為3999222數(shù)據(jù)信息
QueryWrapper<OnlinePrice> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("id", 3999222L);
OnlinePrice onlinePrice = onlinePriceMapper.selectOne(queryWrapper);
System.out.println(onlinePrice.getId());
//查詢添加日期在區(qū)間內(nèi)并且user_city不為null的數(shù)據(jù)
QueryWrapper<OnlinePrice> queryWrapper1 = new QueryWrapper<>();
queryWrapper1.between("add_date","2022-02-02","2022-02-10");
queryWrapper1.isNotNull("user_city");
String summary ="test";
// 第一個(gè)參數(shù)為是否執(zhí)行條件,為true則執(zhí)行該條件
queryWrapper1.eq(StringUtils.isNotBlank(summary),"summary",summary);
List<OnlinePrice> onlinePrices = onlinePriceMapper.selectList(queryWrapper1);
for (OnlinePrice price : onlinePrices) {
System.out.println(price);
}
}3.3分頁(yè)功能
@Test
public void testPage() {
QueryWrapper<OnlinePrice> queryWrapper = new QueryWrapper<>();
queryWrapper.isNotNull("description");
//每頁(yè)四個(gè)
Page<OnlinePrice> page = new Page<>(1, 4);
Page<OnlinePrice> onlinePricePage = onlinePriceMapper.selectPage(page, queryWrapper);
for (OnlinePrice record : page.getRecords()) {
System.out.println(record);
}
}3.4 service層接口
除了 BaseMapper 接口,MyBatis Plus 還提供了 IService 接口,該接口對(duì)應(yīng) Service 層。MyBatis Plus 的通用 Service CRUD 實(shí)現(xiàn)了 IService 接口,進(jìn)一步封裝 CRUD
該接口使用 get(查詢單行)、remove(刪除)、list(查詢集合)和 page(分頁(yè))前綴命名的方式進(jìn)行區(qū)別。
@Autowired
OnlinePriceService onlinePriceService;
@Test
public void testService(){
OnlinePrice price = new OnlinePrice();
price.setId(22222222L);
price.setDescription("test");
onlinePriceService.save(price);
QueryWrapper<OnlinePrice> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("description","test");
OnlinePrice one = onlinePriceService.getOne(queryWrapper);
Page<OnlinePrice> page = new Page<>(1,4);
QueryWrapper<OnlinePrice> queryWrapper1 = new QueryWrapper<>();
queryWrapper1.between("add_date", "2022-02-02", "2022-02-10");
onlinePriceService.page(page,queryWrapper1);
}以上介紹了mybatisplus的常用基礎(chǔ)用法,mybatisplus還有自動(dòng)代碼生成等其他功能,可以自動(dòng)生成代碼,更多相關(guān)Springboot接入MyBatisPlus內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解IDEA JUnit5測(cè)試套件運(yùn)行錯(cuò)誤的問題
這篇文章主要介紹了詳解IDEA JUnit5測(cè)試套件運(yùn)行錯(cuò)誤的問題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
spring?data?jpa如何使用自定義repository實(shí)現(xiàn)類
這篇文章主要介紹了spring?data?jpa如何使用自定義repository實(shí)現(xiàn)類,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
SpringBoot整合定時(shí)任務(wù)之實(shí)現(xiàn)Scheduled注解的過(guò)程(一個(gè)注解全解決)
這篇文章主要介紹了SpringBoot整合定時(shí)任務(wù)之實(shí)現(xiàn)Scheduled注解的過(guò)程(一個(gè)注解全解決),本文通過(guò)使用場(chǎng)景分析給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09
使用jd-gui反編譯修改jar包里的.class并重新生成新jar問題
這篇文章主要介紹了使用jd-gui反編譯修改jar包里的.class并重新生成新jar問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
IDEA項(xiàng)目啟動(dòng)時(shí)Flyway數(shù)據(jù)庫(kù)遷移中的checksum不匹配問題及最新解決方案
面對(duì)IDEA項(xiàng)目啟動(dòng)時(shí)報(bào)出的Flyway遷移校驗(yàn)和不匹配問題,核心在于保持遷移腳本的一致性、正確管理和理解Flyway的工作機(jī)制,本文介紹IDEA項(xiàng)目啟動(dòng)時(shí)Flyway數(shù)據(jù)庫(kù)遷移中的checksum不匹配問題及最新解決方案,感興趣的朋友一起看看吧2024-01-01
SpringBoot結(jié)合Redis哨兵模式的實(shí)現(xiàn)示例
這篇文章主要介紹了SpringBoot結(jié)合Redis哨兵模式的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
Java終止循環(huán)體的具體實(shí)現(xiàn)
這篇文章主要介紹了Java終止循環(huán)體的具體實(shí)現(xiàn),需要的朋友可以參考下2014-02-02
Java中Hashtable類與HashMap類的區(qū)別詳解
Hashtable的應(yīng)用非常廣泛,HashMap是新框架中用來(lái)代替Hashtable的類,也就是說(shuō)建議使用HashMap,不要使用Hashtable。可能你覺得Hashtable很好用,為什么不用呢?這里簡(jiǎn)單分析他們的區(qū)別。2016-01-01

