MyBatis-Plus( CRUD-Mapper接口)

                                                                         Mybatis-Plus快速入门


  • Mybatis-Plus简介及快速入门

      需要掌握技能

         1.  熟悉Lambda表达式

         2.  熟悉Spirngboot、Maven

         3.  最好熟悉MyBatis

      简介

         MP是一个Mybatis的增强工具,只做增强不做改变

      特性介绍

         1.  支持自定义全局通用操作、支持关键字自动转义

         2.  内置代码生成器、内置分页插件、内置性能分析插件

         3.  内置全局拦截插件、内置Sql注入剥离器

      文档及项目地址

          官网地址:https://mybatis.plus/

          github:https://github.com/baomidou/mybatis-plus

          码云:https://gitee.com/baomidou/mybatis-plus

      依赖

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starer</artifactId>
</dependency>
<dependency>
   <groupId>org.projectlombok</groupId>
   <artifactId>lombok</artifactId>
   <optional>true</optional>
</dependency>
<dependency>
  <groupId>com.baomidou</groupId>
  <artifactId>mybatis-plus-boot-starter</artifactId>
  <version>3.1.0</version>
</dependency>
<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
</dependency>
  • Mybatis-Plus核心功能

      实体类注解(常用注解)

      @Data : 该注解功能作用是自动配置get与set方法

      @TableName该注解是映射数据库表名

      @TableId该注解是映射表中主键ID字段

      @TableField该注解是映射表中普通字段

     排除非表字段的三种方式

  1.  在该字段中加入transient成员变量

//在实体类变成成员变量
private transient String remark;

  2.  在该字段中加入static变成静态变量

//在实体类变成静态变量
private static String remark;

  3. 在该字段加入TableField(exist=false)

@TableField(exist=false)
private String remark;

Mapper源码接口(CRUD接口)

       Mapper 继承该接口后,无需编写Mapper.xml文件,即可获得CRUD功能,包括基本增删改查、分页查询及条件查询等等。

  新增

    /**
     * 插入一条记录
     *
     * @param entity 实体对象
     */
    int insert(T entity);

   删除

    /**
     * 根据 ID 删除
     *
     * @param id 主键ID
     */
    int deleteById(Serializable id);

    /**
     * 根据 columnMap 条件,删除记录
     *
     * @param columnMap 表字段 map 对象
     */
    int deleteByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);

    /**
     * 根据 entity 条件,删除记录
     *
     * @param wrapper 实体对象封装操作类(可以为 null)
     */
    int delete(@Param(Constants.WRAPPER) Wrapper<T> wrapper);

    /**
     * 删除(根据ID 批量删除)
     *
     * @param idList 主键ID列表(不能为 null 以及 empty)
     */
    int deleteBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);

  修改

/**
     * 根据 ID 修改
     *
     * @param entity 实体对象
     */
    int updateById(@Param(Constants.ENTITY) T entity);

    /**
     * 根据 whereEntity 条件,更新记录
     *
     * @param entity        实体对象 (set 条件值,可以为 null)
     * @param updateWrapper 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句)
     */
    int update(@Param(Constants.ENTITY) T entity, @Param(Constants.WRAPPER) Wrapper<T> updateWrapper);

  条件查询

   /**
     * 根据 ID 查询
     *
     * @param id 主键ID
     */
    T selectById(Serializable id);


   /**
     * 查询(根据 columnMap 条件)
     *
     * @param columnMap 表字段 map 对象
     */
    List<T> selectByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);


   /**
     * 根据 entity 条件,查询全部记录
     *
     * @param queryWrapper 实体对象封装操作类(可以为 null)
     */
    List<T> selectList(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);


   /**
     * 根据 entity 条件,查询全部记录
     *
     * @param queryWrapper 实体对象封装操作类(可以为 null)
     */
    List<T> selectList(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);


   /**
     * 根据 Wrapper 条件,查询全部记录
     *
     * @param queryWrapper 实体对象封装操作类(可以为 null)
     */
    List<Map<String, Object>> selectMaps(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

   /**
     * 根据 Wrapper 条件,查询全部记录
     * <p>注意: 只返回第一个字段的值</p>
     *
     * @param queryWrapper 实体对象封装操作类(可以为 null)
     */
    List<Object> selectObjs(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

    /**
     * 根据 entity 条件,查询一条记录
     *
     * @param queryWrapper 实体对象封装操作类(可以为 null)
     */
    T selectOne(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

  批量查询

   /**
     * 查询(根据ID 批量查询)
     *
     * @param idList 主键ID列表(不能为 null 以及 empty)
     */
    List<T> selectBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);

  统计个数

   /**
     * 根据 Wrapper 条件,查询总记录数
     *
     * @param queryWrapper 实体对象封装操作类(可以为 null)
     */
    Integer selectCount(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

  测试代码效果:

 

 entity

package com.bdqn.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

import java.io.Serializable;
import java.util.Date;

@Data
@TableName("t_shop_address")
public class ShopAddress implements Serializable {
  private static final long serialVersionUID = 1L;

  @TableId(value = "id", type = IdType.AUTO)
  private Long id;
  @TableField("create_time")
  private Date createTime;
  @TableField("modify_time")
  private Date modifyTime;
  @TableField("address_detail")
  private String addressDetail;
  @TableField("area_code")
  private String areaCode;
  @TableField("city")
  private String city;
  @TableField("district")
  private String district;
  @TableField("id_user")
  private Long idUser;
  @TableField("is_default")
  private Long isDefault;
  @TableField("is_delete")
  private Long isDelete;
  @TableField("name")
  private String name;
  @TableField("post_code")
  private String postCode;
  @TableField("province")
  private String province;
  @TableField("tel")
  private String tel;


}

  Mapper

package com.bdqn.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.bdqn.entity.ShopAddress;

public interface ShopAddressMapper extends BaseMapper<ShopAddress> {

}

application.yml

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mall?useSSL=false&serverTimezone=GMT%2B8
    username: root
    password: 123

logging:
  level:
    root: warn
    com.bdqn.dao: trace
  pattern:
    console: '%p%m%n'

Springboot启动类

package com.bdqn;

import org.junit.runner.RunWith;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.ManagedBean;

@SpringBootApplication
@MapperScan("com.bdqn.dao")
public class MybatisplusApplication {

    public static void main(String[] args) {
        SpringApplication.run(MybatisplusApplication.class, args);
    }

}

  测试类:

package com.bdqn;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bdqn.dao.ShopAddressMapper;
import com.bdqn.entity.ShopAddress;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

@RunWith(SpringRunner.class)
@SpringBootTest
class MybatisplusApplicationTests {
    @Resource
    private ShopAddressMapper shopAddressMapper;

    /**
     * 查询所有
     */
    @Test
    public void select() {
        List<ShopAddress> shopAddresses = shopAddressMapper.selectList(null);
        shopAddresses.forEach(System.out::println);
    }

    /**
     * 新增
     */
    @Test
    public void insert(){
        ShopAddress shopAddress =new ShopAddress();
        shopAddress.setCreateTime(new Date(System.currentTimeMillis()));
        shopAddress.setAddressDetail("民乐大道");
        shopAddress.setAreaCode("321012");
        shopAddress.setCity("深圳市");
        shopAddress.setDistrict("民治区");
        shopAddress.setIdUser(Long.valueOf(3));
        shopAddress.setIsDefault(Long.valueOf(1));
        shopAddress.setName("鹿晗飞");
        shopAddress.setProvince("深圳市");
        shopAddress.setTel("13523921132");
        int insert = shopAddressMapper.insert(shopAddress);
        System.out.println("影像记录数:"+insert);
    }

    /**
     * 根据ID修改
     */
    @Test
    public void updateById(){
        ShopAddress shopAddress=new ShopAddress();
        shopAddress.setId(Long.valueOf(1));
        shopAddress.setName("向东");
        int count = shopAddressMapper.updateById(shopAddress);
        System.out.println("更新一条记录数"+count);
    }
    /**
     *根据条件查询结果进行修改
     */
    @Test
    public void update(){
        ShopAddress shopAddress=new ShopAddress();
        shopAddress.setId(Long.valueOf(1));
        shopAddress.setName("向东");
        QueryWrapper<ShopAddress> queryWrapper=new QueryWrapper<ShopAddress>();
        queryWrapper.like("name","飞").lt("id_user",4);
         int update = shopAddressMapper.update(shopAddress, queryWrapper);
        System.out.println("更新一条记录数:"+update);
    }

    /**
     * 批量删除(必须集合类型)
     */
    @Test
    public void deleteBatchIds(){
        List<Integer> list = Arrays.asList(1, 5, 2, 6);
         int count = shopAddressMapper.deleteBatchIds(list);
        System.out.println("删除总记录数:"+count);
    }

    /**
     * 批量删除(必须Map集合类型)
     */
    @Test
    public void deleteByMp(){
        Map<String,Object> map=new HashMap<>();
        map.put("name","路飞");
        map.put("city","北京市");
        int count = shopAddressMapper.deleteByMap(map);
        System.out.println("删除总记录数:"+count);
    }

    /**
     * 根据条件删除
     */
    @Test
    public void delete(){
        QueryWrapper<ShopAddress> queryWrapper=new QueryWrapper<ShopAddress>();
        queryWrapper.like("name","飞").lt("id_user",4);
        int count = shopAddressMapper.delete(queryWrapper);
        System.out.println("删除总记录数:"+count);
    }


}
package com.bdqn;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bdqn.dao.ShopAddressMapper;
import com.bdqn.entity.ShopAddress;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.util.*;

@RunWith(SpringRunner.class)
@SpringBootTest
class RetrieveTest {
    @Resource
    private ShopAddressMapper shopAddressMapper;

    /**
     * (条件查询)
     * 根据 ID 查询
     */
    @Test
    public void selectById() {
         ShopAddress shopAddress = shopAddressMapper.selectById(1);
        System.out.println(shopAddress);
    }

    /**
     * (条件查询)
     * 查询(根据ID 批量查询)
     *  在实战中Excel批量导出
     *  selectBatchIds:参数是List集合
     */
    @Test
    public void selectBatchIds(){
         List<Integer> list = Arrays.asList(1, 5, 2, 6);
         List<ShopAddress> shopAddresses = shopAddressMapper.selectBatchIds(list);
         shopAddresses.forEach(System.out::print);
    }

    /**
     * (条件查询)
     * 分页查询带条件查询
     * selectByMap:参数必须是Map集合
     */
    @Test
    public void selectByMap(){
        Map<String,Object> map=new HashMap<>();
        map.put("name","路飞");
        map.put("city","北京市");
        List<ShopAddress> shopAddresses = shopAddressMapper.selectByMap(map);
        shopAddresses.forEach(System.out::println);
    }

    /**
     * (条件查询)
     * 模糊查询带“飞”用户,用户ID必须大于等于4
     * 返回值是集合类型
     */
    @Test
    public void  selectByWrapper(){
        QueryWrapper<ShopAddress> queryWrapper=new QueryWrapper<ShopAddress>();
        queryWrapper.like("name","飞").lt("id_user",4);

        List<ShopAddress> shopAddresses = shopAddressMapper.selectList(queryWrapper);
        shopAddresses.forEach(System.out::println);
    }

    /**
     * (条件查询)
     * 模糊查询带“飞”用户并且用户ID必须是1到4,时间不能为空
     * 返回值是集合类型
     */
    @Test
    public void selectByWrapper2(){
        QueryWrapper<ShopAddress> queryWrapper=new QueryWrapper<ShopAddress>();
        queryWrapper.like("name","飞").between("id_user",1,4).isNotNull("create_time");
        List<ShopAddress> shopAddresses = shopAddressMapper.selectList(queryWrapper);
        shopAddresses.forEach(System.out::println);
    }

    /**
     * (条件查询)
     * 模糊查询带“飞”用户并且用户Id大于四,对用户Id进行降序,对主键ID进行生序排列
     * 返回值是集合类型
     */
    @Test
    public void selectByWrapper3(){
        QueryWrapper<ShopAddress> queryWrapper=new QueryWrapper<ShopAddress>();
        queryWrapper.likeRight("name","飞").or().ge("id_user",4).orderByDesc("id_user").orderByAsc("id");
        List<ShopAddress> shopAddresses = shopAddressMapper.selectList(queryWrapper);
        shopAddresses.forEach(System.out::println);
    }

    /**
     * (条件查询)
     * 模糊查询带“飞”用户并且用户Id大于四,对用户Id进行降序,对主键ID进行生序排列
     * 返回值是对象类型
     */
    @Test
    public void selectByWrapper4(){
        QueryWrapper<ShopAddress> queryWrapper=new QueryWrapper<ShopAddress>();
        queryWrapper.likeRight("name","飞").or().ge("id_user",4).orderByDesc("id_user").orderByAsc("id");
        ShopAddress shopAddress = shopAddressMapper.selectOne(queryWrapper);
        System.out.println(shopAddress);
    }

    /**
     * (条件查询)
     * 模糊查询带“飞”用户并且用户Id大于四,对用户Id进行降序,对主键ID进行生序排列
     * 返回值是Integer类型
     */
    @Test
    public void selectByWrapper5(){
        QueryWrapper<ShopAddress> queryWrapper=new QueryWrapper<ShopAddress>();
        queryWrapper.likeRight("name","飞").or().ge("id_user",4).orderByDesc("id_user").orderByAsc("id");
        Integer count = shopAddressMapper.selectCount(queryWrapper);
        System.out.println("统计总记录数:"+count);
    }

}

 

MybatisplusApplicationTests测试结果:

 

RetrieveTest测试结果:

Serivce源码接口(CRUD接口)

  新增

   /**
     * 插入一条记录(选择字段,策略插入)
     *
     * @param entity 实体对象
     */
    boolean save(T entity);

    /**
     * 插入(批量)
     *
     * @param entityList 实体对象集合
     */
    @Transactional(rollbackFor = Exception.class)
    default boolean saveBatch(Collection<T> entityList) {
        return saveBatch(entityList, 1000);
    }

    /**
     * 插入(批量)
     *
     * @param entityList 实体对象集合
     * @param batchSize  插入批次数量
     */
    boolean saveBatch(Collection<T> entityList, int batchSize);

    /**
     * 批量修改插入
     *
     * @param entityList 实体对象集合
     */
    @Transactional(rollbackFor = Exception.class)
    default boolean saveOrUpdateBatch(Collection<T> entityList) {
        return saveOrUpdateBatch(entityList, 1000);
    }

    /**
     * 批量修改插入
     *
     * @param entityList 实体对象集合
     * @param batchSize  每次的数量
     */
    boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize);

   /**
     * TableId 注解存在更新记录,否插入一条记录
     *
     * @param entity 实体对象
     */
    boolean saveOrUpdate(T entity);

   删除

   /**
     * 根据 ID 删除
     *
     * @param id 主键ID
     */
    boolean removeById(Serializable id);

    /**
     * 根据 columnMap 条件,删除记录
     *
     * @param columnMap 表字段 map 对象
     */
    boolean removeByMap(Map<String, Object> columnMap);

    /**
     * 根据 entity 条件,删除记录
     *
     * @param queryWrapper 实体包装类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
     */
    boolean remove(Wrapper<T> queryWrapper);

    /**
     * 删除(根据ID 批量删除)
     *
     * @param idList 主键ID列表
     */
    boolean removeByIds(Collection<? extends Serializable> idList);

  修改

   /**
     * 根据 ID 选择修改
     *
     * @param entity 实体对象
     */
    boolean updateById(T entity);

    /**
     * 根据 whereEntity 条件,更新记录
     *
     * @param entity        实体对象
     * @param updateWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper}
     */
    boolean update(T entity, Wrapper<T> updateWrapper);

    /**
     * 根据 UpdateWrapper 条件,更新记录 需要设置sqlset
     *
     * @param updateWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper}
     */
    default boolean update(Wrapper<T> updateWrapper) {
        return update(null, updateWrapper);
    }

    /**
     * 根据ID 批量更新
     *
     * @param entityList 实体对象集合
     */
    @Transactional(rollbackFor = Exception.class)
    default boolean updateBatchById(Collection<T> entityList) {
        return updateBatchById(entityList, 1000);
    }

    /**
     * 根据ID 批量更新
     *
     * @param entityList 实体对象集合
     * @param batchSize  更新批次数量
     */
    boolean updateBatchById(Collection<T> entityList, int batchSize);

  根据ID查询

   /**
     * 根据 ID 查询
     *
     * @param id 主键ID
     */
    T getById(Serializable id);

    /**
     * 查询(根据ID 批量查询)
     *
     * @param idList 主键ID列表
     */
    Collection<T> listByIds(Collection<? extends Serializable> idList);

    /**
     * 查询(根据 columnMap 条件)
     *
     * @param columnMap 表字段 map 对象
     */
    Collection<T> listByMap(Map<String, Object> columnMap);
查询一条记录
   /**
     * 根据 Wrapper,查询一条记录 <br/>
     * <p>结果集,如果是多个会抛出异常,随机取一条加上限制条件 wrapper.last("LIMIT 1")</p>
     *
     * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
     */
    default T getOne(Wrapper<T> queryWrapper) {
        return getOne(queryWrapper, true);
    }

    /**
     * 根据 Wrapper,查询一条记录
     *
     * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
     * @param throwEx      有多个 result 是否抛出异常
     */
    T getOne(Wrapper<T> queryWrapper, boolean throwEx);

    /**
     * 根据 Wrapper,查询一条记录
     *
     * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
     */
    Map<String, Object> getMap(Wrapper<T> queryWrapper);

    /**
     * 根据 Wrapper,查询一条记录
     *
     * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
     * @param mapper       转换函数
     */
    default <V> V getObj(Wrapper<T> queryWrapper, Function<? super Object, V> mapper) {
        return SqlHelper.getObject(listObjs(queryWrapper, mapper));
    }

  查询所有及条件查询

   /**
     * 查询列表
     *
     * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
     */
    List<T> list(Wrapper<T> queryWrapper);

    /**
     * 查询所有
     *
     * @see Wrappers#emptyWrapper()
     */
    default List<T> list() {
        return list(Wrappers.emptyWrapper());
    }

    /**
     * 翻页查询
     *
     * @param page         翻页对象
     * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
     */
    IPage<T> page(IPage<T> page, Wrapper<T> queryWrapper);

    /**
     * 无条件翻页查询
     *
     * @param page 翻页对象
     * @see Wrappers#emptyWrapper()
     */
    default IPage<T> page(IPage<T> page) {
        return page(page, Wrappers.emptyWrapper());
    }

    /**
     * 查询列表
     *
     * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
     */
    List<Map<String, Object>> listMaps(Wrapper<T> queryWrapper);

    /**
     * 查询所有列表
     *
     * @see Wrappers#emptyWrapper()
     */
    default List<Map<String, Object>> listMaps() {
        return listMaps(Wrappers.emptyWrapper());
    }

    /**
     * 查询全部记录
     */
    default List<Object> listObjs() {
        return listObjs(Function.identity());
    }

    /**
     * 查询全部记录
     *
     * @param mapper 转换函数
     */
    default <V> List<V> listObjs(Function<? super Object, V> mapper) {
        return listObjs(Wrappers.emptyWrapper(), mapper);
    }

    /**
     * 根据 Wrapper 条件,查询全部记录
     *
     * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
     */
    default List<Object> listObjs(Wrapper<T> queryWrapper) {
        return listObjs(queryWrapper, Function.identity());
    }

    /**
     * 根据 Wrapper 条件,查询全部记录
     *
     * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
     * @param mapper       转换函数
     */
    <V> List<V> listObjs(Wrapper<T> queryWrapper, Function<? super Object, V> mapper);

    /**
     * 翻页查询
     *
     * @param page         翻页对象
     * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
     */
    IPage<Map<String, Object>> pageMaps(IPage<T> page, Wrapper<T> queryWrapper);

    /**
     * 无条件翻页查询
     *
     * @param page 翻页对象
     * @see Wrappers#emptyWrapper()
     */
    default IPage<Map<String, Object>> pageMaps(IPage<T> page) {
        return pageMaps(page, Wrappers.emptyWrapper());
    }

 按条件查询及无条件查询

   /**
     * 根据 Wrapper 条件,查询总记录数
     *
     * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
     */
    int count(Wrapper<T> queryWrapper);

    /**
     * 查询总记录数
     *
     * @see Wrappers#emptyWrapper()
     */
    default int count() {
        return count(Wrappers.emptyWrapper());
    }

  ShopAddressService:

package com.bdqn.serivce;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import com.bdqn.entity.ShopAddress;

import java.sql.Wrapper;
import java.util.List;
import java.util.Map;

public interface ShopAddressService extends IService<ShopAddress> {
    /**
     * 根据 ID 删除
     */
    boolean delete(Long id);

    /**
     *批量删除
     */
    boolean delete(Map<String,Object> map);
    /**
     *条件删除
     */
    boolean delete(QueryWrapper<ShopAddress> wrapper,ShopAddress shopAddress);
    /**
     * 批量删除
     */
    boolean delete(List<ShopAddress> shopAddressList);

    /**
     * 修改
     */
    boolean update(ShopAddress shopAddress);
    /**
     * 批量修改
     */
    boolean update(List<ShopAddress> shopAddressList);
    /**
     * 批量修改
     */
    boolean update(List<ShopAddress> shopAddressList,Integer size);
    /**
     * 条件修改
     */
    boolean update(ShopAddress shopAddress, QueryWrapper<ShopAddress> queryWrapper);
    /**
     * 条件修改
     */
    boolean update(QueryWrapper<ShopAddress> wrapper);

    /**
     * 插入
     */
    boolean add(ShopAddress shopAddress);
    /**
     * 批量插入
     */
    boolean saveBatch(List<ShopAddress> shopAddressList);
    /**
     * 批量插入
     */
    boolean saveBatch(List<ShopAddress> shopAddressList,Integer size);


    /**
     * 根据ID 查询
     */
    ShopAddress selectId(Long id);
    /**
     * 查询(根据ID 批量查询)
     */
    List<ShopAddress> selectList(List<ShopAddress> shopAddressList);
    /**
     * 查询(根据 columnMap 条件)
     */
    List<ShopAddress> selectList(Map<String,Object> map);


    /**
     * 查询一条记录
     */
    ShopAddress get(QueryWrapper<ShopAddress> wrapper);
    /**
     * 查询一条记录
     */
    ShopAddress get(QueryWrapper<ShopAddress>  wrapper,boolean flag);
    /**
     * 查询一条记录
     */
    Map<String,Object> getMap(QueryWrapper<ShopAddress>  wrapper);


    /**
     * 查询总记录数
     */
    int total(QueryWrapper<ShopAddress> wrapper);


    /**
     * 查询所有
     */
    List<ShopAddress> queryAll(QueryWrapper<ShopAddress> wrapper);
    /**
     * 条件查询
     */
    List<Map<String,Object>> listMaps(QueryWrapper<ShopAddress> wrapper);

    /**
     * 翻页查询
     */
    IPage<ShopAddress> Pagination(IPage<ShopAddress> page,QueryWrapper<ShopAddress> wrapper);

    /**
     * 无条件翻页查询
     */
    IPage<ShopAddress> Pagination(IPage<ShopAddress> page);
}

ShopAddressServiceImpl:

package com.bdqn.serivce.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bdqn.dao.ShopAddressMapper;
import com.bdqn.entity.ShopAddress;
import com.bdqn.serivce.ShopAddressService;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.util.List;
import java.util.Map;

@Service
public class ShopAddressServiceImpl extends ServiceImpl<ShopAddressMapper, ShopAddress> implements ShopAddressService {
    /**
     * 根据 ID 删除
     *
     * @param id id 主键ID
     */
    @Override
    public boolean delete(Long id) {
        return this.removeById(id);
    }

    /**
     * 批量删除
     *
     * @param map 实体对象集合
     */
    @Override
    public boolean delete(Map<String, Object> map) {
        return this.removeByMap(map);
    }

    /**
     * 条件删除
     *  @param wrapper 实体包装类
     * @param shopAddress
     */
    @Override
    public boolean delete(QueryWrapper<ShopAddress> wrapper, ShopAddress shopAddress) {
        wrapper.like("name",shopAddress.getName()).lt("id_user",shopAddress.getIdUser());
        return this.remove(wrapper);
    }

    /**
     * 批量删除
     *
     * @param shopAddressList 实体对象集合
     */
    @Override
    public boolean delete(List<ShopAddress> shopAddressList) {
        return this.removeByIds(shopAddressList);
    }

    /**
     * 修改
     *
     * @param shopAddress 对象修改
     */
    @Override
    public boolean update(ShopAddress shopAddress) {
        return this.update(shopAddress);
    }

    /**
     * 批量修改
     *
     * @param shopAddressList 实体对象集合
     */
    @Override
    public boolean update(List<ShopAddress> shopAddressList) {
        return this.update(shopAddressList);
    }

    /**
     * 批量修改
     *
     * @param shopAddressList 实体对象集合
     * @param size            每次的数量
     */
    @Override
    public boolean update(List<ShopAddress> shopAddressList, Integer size) {
        return this.update(shopAddressList,size);
    }

    /**
     * 条件修改
     *
     * @param shopAddress 实体对象
     * @param queryWrapper     实体包装类
     */
    @Override
    public boolean update(ShopAddress shopAddress, QueryWrapper<ShopAddress> queryWrapper) {
        ShopAddress shop=new ShopAddress();
        shop.setName(shopAddress.getName());
        queryWrapper.like("name",shopAddress.getName()).lt("id_user",shopAddress.getIdUser());
        return this.update(shopAddress,queryWrapper);
    }

    /**
     * @param wrapper 实体包装类
     * @return 返回值 boolean
     */
    @Override
    public boolean update(QueryWrapper<ShopAddress> wrapper) {
        wrapper.like("name",new ShopAddress().getName());
        return this.update(wrapper) ;
    }

    /**
     * 插入
     *
     * @param shopAddress 实体对象
     */
    @Override
    public boolean add(ShopAddress shopAddress) {
        return this.save(shopAddress);
    }

    /**
     * 批量插入
     *
     * @param shopAddressList 实体对象集合
     */
    @Override
    public boolean saveBatch(List<ShopAddress> shopAddressList) {
        return this.saveBatch(shopAddressList);
    }

    /**
     * 批量插入
     *
     * @param shopAddressList 实体对象集合
     * @param size            每次的数量
     */
    @Override
    public boolean saveBatch(List<ShopAddress> shopAddressList, Integer size) {
        return this.saveBatch(shopAddressList,size);
    }

    /**
     * 根据ID 查询
     *
     * @param id 根据ID 主键
     */
    @Override
    public ShopAddress selectId(Long id) {
        return this.selectId(id);
    }

    /**
     * 查询(根据ID 批量查询)
     *
     * @param shopAddressList 实体对象集合
     */
    @Override
    public List<ShopAddress> selectList(List<ShopAddress> shopAddressList) {
        return this.selectList(shopAddressList);
    }

    /**
     * 查询(根据 columnMap 条件)
     *
     * @param map 实体对象集合
     */
    @Override
    public List<ShopAddress> selectList(Map<String, Object> map) {
        return this.selectList(map);
    }

    /**
     * 查询一条记录
     *
     * @param wrapper
     */
    @Override
    public ShopAddress get(QueryWrapper<ShopAddress>  wrapper) {
        wrapper.like("name",new ShopAddress().getName());
        return this.getOne(wrapper);
    }

    /**
     * 查询一条记录
     *
     * @param wrapper 实体对象封装操作类
     * @param flag    有多个 result 是否抛出异常
     */
    @Override
    public ShopAddress get(QueryWrapper<ShopAddress>  wrapper, boolean flag) {
        wrapper.like("name",new ShopAddress().getName());
        return this.getOne(wrapper,true);
    }

    /**
     * 查询一条记录
     *
     * @param wrapper 实体对象封装操作类
     */
    @Override
    public Map<String, Object> getMap(QueryWrapper<ShopAddress>  wrapper) {
        wrapper.like("name",new ShopAddress().getName());

        return this.getMap(wrapper);
    }

    /**
     * 查询总记录数
     *
     * @param wrapper 实体对象封装操作类
     */
    @Override
    public int total(QueryWrapper<ShopAddress> wrapper) {
        wrapper.like("name",new ShopAddress().getName());
        return this.count(wrapper);
    }

    /**
     * 查询所有记录数
     *
     * @param wrapper 实体对象封装操作类
     */
    @Override
    public List<ShopAddress> queryAll(QueryWrapper<ShopAddress> wrapper) {
        wrapper.like("name",new ShopAddress().getName());
        return this.list(wrapper);
    }

    /**
     * 条件查询
     *
     * @param wrapper 实体对象封装操作类
     */
    @Override
    public List<Map<String, Object>> listMaps(QueryWrapper<ShopAddress> wrapper) {
        wrapper.like("name",new ShopAddress().getName());
        return this.listMaps(wrapper);
    }

    /**
     * 翻页查询
     *
     * @param page    翻页对象
     * @param wrapper 实体对象封装操作类
     */
    @Override
    public IPage<ShopAddress> Pagination(IPage<ShopAddress> page, QueryWrapper<ShopAddress> wrapper) {
        wrapper.like("name",new ShopAddress().getName());
        return this.page(page,wrapper);
    }

    /**
     * 无条件翻页查询
     *
     * @param page 翻页对象
     */
    @Override
    public IPage<ShopAddress> Pagination(IPage<ShopAddress> page) {
        return this.page(page);
    }
}

以下是对BaseMapper及IService接口大致测试结果,条件查询功能是MybatisPlus核心点,多敲敲代码就明白其中用法,就可以早点下班陪家人啦!

 


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值