Springboot接入MyBatisPlus的实现

 更新时间:2023年09月18日 11:41:37   作者:夜阑卧听风吹雨,铁马冰河入梦来  
最近web端比较热门的框架就是SpringBoot和Mybatis-Plus,这里简单总结集成用法,具有一定的参考价值,感兴趣的可以了解一下

1、什么是MyBatisPlus?

Mybatis-Plus(简称MP)是一个 Mybatis 的增强工具,在 Mybatis 的基础上只做增强不做改变,为简化开发、提高效率而生。

通过封装一些基础通用的curd方法,我们不用再在xml文件中编写sql语句,就可以直接调用api进行对数据库的操作。

2、MyBatisPlus环境准备

2.1 创建一个springboot项目,在pom文件下添加如下依赖:

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.1</version>
</dependency>

2.2 创建一个包用来存放 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());
        //查询添加日期在区间内并且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";
        // 第一个参数为是否执行条件,为true则执行该条件
        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(分页)前缀命名的方式进行区别。

    @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内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 详解IDEA JUnit5测试套件运行错误的问题

    详解IDEA JUnit5测试套件运行错误的问题

    这篇文章主要介绍了详解IDEA JUnit5测试套件运行错误的问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-12-12
  • spring data jpa如何使用自定义repository实现类

    spring data jpa如何使用自定义repository实现类

    这篇文章主要介绍了spring data jpa如何使用自定义repository实现类,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-11-11
  • SpringBoot整合定时任务之实现Scheduled注解的过程(一个注解全解决)

    SpringBoot整合定时任务之实现Scheduled注解的过程(一个注解全解决)

    这篇文章主要介绍了SpringBoot整合定时任务之实现Scheduled注解的过程(一个注解全解决),本文通过使用场景分析给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-09-09
  • 使用jd-gui反编译修改jar包里的.class并重新生成新jar问题

    使用jd-gui反编译修改jar包里的.class并重新生成新jar问题

    这篇文章主要介绍了使用jd-gui反编译修改jar包里的.class并重新生成新jar问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-05-05
  • 详析Spring中依赖注入的三种方式

    详析Spring中依赖注入的三种方式

    在开发的过程中突然对Spring的依赖注入几种方式出现混交,打算做个简单的小结,方便大家和自己以后参考借鉴,如有总结不对的地方,请大家不吝指教!下面来一起看看吧。
    2016-09-09
  • IDEA项目启动时Flyway数据库迁移中的checksum不匹配问题及最新解决方案

    IDEA项目启动时Flyway数据库迁移中的checksum不匹配问题及最新解决方案

    面对IDEA项目启动时报出的Flyway迁移校验和不匹配问题,核心在于保持迁移脚本的一致性、正确管理和理解Flyway的工作机制,本文介绍IDEA项目启动时Flyway数据库迁移中的checksum不匹配问题及最新解决方案,感兴趣的朋友一起看看吧
    2024-01-01
  • Java8新特性Optional常用方法

    Java8新特性Optional常用方法

    optional类是Java8新增加的一个对象容器,主要的功能有对象的创建、获取、判断、过滤,映射等,下面这篇文章主要给大家介绍了关于Java8新特性Optional常用方法的相关资料,需要的朋友可以参考下
    2024-02-02
  • SpringBoot结合Redis哨兵模式的实现示例

    SpringBoot结合Redis哨兵模式的实现示例

    这篇文章主要介绍了SpringBoot结合Redis哨兵模式的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-04-04
  • Java终止循环体的具体实现

    Java终止循环体的具体实现

    这篇文章主要介绍了Java终止循环体的具体实现,需要的朋友可以参考下
    2014-02-02
  • Java中Hashtable类与HashMap类的区别详解

    Java中Hashtable类与HashMap类的区别详解

    Hashtable的应用非常广泛,HashMap是新框架中用来代替Hashtable的类,也就是说建议使用HashMap,不要使用Hashtable。可能你觉得Hashtable很好用,为什么不用呢?这里简单分析他们的区别。
    2016-01-01

最新评论