• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Springboot接入MyBatisPlus

武飞扬头像
夜阑卧听风吹雨,铁马冰河入梦来
帮助3

1、什么是MyBatisPlus?

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

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

2、MyBatisPlus环境准备

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

  1.  
    <dependency>
  2.  
    <groupId>com.baomidou</groupId>
  3.  
    <artifactId>mybatis-plus-boot-starter</artifactId>
  4.  
    <version>3.4.1</version>
  5.  
    </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 基础使用

  1.  
    @Mapper
  2.  
    public interface OnlinePriceMapper extends BaseMapper<OnlinePrice> {
  3.  
     
  4.  
    }

mapper中没有定义任何方法

  1.  
    package com.online.analyze.service.impl;
  2.  
     
  3.  
    import com.online.analyze.mapper.OnlinePriceMapper;
  4.  
    import com.online.analyze.model.OnlinePrice;
  5.  
    import org.junit.jupiter.api.Test;
  6.  
    import org.springframework.beans.factory.annotation.Autowired;
  7.  
    import org.springframework.boot.test.context.SpringBootTest;
  8.  
     
  9.  
    import java.util.Date;
  10.  
     
  11.  
    /**
  12.  
    * @author lijianxi
  13.  
    * @date 2022年03月18日 11:13 上午
  14.  
    */
  15.  
    @SpringBootTest
  16.  
    public class MapperTest {
  17.  
    @Autowired
  18.  
    OnlinePriceMapper onlinePriceMapper;
  19.  
     
  20.  
    @Test
  21.  
    public void testMapper() {
  22.  
    OnlinePrice onlinePrice = new OnlinePrice();
  23.  
    onlinePrice.setId(3999222L);
  24.  
    onlinePrice.setAddDate(new Date());
  25.  
    onlinePrice.setDescription("test");
  26.  
    onlinePrice.setSummary("test");
  27.  
    onlinePrice.setProcessMethod("修改数据");
  28.  
    //新增
  29.  
    int result = onlinePriceMapper.insert(onlinePrice);
  30.  
    if (result > 0) {
  31.  
    System.out.println("插入成功");
  32.  
    }
  33.  
    //查询
  34.  
    OnlinePrice price = onlinePriceMapper.selectById(3999222L);
  35.  
    System.out.println(price.getDescription() "查询成功");
  36.  
    //更新
  37.  
    onlinePrice.setDescription("修改后");
  38.  
    onlinePriceMapper.updateById(onlinePrice);
  39.  
    System.out.println(onlinePriceMapper.selectById(3999222L).getDescription() "修改成功");
  40.  
    //删除
  41.  
    if (onlinePriceMapper.deleteById(3999222L) > 0) {
  42.  
    System.out.println("删除成功");
  43.  
    }
  44.  
     
  45.  
     
  46.  
    }
  47.  
    }
学新通

通过注入mapper,就可以使用 mapper 的 insert(插入)、selectById(根据ID查找)、updateById(根据ID更新)和 deleteById(根据ID删除)等简单的 CRUD 操作

常用的增删改查方法可以查看BaseMapper接口

  1.  
    //
  2.  
    // Source code recreated from a .class file by IntelliJ IDEA
  3.  
    // (powered by FernFlower decompiler)
  4.  
    //
  5.  
     
  6.  
    package com.baomidou.mybatisplus.core.mapper;
  7.  
     
  8.  
    import com.baomidou.mybatisplus.core.conditions.Wrapper;
  9.  
    import com.baomidou.mybatisplus.core.metadata.IPage;
  10.  
    import java.io.Serializable;
  11.  
    import java.util.Collection;
  12.  
    import java.util.List;
  13.  
    import java.util.Map;
  14.  
    import org.apache.ibatis.annotations.Param;
  15.  
     
  16.  
    public interface BaseMapper<T> extends Mapper<T> {
  17.  
    int insert(T entity);
  18.  
     
  19.  
    int deleteById(Serializable id);
  20.  
     
  21.  
    int deleteByMap(@Param("cm") Map<String, Object> columnMap);
  22.  
     
  23.  
    int delete(@Param("ew") Wrapper<T> queryWrapper);
  24.  
     
  25.  
    int deleteBatchIds(@Param("coll") Collection<? extends Serializable> idList);
  26.  
     
  27.  
    int updateById(@Param("et") T entity);
  28.  
     
  29.  
    int update(@Param("et") T entity, @Param("ew") Wrapper<T> updateWrapper);
  30.  
     
  31.  
    T selectById(Serializable id);
  32.  
     
  33.  
    List<T> selectBatchIds(@Param("coll") Collection<? extends Serializable> idList);
  34.  
     
  35.  
    List<T> selectByMap(@Param("cm") Map<String, Object> columnMap);
  36.  
     
  37.  
    T selectOne(@Param("ew") Wrapper<T> queryWrapper);
  38.  
     
  39.  
    Integer selectCount(@Param("ew") Wrapper<T> queryWrapper);
  40.  
     
  41.  
    List<T> selectList(@Param("ew") Wrapper<T> queryWrapper);
  42.  
     
  43.  
    List<Map<String, Object>> selectMaps(@Param("ew") Wrapper<T> queryWrapper);
  44.  
     
  45.  
    List<Object> selectObjs(@Param("ew") Wrapper<T> queryWrapper);
  46.  
     
  47.  
    <E extends IPage<T>> E selectPage(E page, @Param("ew") Wrapper<T> queryWrapper);
  48.  
     
  49.  
    <E extends IPage<Map<String, Object>>> E selectMapsPage(E page, @Param("ew") Wrapper<T> queryWrapper);
  50.  
    }
学新通

3.2 wapper构建

除了通过已定义的方法来查询还可以通过 Wrapper 构建查询条件

  1.  
    @Test
  2.  
    public void testMapper(){
  3.  
    //查询id为3999222数据信息
  4.  
    QueryWrapper<OnlinePrice> queryWrapper = new QueryWrapper<>();
  5.  
    queryWrapper.eq("id", 3999222L);
  6.  
    OnlinePrice onlinePrice = onlinePriceMapper.selectOne(queryWrapper);
  7.  
    System.out.println(onlinePrice.getId());
  8.  
    //查询添加日期在区间内并且user_city不为null的数据
  9.  
    QueryWrapper<OnlinePrice> queryWrapper1 = new QueryWrapper<>();
  10.  
    queryWrapper1.between("add_date","2022-02-02","2022-02-10");
  11.  
    queryWrapper1.isNotNull("user_city");
  12.  
    String summary ="test";
  13.  
    // 第一个参数为是否执行条件,为true则执行该条件
  14.  
    queryWrapper1.eq(StringUtils.isNotBlank(summary),"summary",summary);
  15.  
    List<OnlinePrice> onlinePrices = onlinePriceMapper.selectList(queryWrapper1);
  16.  
    for (OnlinePrice price : onlinePrices) {
  17.  
    System.out.println(price);
  18.  
    }
  19.  
    }
学新通

3.3分页功能

  1.  
    @Test
  2.  
    public void testPage() {
  3.  
    QueryWrapper<OnlinePrice> queryWrapper = new QueryWrapper<>();
  4.  
    queryWrapper.isNotNull("description");
  5.  
    //每页四个
  6.  
    Page<OnlinePrice> page = new Page<>(1, 4);
  7.  
    Page<OnlinePrice> onlinePricePage = onlinePriceMapper.selectPage(page, queryWrapper);
  8.  
    for (OnlinePrice record : page.getRecords()) {
  9.  
    System.out.println(record);
  10.  
    }
  11.  
    }

3.4 service层接口

除了 BaseMapper 接口,MyBatis Plus 还提供了 IService 接口,该接口对应 Service 层。MyBatis Plus 的通用 Service CRUD 实现了 IService 接口,进一步封装 CRUD

该接口使用 get(查询单行)、remove(删除)、list(查询集合)和 page(分页)前缀命名的方式进行区别。

  1.  
    @Autowired
  2.  
    OnlinePriceService onlinePriceService;
  3.  
    @Test
  4.  
    public void testService(){
  5.  
    OnlinePrice price = new OnlinePrice();
  6.  
    price.setId(22222222L);
  7.  
    price.setDescription("test");
  8.  
    onlinePriceService.save(price);
  9.  
    QueryWrapper<OnlinePrice> queryWrapper = new QueryWrapper<>();
  10.  
    queryWrapper.eq("description","test");
  11.  
    OnlinePrice one = onlinePriceService.getOne(queryWrapper);
  12.  
    Page<OnlinePrice> page = new Page<>(1,4);
  13.  
    QueryWrapper<OnlinePrice> queryWrapper1 = new QueryWrapper<>();
  14.  
    queryWrapper1.between("add_date", "2022-02-02", "2022-02-10");
  15.  
    onlinePriceService.page(page,queryWrapper1);
  16.  
    }
学新通


以上介绍了mybatisplus的常用基础用法,mybatisplus还有自动代码生成等其他功能,可以自动生成代码,以后有空继续学习分享

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfhfaik
系列文章
更多 icon
同类精品
更多 icon
继续加载