Springboot接入MyBatisPlus的實現
1、什么是MyBatisPlus?
Mybatis-Plus(簡稱MP)是一個 Mybatis 的增強工具,在 Mybatis 的基礎上只做增強不做改變,為簡化開發(fā)、提高效率而生。
通過封裝一些基礎通用的curd方法,我們不用再在xml文件中編寫sql語句,就可以直接調用api進行對數據庫的操作。
2、MyBatisPlus環(huán)境準備
2.1 創(chuàng)建一個springboot項目,在pom文件下添加如下依賴:
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>2.2 創(chuàng)建一個包用來存放 mapper 文件
2.3 在 Spring Boot 主類上面使用 @MapperScan 注解掃描我們自定義的 mapper
2.4 自定義自己的 mapper,該 mapper 將繼承 com.baomidou.mybatisplus.core.mapper.BaseMapper
2.5 在我們的服務中使用 @Autowired 注解自動注入自定義的 mapper
3、具體使用
3.1 基礎使用
@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("修改數據");
//新增
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("刪除成功");
}
}
}通過注入mapper,就可以使用 mapper 的 insert(插入)、selectById(根據ID查找)、updateById(根據ID更新)和 deleteById(根據ID刪除)等簡單的 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構建
除了通過已定義的方法來查詢還可以通過 Wrapper 構建查詢條件
@Test
public void testMapper(){
//查詢id為3999222數據信息
QueryWrapper<OnlinePrice> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("id", 3999222L);
OnlinePrice onlinePrice = onlinePriceMapper.selectOne(queryWrapper);
System.out.println(onlinePrice.getId());
//查詢添加日期在區(qū)間內并且user_city不為null的數據
QueryWrapper<OnlinePrice> queryWrapper1 = new QueryWrapper<>();
queryWrapper1.between("add_date","2022-02-02","2022-02-10");
queryWrapper1.isNotNull("user_city");
String summary ="test";
// 第一個參數為是否執(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分頁功能
@Test
public void testPage() {
QueryWrapper<OnlinePrice> queryWrapper = new QueryWrapper<>();
queryWrapper.isNotNull("description");
//每頁四個
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 接口,該接口對應 Service 層。MyBatis Plus 的通用 Service CRUD 實現了 IService 接口,進一步封裝 CRUD
該接口使用 get(查詢單行)、remove(刪除)、list(查詢集合)和 page(分頁)前綴命名的方式進行區(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的常用基礎用法,mybatisplus還有自動代碼生成等其他功能,可以自動生成代碼,更多相關Springboot接入MyBatisPlus內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
spring?data?jpa如何使用自定義repository實現類
這篇文章主要介紹了spring?data?jpa如何使用自定義repository實現類,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11
SpringBoot整合定時任務之實現Scheduled注解的過程(一個注解全解決)
這篇文章主要介紹了SpringBoot整合定時任務之實現Scheduled注解的過程(一個注解全解決),本文通過使用場景分析給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09
使用jd-gui反編譯修改jar包里的.class并重新生成新jar問題
這篇文章主要介紹了使用jd-gui反編譯修改jar包里的.class并重新生成新jar問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05
IDEA項目啟動時Flyway數據庫遷移中的checksum不匹配問題及最新解決方案
面對IDEA項目啟動時報出的Flyway遷移校驗和不匹配問題,核心在于保持遷移腳本的一致性、正確管理和理解Flyway的工作機制,本文介紹IDEA項目啟動時Flyway數據庫遷移中的checksum不匹配問題及最新解決方案,感興趣的朋友一起看看吧2024-01-01
Java中Hashtable類與HashMap類的區(qū)別詳解
Hashtable的應用非常廣泛,HashMap是新框架中用來代替Hashtable的類,也就是說建議使用HashMap,不要使用Hashtable??赡苣阌X得Hashtable很好用,為什么不用呢?這里簡單分析他們的區(qū)別。2016-01-01

