Springboot接入MyBatisPlus的實(shí)現(xiàn)
1、什么是MyBatisPlus?
Mybatis-Plus(簡稱MP)是一個(gè) Mybatis 的增強(qiáng)工具,在 Mybatis 的基礎(chǔ)上只做增強(qiáng)不做改變,為簡化開發(fā)、提高效率而生。
通過封裝一些基礎(chǔ)通用的curd方法,我們不用再在xml文件中編寫sql語句,就可以直接調(diào)用api進(jìn)行對(duì)數(shù)據(jù)庫的操作。
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è)包用來存放 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("刪除成功"); } } }
通過注入mapper,就可以使用 mapper 的 insert(插入)、selectById(根據(jù)ID查找)、updateById(根據(jù)ID更新)和 deleteById(根據(jù)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構(gòu)建
除了通過已定義的方法來查詢還可以通過 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分頁功能
@Test public void testPage() { QueryWrapper<OnlinePrice> queryWrapper = new QueryWrapper<>(); queryWrapper.isNotNull("description"); //每頁四個(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(分頁)前綴命名的方式進(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)文章
SpringBoot快速整合Mybatis、MybatisPlus(代碼生成器)實(shí)現(xiàn)數(shù)據(jù)庫訪問功能
這篇文章主要介紹了SpringBoot快速整合Mybatis、MybatisPlus(代碼生成器)實(shí)現(xiàn)數(shù)據(jù)庫訪問功能,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04Servlet實(shí)現(xiàn)統(tǒng)計(jì)頁面訪問次數(shù)功能
這篇文章主要介紹了Servlet實(shí)現(xiàn)統(tǒng)計(jì)頁面訪問次數(shù)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04Java圖形化編程之JFrame疫苗接種系統(tǒng)詳解
GUI圖形界面設(shè)計(jì)是用戶和程序交互的工具,用戶通過圖形界面控制程序事件的發(fā)生。首先介紹Swing的基本體系結(jié)構(gòu),這是底層2021-09-09Java實(shí)現(xiàn)文件分割和文件合并實(shí)例
本篇文章主要介紹了Java實(shí)現(xiàn)文件分割和文件合并實(shí)例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08