【框架----MyBatisPlus】四、Springboot + MyBatisPlus详细拆解CRUD

注明 : 本篇文章将介绍springboot+mybatis-plus通过AutoGenerator自动生成entrty、controller、service、dao、mapper后对于基本的CRUD的操作和注意事项。

初始化项目搭建
初始化项目我就不在一一阐述了,如果有不知道的童鞋可以看我的上篇博文来做项目的初始化搭建,也可以去Gitee下载源码
源码地址 Gitee

层级代码展示

å¨è¿éæå¥å¾çæè¿°

上图为自动生成后的层级展示,那么下面就开始我们的测试之路

在测试之前我们需要注意一些要点,如下放代码所示我们需要在yml配置文件中加入下面的代码块。这样在我们测试的时候会在控制台打印sql语句供我们参考

mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

实例测试

    /**
     * <p>
     *     insert 插入测试
     * </p>
     */
    @Test
    public void insertLoads() {
        User user = new User();
        user.setEmail("lqf@163.com");
        user.setAge(12);

        Integer insert = mapper.insert(user);
        System.out.println("return insert value = " + insert);
    }

sql执行和打印结果

==>  Preparing: INSERT INTO user ( id, age, email ) VALUES ( ?, ?, ? ) 
==> Parameters: 1046280811781570561(Long), 12(Integer), lqf@163.com(String)
<==    Updates: 1
return insert value = 1

上面的结果我们可以看到inset操作正常添加了,插入的也只有三个字段,其他没有填写的字段是不会添加,在看自动生成的id是一个Long类型的设置。这里需要注意的是如果你想要一个主键自增的id那么你需要进行id主键自增的设置

    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

IdType.AUTO这个就是主键自增的标识,如果你在创建数据表的时候设置了主键自增在自动生成的时候注解就会自动添加的。在2.X的版本中还存在insertAllColumn现在的3.0版本已经不存在了就不做介绍了。

    /**
     *<P>
     *     通过id更新信息
     *</P>
     */
    @Test
    public void updateByIdLoads() {
        User user = new User();
        user.setAge(123);
        user.setEmail("weqee@163.com");
        user.setId(1L);

        Integer insert = mapper.updateById(user);
        System.out.println("return insert value = " + insert);
    }

sql执行和打印结果

==>  Preparing: UPDATE user SET age=?, email=? WHERE id=? 
==> Parameters: 123(Integer), weqee@163.com(String), 1(Long)
<==    Updates: 1
return insert value = 1

这里我就不多说了相信大家都懂

    /**
     * <P>
     *     deleteBatchIds 根据id批量删除
     * </P>
     */
    @Test
    public void deleteLoads() {
        List<Long> list = new ArrayList<>();
        list.add(1L);
        list.add(2L);
        list.add(3L);

        Integer insert = mapper.deleteBatchIds(list);
        System.out.println("return deleteBatchIds value = " + insert);
    }

sql执行和打印结果

==>  Preparing: DELETE FROM user WHERE id IN ( ? , ? , ? ) 
==> Parameters: 1(Long), 2(Long), 3(Long)
<==    Updates: 3
return deleteBatchIds value = 3

看上面的结果可以看出批量删除的操作其实是id in返回结构是删除成功的条数

    /**
     * <P>
     *     deleteById 根据id删除
     * </P>
     */
    @Test
    public void deleteByIdLoads() {
        Integer deleteById = mapper.deleteById(4L);
        System.out.println("return deleteById value = " + deleteById);
    }

这个就是通过id删除一条数据

    /**
     * <p>
     * deleteByMap 根据map条件进行删除
     * </P>
     */
    @Test
    public void deleteByMapsLoads() {
        HashMap<String, Object> map = new HashMap<>(16);
        map.put("email", "lqf@163.com");
        map.put("age", 12);

        Integer insert = mapper.deleteByMap(map);
        System.out.println("return deleteByMap value = " + insert);
    }

这个就是通过map条件进行删除操作,条件查询到多少条删除多少,这里需要注意的是,map中的key对应的是数据库字段例如数据库user_id,实体类是userId这是在作为map key的时候需要填写user_id。

sql执行和打印结果

==>  Preparing: DELETE FROM user WHERE email = ? AND age = ? 
==> Parameters: lqf@163.com(String), 12(Integer)
<==    Updates: 6
return deleteByMap value = 6
    /**
     * <p>
     * 通过id查询对象
     * </P>
     */
    @Test
    public void selectByIdLoads() {
        User user = mapper.selectById(4L);
        System.out.println("return insert value = " + user);
    }

这里是通过id进行查询返回单个实体对象

    /**
     * <p>
     * 通过多个id进行查询
     * </P>
     */
    @Test
    public void selectBatchIdsLoads() {
        List<Long> list = new ArrayList<>();
        list.add(1L);
        list.add(2L);
        list.add(3L);
        List<User> list1 = mapper.selectBatchIds(list);
        System.out.println("return selectBatchIds value = " + list1);
    }

通过多个id进行查询返回list集合

    /**
     * <p>
     * 通过条件进行实体list查询
     * </P>
     */
    @Test
    public void selectByMapLoads() {
        HashMap<String, Object> map = new HashMap<>(16);
        map.put("email", "lqf@163.com");
        map.put("age", 12);
        List<User> list = mapper.selectByMap(map);
        System.out.println("return selectByMap value = " + list);
    }

通过多个条件进行实体list查询

    /**
     * <p>
     * 分页查询
     * </P>
     */
    @Test
    public void selectPageLoads() {
        Page<User> page = new Page<>(1,5);
        IPage<User> lstUser = mapper.selectPage(page, null);
        System.out.println("return selectPageLoads value = " + lstUser);
    }

上面的查询是分页查询,page中的1代表当前页,5代表每页条数。要想实现mybatis-plus分页首先需要确定配置了分页插件配置方法如下,null代表的是条件构造器这个我在后面会讲,

    /**
     * 分页插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }

在配置类或启动类中增加上面的bean就可以使用了,如不不添加分页不成功查询所有内容

分页后sql打印

==>  Preparing: SELECT COUNT(1) FROM user 
==> Parameters: 
<==    Columns: COUNT(1)
<==        Row: 11
==>  Preparing: SELECT id,name,age,email,status FROM user LIMIT 0,5 
==> Parameters: 
<==    Columns: id, name, age, email, status
<==        Row: 1046282328366391307, null, 12, lqf@163.com, null
<==        Row: 1046282328366391308, null, 12, lqf@163.com, null
<==        Row: 1046282328366391309, null, 12, lqf@163.com, null
<==        Row: 1046282328366391310, null, 12, lqf@163.com, null
<==        Row: 1046282328366391311, null, 12, lqf@163.com, null
<==      Total: 5

上方的所有测试就是springboot + mybatis-plus自动生成后的所有简单CRUD操作查询。

我们来看一下mapper中的BaseMapper<>源码

/*
 * Copyright (c) 2011-2020, hubin (jobob@qq.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.baomidou.mybatisplus.core.mapper;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;

/**
 * <p>
 * Mapper 继承该接口后,无需编写 mapper.xml 文件,即可获得CRUD功能
 * </p>
 * <p>
 * 这个 Mapper 支持 id 泛型
 * </p>
 *
 * @author hubin
 * @since 2016-01-23
 */
public interface BaseMapper<T> {

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

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

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

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

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

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

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

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

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

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

    /**
     * <p>
     * 根据 entity 条件,查询一条记录
     * </p>
     *
     * @param queryWrapper 实体对象
     */
    T selectOne(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

    /**
     * <p>
     * 根据 Wrapper 条件,查询总记录数
     * </p>
     *
     * @param queryWrapper 实体对象
     */
    Integer selectCount(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

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

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

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

    /**
     * <p>
     * 根据 entity 条件,查询全部记录(并翻页)
     * </p>
     *
     * @param page         分页查询条件(可以为 RowBounds.DEFAULT)
     * @param queryWrapper 实体对象封装操作类(可以为 null)
     */
    IPage<T> selectPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

    /**
     * <p>
     * 根据 Wrapper 条件,查询全部记录(并翻页)
     * </p>
     *
     * @param page         分页查询条件
     * @param queryWrapper 实体对象封装操作类
     */
    IPage<Map<String, Object>> selectMapsPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
}

 通过看上面的baseMapper大家肯定会说你里面有很多没有说啊!是的,在这里介绍的都是基本的CRUD没有涉及到条件构造器的,我将在下篇为大家详细的讲解条件构造器,并将上面没有说到的方法一并解决。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值