【Mybatis-Plus 学习笔记】2、日志配置及常用 CRUD

本文涉及的产品
日志服务 SLS,月写入数据量 50GB 1个月
简介: 【Mybatis-Plus 学习笔记】2、日志配置及常用 CRUD

前言

上一篇文章中我们讲了 Mybatis-Plus 的定义以及相关特点,并从零开始编写了一个 SpringBoot + Mybatis-Plus 的实例。今天我们就来看看,如何利用 MP 来实现对数据库的增删改查。


日志配置

使用 MP 时,默认是不打印任何 SQL 语句的。而为了方便日常开发工作的调试,我们需要联合控制台和各种数据可视化工具进行语句的拼接检查,因此我们利用 MP 自带的日志功能,在控制台输出我们的 SQL 语句,从而方便我们调试。


在配置文件 application.yml (IDEA 默认生成的配置文件为 application.properties)中,加入一下配置,这样 MP 就会在控制台中打印完整带参数的 SQL 语句,方便我们查看。


mybatis-plus:

 configuration:

   log-impl: org.apache.ibatis.logging.stdout.StdOutImpl


Mapper CRUD 使用方法

首先我们来看 Mapper 层 CRUD 涉及的一些方法,Mapper 层主要继承自 BaseMapper 接口,里边实现了各种用于操作数据库的增删改查的方法,以下我们就来看看日常我们常用的一些方法。


package com.cunyu.employee.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.cunyu.employee.entity.Employee;
/**
 * Created with IntelliJ IDEA.
 *
 * @author : cunyu
 * @version : 1.0
 * @project : Employee
 * @package : com.cunyu.employee.mapper
 * @className : EmployeeMapper
 * @createTime : 2021/8/7 17:45
 * @description : 员工 Mapper 类
 */
public interface EmployeeMapper extends BaseMapper<Employee> {
}

insert 操作

首先是插入数据,insert 方法中,传入我们所要插入数据库的实体对象作为参数即可。

  • 方法声明
/**
     * 插入一条记录
     *
     * @param entity 实体对象
     */
int insert(T entity);
  • 插入实例
package com.cunyu.employee;
import com.cunyu.employee.entity.Employee;
import com.cunyu.employee.mapper.EmployeeMapper;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class EmployeeApplicationTests {
    @Test
    void contextLoads() {
    }
    @Autowired
    private EmployeeMapper employeeMapper;
    @Test
    void testInsert() {
        Employee employee = new Employee();
        employee.setId(4L);
        employee.setName("赵六");
        employee.setSex("男");
        employee.setEmail("zhaoliu@cunyu1943.com");
        Assert.assertEquals(1, employeeMapper.insert(employee));
        System.out.println("插入成功");
    }
}

测试结果image.pngimage.png

select 操作

相比于插入数据操作,查询数据的方法就要更多,而且还能实现批量查询和条件查询。

  1. 根据主键查询

将所要查询数据的主键作为参数传入我们的 selectById 方法中,即可实现。

  • 方法声明
/**
     * 根据 ID 查询
     *
     * @param id 主键ID
     */
T selectById(Serializable id);
  • 查询实例
package com.cunyu.employee;
import com.cunyu.employee.entity.Employee;
import com.cunyu.employee.mapper.EmployeeMapper;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class EmployeeApplicationTests {
    @Test
    void contextLoads() {
    }
    @Autowired
    private EmployeeMapper employeeMapper;
    @Test
    void testSelectById() {
        Employee employee = employeeMapper.selectById(3);
        System.out.println(employee);
    }
}

image.png

  1. 根据主键批量查询

上一个方法每次只能查询一条记录,如果我们想要查询多条数据记录,那么就可以将要查询数据的主键列表传入 selectBatchIds 方法即可。

  • 方法声明
/**
     * 查询(根据ID 批量查询)
     *
     * @param idList 主键ID列表(不能为 null 以及 empty)
     */
List<T> selectBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
  • 批量查询实例
package com.cunyu.employee;
import com.cunyu.employee.entity.Employee;
import com.cunyu.employee.mapper.EmployeeMapper;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class EmployeeApplicationTests {
    @Test
    void contextLoads() {
    }
    @Autowired
    private EmployeeMapper employeeMapper;
    @Test
    void testSelectBatchIds() {
        List<Integer> ids = new ArrayList<>();
        ids.add(1);
        ids.add(4);
        List<Employee> employeeList = employeeMapper.selectBatchIds(ids);
        System.out.println(employeeList);
    }
}

image.png

  1. 根据多条件查询

除开支持主键查询外,MP 还支持条件查询,只要将我们的条件传入 Map 列表中,然后将其作为 selectByMap 方法的参数即可,其中传入 Mapkey 对应我们数据库中的字段,而 value 则对应字段的值。

  • 方法声明
/**
     * 查询(根据 columnMap 条件)
     *
     * @param columnMap 表字段 map 对象
     */
List<T> selectByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
  • 条件查询实例
package com.cunyu.employee;
import com.cunyu.employee.entity.Employee;
import com.cunyu.employee.mapper.EmployeeMapper;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class EmployeeApplicationTests {
    @Test
    void contextLoads() {
    }
    @Autowired
    private EmployeeMapper employeeMapper;
    @Test
    void testSelectByMap() {
        Map<String, Object> map = new HashMap<>();
        map.put("sex", "男");
        map.put("name", "张三");
        System.out.println(employeeMapper.selectByMap(map));
    }
}

image.pngupdate 操作

更新操作,主要是根据我们数据库的主键进行查询,将对应主键的实体对象传入 updateById 方法即可。


方法声明

/**

    * 根据 ID 修改

    *

    * @param entity 实体对象

    */

int updateById(@Param(Constants.ENTITY) T entity);


更新实例


package com.cunyu.employee;
import com.cunyu.employee.entity.Employee;
import com.cunyu.employee.mapper.EmployeeMapper;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class EmployeeApplicationTests {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Test
    void testUpdate() {
        Employee employee = new Employee();
        employee.setEmail("zhao6@cunyu1943.com");
        employee.setName("赵 6");
        employee.setSex("女");
        employee.setId(4L);
        Assert.assertEquals(1, employeeMapper.updateById(employee));
        System.out.println("更新成功");
    }
}

image.pngimage.pngdelete 操作

删除操作,既可以根据主键删除一条记录,也能根据主键列表实现批量删除,还能根据条件来进行删除。


根据主键删除一条数据

将所要删除记录的主键作为参数传入 deleteById 方法即可。


方法声明

/**

    * 根据 ID 删除

    *

    * @param id 主键ID

    */

int deleteById(Serializable id);


删除实例


package com.cunyu.employee;
import com.cunyu.employee.entity.Employee;
import com.cunyu.employee.mapper.EmployeeMapper;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class EmployeeApplicationTests {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Test
    void testDeleteById() {
        Assert.assertEquals(1, employeeMapper.deleteById(2L));
        System.out.println("删除成功");
    }
}

image.png根据条件删除

根据条件删除同样是讲条件传入 Map 中,然后将 Map 作为参数传入 deleteByMap 方法,其中 key 对应数据库中的字段,value 对应字段的值。


方法声明

/**

    * 根据 columnMap 条件,删除记录

    *

    * @param columnMap 表字段 map 对象

    */

int deleteByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);


删除实例


package com.cunyu.employee;
import com.cunyu.employee.entity.Employee;
import com.cunyu.employee.mapper.EmployeeMapper;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class EmployeeApplicationTests {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Test
    void testDeleteByMap() {
        Map<String, Object> map = new HashMap<>();
        map.put("name", "赵 6");
        Assert.assertEquals(1, employeeMapper.deleteByMap(map));
        System.out.println("删除成功");
    }
}

image.png根据主键批量删除

将要删除记录的主键传入集合中,然后将集合作为 deleteBatchIds 方法的参数即可。


方法声明

/**

    * 删除(根据ID 批量删除)

    *

    * @param idList 主键ID列表(不能为 null 以及 empty)

    */

int deleteBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);


批量删除实例


package com.cunyu.employee;
import com.cunyu.employee.entity.Employee;
import com.cunyu.employee.mapper.EmployeeMapper;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class EmployeeApplicationTests {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Test
    void testDeleteBatchIds() {
        List<Integer> ids = new ArrayList<>();
        ids.add(1);
        ids.add(3);
        Assert.assertEquals(2, employeeMapper.deleteBatchIds(ids));
        System.out.println("删除成功");
    }
}

image.png

Service CRUD 接口

Service 层继承自 IService 接口,其中的方法和 Mapper 层中所提供的方法功能是一致的,除了方法名有所不同外,其他基本类似,但 Service 层中提供了更为丰富的方法,两者的继承结构如下图所示。image.png

package com.cunyu.employee.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.cunyu.employee.entity.Employee;
/**
 * Created with IntelliJ IDEA.
 *
 * @author : cunyu
 * @version : 1.0
 * @project : Employee
 * @package : com.cunyu.employee.service
 * @className : EmployeeService
 * @createTime : 2021/8/8 7:52
 * @description : 员工服务接口
 */
@Service
public interface EmployeeService extends IService<Employee> {
}
package com.cunyu.employee.service.Impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cunyu.employee.entity.Employee;
import com.cunyu.employee.mapper.EmployeeMapper;
import com.cunyu.employee.service.EmployeeService;
/**
 * Created with IntelliJ IDEA.
 *
 * @author : cunyu
 * @version : 1.0
 * @project : Employee
 * @package : com.cunyu.employee.service.Impl
 * @className : EmployeeServiceImpl
 * @createTime : 2021/8/8 7:53
 * @description : 员工服务类实现
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {
}

Save

  1. 插入一条记录

功能同 Mapper 层中的 insert 方法,只不过方法名不同。

  • 方法声明
// 插入一条记录(选择字段,策略插入)
boolean save(T entity);
• 1
• 2
  • 插入实例
package com.cunyu.employee;
import com.cunyu.employee.entity.Employee;
import com.cunyu.employee.mapper.EmployeeMapper;
import com.cunyu.employee.service.EmployeeService;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class EmployeeApplicationTests {
    @Autowired
    private EmployeeService employeeService;
    @Test
    void testSave() {
        Employee employee = new Employee();
        employee.setId(5L);
        employee.setName("周七");
        employee.setEmail("zhouqi@cunyu1943.com");
        employee.setSex("女");
        Assert.assertTrue(employeeService.save(employee));
        System.out.println("插入成功");
    }
}

image.png批量插入

这里就和 Mapper 层中所有区别,Mapper 层中只支持单次插入,而 Service 层中支持批量插入,而传入的参数就是我们所要传入实体的集合,而且还可以分批次插入和统一插入。


2.1 统一插入


方法声明

// 插入(批量)

boolean saveBatch(Collection<T> entityList);


package com.cunyu.employee;
import com.cunyu.employee.entity.Employee;
import com.cunyu.employee.mapper.EmployeeMapper;
import com.cunyu.employee.service.EmployeeService;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.ArrayList;
import java.util.List;
@SpringBootTest
class EmployeeApplicationTests {
    @Autowired
    private EmployeeService employeeService;
    @Test
    void testSaveBatch() {
        Employee employee1 = new Employee();
        employee1.setId(6L);
        employee1.setEmail("zhangliang@cunyu1943.com");
        employee1.setSex("男");
        employee1.setName("张良");
        Employee employee2 = new Employee();
        employee2.setId(7L);
        employee2.setEmail("zhouyu@cunyu1943.com");
        employee2.setName("周瑜");
        employee2.setSex("男");
        List<Employee> employeeList = new ArrayList<>();
        employeeList.add(employee1);
        employeeList.add(employee2);
        Assert.assertTrue(employeeService.saveBatch(employeeList));
        System.out.println("批量插入成功");
    }
}

image.png2.2 分批次插入

  • 方法声明
// 插入(批量)
boolean saveBatch(Collection<T> entityList, int batchSize);
• 1
• 2
  • 分批次插入实例
package com.cunyu.employee;
import com.cunyu.employee.entity.Employee;
import com.cunyu.employee.mapper.EmployeeMapper;
import com.cunyu.employee.service.EmployeeService;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.ArrayList;
import java.util.List;
@SpringBootTest
class EmployeeApplicationTests {
    @Autowired
    private EmployeeService employeeService;
    @Test
    void testSaveBatch() {
        Employee employee1 = new Employee();
        employee1.setId(8L);
        employee1.setEmail("jialuo@cunyu1943.com");
        employee1.setSex("女");
        employee1.setName("迦罗");
        Employee employee2 = new Employee();
        employee2.setId(9L);
        employee2.setEmail("zhugeliang@cunyu1943.com");
        employee2.setName("诸葛亮");
        employee2.setSex("男");
        List<Employee> employeeList = new ArrayList<>();
        employeeList.add(employee1);
        employeeList.add(employee2);
        Assert.assertTrue(employeeService.saveBatch(employeeList,2));
        System.out.println("批量插入成功");
    }
}

image.png

SaveOrUpdate

  1. 单条修改插入
  • 方法声明
// TableId 注解存在更新记录,否插入一条记录
boolean saveOrUpdate(T entity);
• 1
• 2
  • 单条修改插入实例
package com.cunyu.employee;
import com.cunyu.employee.entity.Employee;
import com.cunyu.employee.mapper.EmployeeMapper;
import com.cunyu.employee.service.EmployeeService;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.ArrayList;
import java.util.List;
@SpringBootTest
class EmployeeApplicationTests {
    @Autowired
    private EmployeeService employeeService;
    @Test
    void testSaveOrUpdate() {
        Employee employee = new Employee();
        employee.setId(5L);
        employee.setName("周武");
        employee.setEmail("zhouwu@cunyu1943.com");
        employee.setSex("男");
        Assert.assertTrue(employeeService.saveOrUpdate(employee));
        System.out.println("更新成功");
    }
}

image.pngimage.png

  1. 批量修改插入

2.1 统一插入

  • 方法声明
// 批量修改插入
boolean saveOrUpdateBatch(Collection<T> entityList);
• 1
• 2
  • 统一插入实例
package com.cunyu.employee;
import com.cunyu.employee.entity.Employee;
import com.cunyu.employee.mapper.EmployeeMapper;
import com.cunyu.employee.service.EmployeeService;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.ArrayList;
import java.util.List;
@SpringBootTest
class EmployeeApplicationTests {
    @Autowired
    private EmployeeService employeeService;
    @Test
    void testSaveOrUpdateBatch() {
        Employee employee1 = new Employee();
        employee1.setId(10L);
        employee1.setEmail("zhongwuyan@cunyu1943.com");
        employee1.setSex("女");
        employee1.setName("钟无艳");
        Employee employee2 = new Employee();
        employee2.setId(11L);
        employee2.setEmail("direnjie@cunyu1943.com");
        employee2.setName("狄仁杰");
        employee2.setSex("男");
        List<Employee> employeeList = new ArrayList<>();
        employeeList.add(employee1);
        employeeList.add(employee2);
        Assert.assertTrue(employeeService.saveOrUpdateBatch(employeeList));
        System.out.println("批量修改插入成功");
    }
}

image.png2.2 分批次插入

  • 方法声明
// 批量修改插入
boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize);
• 1
• 2
  • 方法实例
package com.cunyu.employee;
import com.cunyu.employee.entity.Employee;
import com.cunyu.employee.mapper.EmployeeMapper;
import com.cunyu.employee.service.EmployeeService;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.ArrayList;
import java.util.List;
@SpringBootTest
class EmployeeApplicationTests {
    @Autowired
    private EmployeeService employeeService;
    @Test
    void testSaveOrUpdateBatch() {
        Employee employee1 = new Employee();
        employee1.setId(12L);
        employee1.setEmail("yuji@cunyu1943.com");
        employee1.setSex("女");
        employee1.setName("虞姬");
        Employee employee2 = new Employee();
        employee2.setId(13L);
        employee2.setEmail("sulie@cunyu1943.com");
        employee2.setName("苏烈");
        employee2.setSex("男");
        List<Employee> employeeList = new ArrayList<>();
        employeeList.add(employee1);
        employeeList.add(employee2);
        Assert.assertTrue(employeeService.saveOrUpdateBatch(employeeList, 2));
        System.out.println("批量修改插入成功");
    }
}

image.pngimage.png

Remove

  1. 根据 ID 删除
  • 方法实例
// 根据 ID 删除
boolean removeById(Serializable id);
• 1
• 2
  • 删除实例
package com.cunyu.employee;
import com.cunyu.employee.entity.Employee;
import com.cunyu.employee.mapper.EmployeeMapper;
import com.cunyu.employee.service.EmployeeService;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class EmployeeApplicationTests {
    @Autowired
    private EmployeeService employeeService;
    @Test
    void testRemoveById() {
        Assert.assertTrue(employeeService.removeById(5));
        System.out.println("删除成功");
    }
}

image.png

  1. 根据条件删除
  • 方法声明
// 根据 columnMap 条件,删除记录
boolean removeByMap(Map<String, Object> columnMap);
• 1
• 2
  • 按条件删除实例
package com.cunyu.employee;
import com.cunyu.employee.entity.Employee;
import com.cunyu.employee.mapper.EmployeeMapper;
import com.cunyu.employee.service.EmployeeService;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.HashMap;
import java.util.Map;
@SpringBootTest
class EmployeeApplicationTests {
    @Autowired
    private EmployeeService employeeService;
    @Test
    void testRemoveByMap() {
        Map<String, Object> map = new HashMap<>();
        map.put("sex", "女");
        Assert.assertTrue(employeeService.removeByMap(map));
        System.out.println("删除成功");
    }
}

image.png

  1. 根据 ID 批量删除
  • 方法声明
// 删除(根据ID 批量删除)
boolean removeByIds(Collection<? extends Serializable> idList);
• 1
• 2
  • 批量删除实例
package com.cunyu.employee;
import com.cunyu.employee.entity.Employee;
import com.cunyu.employee.mapper.EmployeeMapper;
import com.cunyu.employee.service.EmployeeService;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.ArrayList;
import java.util.List;
@SpringBootTest
class EmployeeApplicationTests {
    @Autowired
    private EmployeeService employeeService;
    @Test
    void testRemoveByIds() {
        List<Integer> ids = new ArrayList<>();
        ids.add(1);
        ids.add(4);
        Assert.assertTrue(employeeService.removeByIds(ids));
        System.out.println("批量删除成功");
    }
}

image.png

Update

  1. 根据 ID 选择修改
  • 方法声明
// 根据 ID 选择修改
boolean updateById(T entity);
• 1
• 2
  • 根据 ID 修改实例
package com.cunyu.employee;
import com.cunyu.employee.entity.Employee;
import com.cunyu.employee.mapper.EmployeeMapper;
import com.cunyu.employee.service.EmployeeService;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class EmployeeApplicationTests {
    @Autowired
    private EmployeeService employeeService;
    @Test
    void testUpdateById() {
        Employee employee = new Employee();
        employee.setId(3L);
        employee.setName("程咬金");
        employee.setSex("男");
        employee.setEmail("chengyaojin@cunyu1943.com");
        Assert.assertTrue(employeeService.updateById(employee));
        System.out.println("更新成功");
    }
}

image.png

  1. 根据 ID 批量更新

2.1 统一更新

  • 方法声明
// 根据ID 批量更新
boolean updateBatchById(Collection<T> entityList);
• 1
• 2
  • 批量更新实例
package com.cunyu.employee;
import com.cunyu.employee.entity.Employee;
import com.cunyu.employee.mapper.EmployeeMapper;
import com.cunyu.employee.service.EmployeeService;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.ArrayList;
import java.util.List;
@SpringBootTest
class EmployeeApplicationTests {
    @Autowired
    private EmployeeService employeeService;
    @Test
    void testUpdateBatchById() {
        Employee employee1 = new Employee();
        employee1.setId(6L);
        employee1.setName("妲己");
        employee1.setSex("女");
        employee1.setEmail("daji@cunyu1943.com");
        Employee employee2 = new Employee();
        employee2.setId(13L);
        employee2.setName("小乔");
        employee2.setSex("女");
        employee2.setEmail("xiaoqiao@cunyu1943.com");
        List<Employee> employeeList = new ArrayList<>();
        employeeList.add(employee1);
        employeeList.add(employee2);
        Assert.assertTrue(employeeService.updateBatchById(employeeList));
        System.out.println("更新成功");
    }
}

image.png2.2 分批次更新

  • 方法声明
// 根据ID 批量更新
boolean updateBatchById(Collection<T> entityList, int batchSize);
• 1
• 2
  • 分批次更新实例
package com.cunyu.employee;
import com.cunyu.employee.entity.Employee;
import com.cunyu.employee.mapper.EmployeeMapper;
import com.cunyu.employee.service.EmployeeService;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.ArrayList;
import java.util.List;
@SpringBootTest
class EmployeeApplicationTests {
    @Autowired
    private EmployeeService employeeService;
    @Test
    void testUpdateBatchById() {
        Employee employee1 = new Employee();
        employee1.setId(7L);
        employee1.setName("武则天");
        employee1.setSex("女");
        employee1.setEmail("wuzetian@cunyu1943.com");
        Employee employee2 = new Employee();
        employee2.setId(3L);
        employee2.setName("李元芳");
        employee2.setSex("男");
        employee2.setEmail("liyuanfang@cunyu1943.com");
        List<Employee> employeeList = new ArrayList<>();
        employeeList.add(employee1);
        employeeList.add(employee2);
        Assert.assertTrue(employeeService.updateBatchById(employeeList, 2));
        System.out.println("更新成功");
    }
}

image.png

Get

  1. 根据 ID 查询

将所要查询记录的 id 作为参数,然后将查询到的实体返回。

  • 方法声明
// 根据 ID 查询
T getById(Serializable id);
• 1
• 2
  • 查询实例
package com.cunyu.employee;
import com.cunyu.employee.entity.Employee;
import com.cunyu.employee.mapper.EmployeeMapper;
import com.cunyu.employee.service.EmployeeService;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class EmployeeApplicationTests {
    @Autowired
    private EmployeeService employeeService;
    @Test
    void testGetById() {
        Employee employee = employeeService.getById(9);
        System.out.println(employee);
    }
}

image.png

List

  1. 查询所有

查询所有记录,然后返回到一个集合中。

  • 方法声明
// 查询所有
List<T> list();
• 1
• 2
  • 查询实例
package com.cunyu.employee;
import com.cunyu.employee.entity.Employee;
import com.cunyu.employee.mapper.EmployeeMapper;
import com.cunyu.employee.service.EmployeeService;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.ArrayList;
import java.util.List;
@SpringBootTest
class EmployeeApplicationTests {
    @Autowired
    private EmployeeService employeeService;
    @Test
    void testList() {
        List<Employee> employeeLists = new ArrayList<>();
        employeeLists = employeeService.list();
        Assert.assertEquals(6, employeeLists.size());
        System.out.println("查询成功");
    }
}

image.png根据 ID 批量查询

讲所要查询的记录 id 传入集合,然后座位方法参数,最后返回查询到的结果到一个集合中。


方法声明

// 查询(根据ID 批量查询)

Collection<T> listByIds(Collection<? extends Serializable> idList);


批量查询实例


package com.cunyu.employee;
import com.cunyu.employee.entity.Employee;
import com.cunyu.employee.mapper.EmployeeMapper;
import com.cunyu.employee.service.EmployeeService;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.ArrayList;
import java.util.List;
@SpringBootTest
class EmployeeApplicationTests {
    @Autowired
    private EmployeeService employeeService;
    @Test
    void testListByIds() {
        List<Long> ids = new ArrayList<>();
        ids.add(6L);
        ids.add(7L);
        Assert.assertEquals(2, employeeService.listByIds(ids).size());
        System.out.println("查询成功");
    }
}

image.png根据条件查询

条件传入 Map 集合,key 对应字段,value 对应值,然后返回集合。


方法声明

// 查询(根据 columnMap 条件)

Collection<T> listByMap(Map<String, Object> columnMap);


根据条件查询实例


package com.cunyu.employee;
import com.cunyu.employee.entity.Employee;
import com.cunyu.employee.mapper.EmployeeMapper;
import com.cunyu.employee.service.EmployeeService;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.HashMap;
import java.util.Map;
@SpringBootTest
class EmployeeApplicationTests {
    @Autowired
    private EmployeeService employeeService;
    @Test
    void testListByMap() {
        Map<String, Object> map = new HashMap<>();
        map.put("sex", "女");
        Assert.assertEquals(3, employeeService.listByMap(map).size());
        System.out.println("查询成功");
    }
}

image.png

  1. 查询所有列表
  • 方法声明
// 查询所有列表
List<Map<String, Object>> listMaps();
• 1
• 2
  • 查询实例
package com.cunyu.employee;
import com.cunyu.employee.entity.Employee;
import com.cunyu.employee.mapper.EmployeeMapper;
import com.cunyu.employee.service.EmployeeService;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class EmployeeApplicationTests {
    @Autowired
    private EmployeeService employeeService;
    @Test
    void testListMaps() {
        Assert.assertEquals(6, employeeService.listMaps());
        System.out.println("查询成功");
    }
}

image.png

  1. 查询所有记录

用于查询所有数据记录,并将其返回到一个集合中。

  • 方法声明
// 查询全部记录
List<Object> listObjs();
• 1
• 2
  • 查询实例
package com.cunyu.employee;
import com.cunyu.employee.entity.Employee;
import com.cunyu.employee.mapper.EmployeeMapper;
import com.cunyu.employee.service.EmployeeService;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class EmployeeApplicationTests {
    @Autowired
    private EmployeeService employeeService;
    @Test
    void testListObjs() {
        Assert.assertEquals(6, employeeService.listObjs().size());
        System.out.println("查询成功");
    }
}

image.png

Count

  1. 查询记录总数

用于统计数据控中的记录总条数,方法返回记录条数。

  • 方法声明
// 查询总记录数
int count();
• 1
• 2
  • 查询记录总数实例
package com.cunyu.employee;
import com.cunyu.employee.entity.Employee;
import com.cunyu.employee.mapper.EmployeeMapper;
import com.cunyu.employee.service.EmployeeService;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class EmployeeApplicationTests {
    @Autowired
    private EmployeeService employeeService;
    @Test
    void testCount() {
        Assert.assertEquals(6, employeeService.count());
        System.out.println("查询成功");
    }
}

image.png总结

好了,以上就是关于 Mybatis-Plus 的日志配置以及如何进行 CRUD 的相关内容了,这里 CRUD 主要又分为 Mapper 层和 Service 层,我们可以根据自己的需要进行选择。当然,在我们日常使用中,常常都是两个接口一起使用,关于更多 MP 的使用技巧,我们下期文章再见!



相关实践学习
【涂鸦即艺术】基于云应用开发平台CAP部署AI实时生图绘板
【涂鸦即艺术】基于云应用开发平台CAP部署AI实时生图绘板
目录
相关文章
|
15天前
|
XML Java 数据库连接
MyBatis的常见配置
MyBatis 常见配置包括数据库连接、类型别名、映射器等核心模块,合理配置可提升开发效率与系统性能。主要内容涵盖核心配置文件结构、关键配置项详解及配置优先级说明。
122 4
|
10天前
|
缓存 Java 应用服务中间件
Spring Boot配置优化:Tomcat+数据库+缓存+日志,全场景教程
本文详解Spring Boot十大核心配置优化技巧,涵盖Tomcat连接池、数据库连接池、Jackson时区、日志管理、缓存策略、异步线程池等关键配置,结合代码示例与通俗解释,助你轻松掌握高并发场景下的性能调优方法,适用于实际项目落地。
160 4
|
30天前
|
SQL XML Java
通过MyBatis的XML配置实现灵活的动态SQL查询
总结而言,通过MyBatis的XML配置实现灵活的动态SQL查询,可以让开发者以声明式的方式构建SQL语句,既保证了SQL操作的灵活性,又简化了代码的复杂度。这种方式可以显著提高数据库操作的效率和代码的可维护性。
142 18
|
6月前
|
Oracle 关系型数据库 Java
【YashanDB知识库】Mybatis-Plus适配崖山配置
【YashanDB知识库】Mybatis-Plus适配崖山配置
|
安全 BI 网络安全
EventLog Analyzer 如何满足等保合规要求?密码有效期、产品日志保留、配置备份三大核心问题全面解答
EventLog Analyzer(ELA)助力企业满足网络安全等级保护要求,支持配置自动/手动备份、日志180天留存及密码策略管理,提升合规性与安全运营效率。
|
6月前
|
SQL Java 数据库连接
微服务——SpringBoot使用归纳——Spring Boot使用slf4j进行日志记录—— application.yml 中对日志的配置
在 Spring Boot 项目中,`application.yml` 文件用于配置日志。通过 `logging.config` 指定日志配置文件(如 `logback.xml`),实现日志详细设置。`logging.level` 可定义包的日志输出级别,例如将 `com.itcodai.course03.dao` 包设为 `trace` 级别,便于开发时查看 SQL 操作。日志级别从高到低为 ERROR、WARN、INFO、DEBUG,生产环境建议调整为较高级别以减少日志量。本课程采用 yml 格式,因其层次清晰,但需注意格式要求。
622 0
|
6月前
|
Java 数据库连接 微服务
微服务——MyBatis配置——事务管理
本段内容主要介绍了事务管理的两种类型:JDBC 和 MANAGED。JDBC 类型直接利用数据源连接管理事务,依赖提交和回滚机制;而 MANAGED 类型则由容器全程管理事务生命周期,例如 JEE 应用服务器上下文,默认会关闭连接,但可根据需要设置 `closeConnection` 属性为 false 阻止关闭行为。此外,提到在使用 Spring + MyBatis 时,无需额外配置事务管理器,因为 Spring 模块自带的功能可覆盖上述配置,且这两种事务管理器类型均无需设置属性。
95 0
|
6月前
|
Java 数据库连接 数据库
微服务——MyBatis配置——多环境配置
在 MyBatis 中,多环境配置允许为不同数据库创建多个 SqlSessionFactory。通过传递环境参数给 SqlSessionFactoryBuilder,可指定使用哪种环境;若忽略,则加载默认环境。`environments` 元素定义环境配置,包括默认环境 ID、事务管理器和数据源类型等。每个环境需唯一标识,确保默认环境匹配其中之一。代码示例展示了如何构建工厂及配置 XML 结构。
95 0
|
6月前
|
缓存 Java 数据库连接
微服务——MyBatis配置——常见配置
本文介绍了 MyBatis 的常见配置及其加载顺序。属性配置优先级为:方法参数传递的属性 &gt; resource/url 属性中配置 &gt; properties 元素中指定属性。同时列举了多个关键配置项,如 `cacheEnabled`(全局缓存开关)、`lazyLoadingEnabled`(延迟加载)、`useGeneratedKeys`(使用 JDBC 自动生成主键)等,并详细说明其作用、有效值及默认值。这些配置帮助开发者优化 MyBatis 的性能与行为。
97 0
|
2月前
|
JSON 安全 Go
Go语言项目工程化 —— 日志、配置、错误处理规范
本章详解Go语言项目工程化核心规范,涵盖日志、配置与错误处理三大关键领域。在日志方面,强调其在问题排查、性能优化和安全审计中的作用,推荐使用高性能结构化日志库zap,并介绍日志级别与结构化输出的最佳实践。配置管理部分讨论了配置分离的必要性,对比多种配置格式如JSON、YAML及环境变量,并提供viper库实现多环境配置的示例。错误处理部分阐述Go语言显式返回error的设计哲学,讲解标准处理方式、自定义错误类型、错误封装与堆栈追踪技巧,并提出按调用层级进行错误处理的建议。最后,总结各模块的工程化最佳实践,助力构建可维护、可观测且健壮的Go应用。