This is the 9th day of my participation in the August Wen Challenge.More challenges in August

preface

In the last article, we explained the definition and related features of Mybatis-Plus, and wrote an example of SpringBoot + Mybatis-Plus from scratch. Today we will look at how to use MP to achieve the increase, delete, change and check of the database.

The log configuration

When MP is used, no SQL statements are printed by default. In order to facilitate the debugging of daily development work, we need the joint console and various data visualization tools for statement splicing inspection, so we use the log function of MP to output our SQL statements in the console, so as to facilitate our debugging.

In the configuration file application.yml (the default configuration file generated by IDEA is application.properties), add the following configuration, so that MP will print the complete SQL statement with parameters in the console for us to view.

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

Mapper CRUD usage

First we look at the Mapper layer CRUD involved in some methods, Mapper layer is mainly inherited from BaseMapper interface, inside the implementation of a variety of methods used to operate the database add, delete, change, check, the following we will look at some of the daily we commonly used methods.

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 then *@description: Employee Mapper class */
public interface EmployeeMapper extends BaseMapper<Employee> {}Copy the code

Insert operations

The first step is to insert data. In the INSERT method, we pass in the entity object we want to insert into the database as a parameter.

  • Method statement
/** * Insert a record **@paramEntity Entity object */
int insert(T entity);
Copy the code
  • Insert the instance
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(a) {}@Autowired
    private EmployeeMapper employeeMapper;

    @Test
    void testInsert(a) {
        Employee employee = new Employee();
        employee.setId(4L);
        employee.setName("Daisy");
        employee.setSex("Male");
        employee.setEmail("[email protected]");

        Assert.assertEquals(1, employeeMapper.insert(employee));
        System.out.println("Insert successful"); }}Copy the code
  • The test results

  • After data is inserted into the database

Select the operating

There are more ways to query data than to insert data, and batch and conditional queries can also be implemented.

  1. Query by primary key

We do this by passing the primary key of the data to be queried as an argument to our selectById method.

  • Method statement
/** * query ** by ID@paramId Primary key ID */
T selectById(Serializable id);
Copy the code
  • The query instance
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(a) {}@Autowired
    private EmployeeMapper employeeMapper;

    @Test
    void testSelectById(a) {
        Employee employee = employeeMapper.selectById(3); System.out.println(employee); }}Copy the code
  • The test results

  1. Batch query information by primary key

The previous method could only query one record at a time. If we wanted to query multiple data records, we could pass in the selectBatchIds method the list of primary keys for the data to be queried.

  • Method statement
/** * query (by ID) **@paramIdList List of primary key ids (cannot be null or empty) */
List<T> selectBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
Copy the code
  • Batch Query Instance
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(a) {}@Autowired
    private EmployeeMapper employeeMapper;

    @Test
    void testSelectBatchIds(a) {
        List<Integer> ids = new ArrayList<>();
        ids.add(1);
        ids.add(4); List<Employee> employeeList = employeeMapper.selectBatchIds(ids); System.out.println(employeeList); }}Copy the code
  • The test results

  1. Query by multiple criteria

In addition to supporting primary key queries, MP also supports conditional queries by passing our criteria into the Map list and passing them as parameters to the selectByMap method, where the key passed into the Map corresponds to the field in our database and the value corresponds to the value of the field.

  • Method statement
/** * Query (based on columnMap condition) **@paramColumnMap Table field map object */
List<T> selectByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
Copy the code
  • Conditional Query instance
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(a) {}@Autowired
    private EmployeeMapper employeeMapper;

    @Test
    void testSelectByMap(a) {
        Map<String, Object> map = new HashMap<>();
        map.put("sex"."Male");
        map.put("name"."Zhang"); System.out.println(employeeMapper.selectByMap(map)); }}Copy the code
  • The test results

The update operation

The update operation is based on the primary key of our database, passing the entity object corresponding to the primary key into the updateById method.

  • Method statement
/** * Change ** according to ID@paramEntity Entity object */
int updateById(@Param(Constants.ENTITY) T entity);
Copy the code
  • Update the instance
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(a) {
        Employee employee = new Employee();

        employee.setEmail("[email protected]");
        employee.setName("Zhao 6");
        employee.setSex("Female");
        employee.setId(4L);

        Assert.assertEquals(1, employeeMapper.updateById(employee));
        System.out.println("Update successful"); }}Copy the code
  • The test results

  • Database after data update

The delete operation

Delete operation, you can delete a record according to the primary key, delete in batches according to the primary key list, and delete according to conditions.

  1. Delete a piece of data based on the primary key

Pass the primary key of the record you want to delete as an argument to the deleteById method.

  • Method statement
/** * delete ** based on ID@paramId Primary key ID */
int deleteById(Serializable id);
Copy the code
  • Delete the instance
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(a) {
        Assert.assertEquals(1, employeeMapper.deleteById(2L));
        System.out.println("Deleted successfully"); }}Copy the code
  • The test results

  • The database after deleting data

  1. Delete by condition

Delete by condition also means passing conditions into a Map, and then passing a Map as a parameter to the deleteByMap method, where key corresponds to a field in the database and value corresponds to the value of a field.

  • Method statement
/** * Drop the record ** according to the columnMap condition@paramColumnMap Table field map object */
int deleteByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
Copy the code
  • Delete the instance
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(a) {
        Map<String, Object> map = new HashMap<>();
        map.put("name"."Zhao 6");
        Assert.assertEquals(1, employeeMapper.deleteByMap(map));
        System.out.println("Deleted successfully"); }}Copy the code
  • The test results

  • The database after deleting data

  1. Delete data in batches based on the primary key

The primary key for the record to be deleted is passed into the collection, which is then passed as an argument to the deleteBatchIds method.

  • Method statement
/** * delete (batch delete by ID) **@paramIdList List of primary key ids (cannot be null or empty) */
int deleteBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
Copy the code
  • Batch Deleting Instances
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(a) {
        List<Integer> ids = new ArrayList<>();
        ids.add(1);
        ids.add(3);
        Assert.assertEquals(2, employeeMapper.deleteBatchIds(ids));
        System.out.println("Deleted successfully"); }}Copy the code
  • The test results

  • The database after deleting data

The Service of the CRUD interface

The Service layer inherits the IService interface, and its methods have the same functions as those provided by the Mapper layer. Except for different method names, the other methods are basically similar, but the Service layer provides richer methods. The inheritance structure of the two is shown in the following figure.

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 art *@description: Employee service interface */
@Service
public interface EmployeeService extends IService<Employee> {}Copy the code
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 sacrifice *@description: Employee service class implementation */

@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper.Employee> implements EmployeeService {}Copy the code

Save

  1. Insert a record

The function is the same as the insert method in the Mapper layer, but the method name is different.

  • Method statement
// Insert a record (select field, policy insert)
boolean save(T entity);
Copy the code
  • Insert the instance
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(a) {
        Employee employee = new Employee();
        employee.setId(5L);
        employee.setName("Sunday");
        employee.setEmail("[email protected]");
        employee.setSex("Female");
        Assert.assertTrue(employeeService.save(employee));
        System.out.println("Insert successful"); }}Copy the code
  • The test results

  • After data is inserted into the database

  1. Bulk insert

This is all different from the Mapper layer, Mapper layer only supports single insert, while the Service layer supports batch insert, and the passed parameter is the collection of entities we want to pass, and can also be batch insert and unified insert.

2.1 Unified Insertion

  • Method statement
// Insert (batch)
boolean saveBatch(Collection<T> entityList);
Copy the code
  • Insert the instance
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(a) {
        Employee employee1 = new Employee();
        employee1.setId(6L);
        employee1.setEmail("[email protected]");
        employee1.setSex("Male");
        employee1.setName("Zhang liang");

        Employee employee2 = new Employee();
        employee2.setId(7L);
        employee2.setEmail("[email protected]");
        employee2.setName("Zhou yu");
        employee2.setSex("Male");

        List<Employee> employeeList = new ArrayList<>();
        employeeList.add(employee1);
        employeeList.add(employee2);

        Assert.assertTrue(employeeService.saveBatch(employeeList));
        System.out.println("Batch insert succeeded"); }}Copy the code
  • The test results

  • Unified database after insertion

2.2 Batch insertion

  • Method statement
// Insert (batch)
boolean saveBatch(Collection<T> entityList, int batchSize);
Copy the code
  • Insert instances in batches
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(a) {
        Employee employee1 = new Employee();
        employee1.setId(8L);
        employee1.setEmail("[email protected]");
        employee1.setSex("Female");
        employee1.setName(Caleb "ROM.");

        Employee employee2 = new Employee();
        employee2.setId(9L);
        employee2.setEmail("[email protected]");
        employee2.setName("Zhuge Liang");
        employee2.setSex("Male");

        List<Employee> employeeList = new ArrayList<>();
        employeeList.add(employee1);
        employeeList.add(employee2);

        Assert.assertTrue(employeeService.saveBatch(employeeList,2));
        System.out.println("Batch insert succeeded"); }}Copy the code
  • The test results

  • Batch inserts into the database

SaveOrUpdate

  1. Single modification insert
  • Method statement
// TableId annotation has update record, no insert record
boolean saveOrUpdate(T entity);
Copy the code
  • Single modify insert instance
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(a) {
        Employee employee = new Employee();
        employee.setId(5L);
        employee.setName("Tempo");
        employee.setEmail("[email protected]");
        employee.setSex("Male");

        Assert.assertTrue(employeeService.saveOrUpdate(employee));
        System.out.println("Update successful"); }}Copy the code
  • The test results

  • Modify the inserted database

  1. Batch modify insert

2.1 Unified Insertion

  • Method statement
// Batch modify inserts
boolean saveOrUpdateBatch(Collection<T> entityList);
Copy the code
  • Unified insert instance
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(a) {
        Employee employee1 = new Employee();
        employee1.setId(10L);
        employee1.setEmail("[email protected]");
        employee1.setSex("Female");
        employee1.setName("Wu Yan Zhong");

        Employee employee2 = new Employee();
        employee2.setId(11L);
        employee2.setEmail("[email protected]");
        employee2.setName(Dee Renjie);
        employee2.setSex("Male");

        List<Employee> employeeList = new ArrayList<>();
        employeeList.add(employee1);
        employeeList.add(employee2);

        Assert.assertTrue(employeeService.saveOrUpdateBatch(employeeList));
        System.out.println("Batch modification inserted successfully"); }}Copy the code
  • The test results

  • Unified database after data is inserted

2.2 Batch insertion

  • Method statement
// Batch modify inserts
boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize);
Copy the code
  • Methods the instance
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(a) {
        Employee employee1 = new Employee();
        employee1.setId(12L);
        employee1.setEmail("[email protected]");
        employee1.setSex("Female");
        employee1.setName("Concubine");

        Employee employee2 = new Employee();
        employee2.setId(13L);
        employee2.setEmail("[email protected]");
        employee2.setName("Su Lie");
        employee2.setSex("Male");

        List<Employee> employeeList = new ArrayList<>();
        employeeList.add(employee1);
        employeeList.add(employee2);

        Assert.assertTrue(employeeService.saveOrUpdateBatch(employeeList, 2));
        System.out.println("Batch modification inserted successfully"); }}Copy the code
  • The test results

  • Database after batch insertion of data

Remove

  1. Delete by ID
  • Methods the instance
// Delete by ID
boolean removeById(Serializable id);
Copy the code
  • Delete the instance
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(a) {
        Assert.assertTrue(employeeService.removeById(5));
        System.out.println("Deleted successfully"); }}Copy the code
  • The test results

  1. Delete by condition
  • Method statement
// Delete the record according to the columnMap condition
boolean removeByMap(Map<String, Object> columnMap);
Copy the code
  • Delete instances by condition
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(a) {
        Map<String, Object> map = new HashMap<>();
        map.put("sex"."Female");
        Assert.assertTrue(employeeService.removeByMap(map));
        System.out.println("Deleted successfully"); }}Copy the code
  • The test results

  • The database after conditional deletion

  1. Delete data in batches based on the ID
  • Method statement
// Delete (delete by ID)
boolean removeByIds(Collection<? extends Serializable> idList);
Copy the code
  • Batch Deleting Instances
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(a) {
        List<Integer> ids = new ArrayList<>();
        ids.add(1);
        ids.add(4);
        Assert.assertTrue(employeeService.removeByIds(ids));
        System.out.println("Batch deleted successfully"); }}Copy the code
  • The test results

  • Deleted databases in batches

Update

  1. Select a value based on the ID
  • Method statement
// Select modify according to ID
boolean updateById(T entity);
Copy the code
  • Modify the instance by 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(a) {
        Employee employee = new Employee();
        employee.setId(3L);
        employee.setName("Cheng Bite gold");
        employee.setSex("Male");
        employee.setEmail("[email protected]");

        Assert.assertTrue(employeeService.updateById(employee));
        System.out.println("Update successful"); }}Copy the code
  • The test results

  • Updated database

  1. Batch update based on ID

2.1 Unified Update

  • Method statement
// Batch update by ID
boolean updateBatchById(Collection<T> entityList);
Copy the code
  • Batch Update instances
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(a) {
        Employee employee1 = new Employee();
        employee1.setId(6L);
        employee1.setName("Da ji");
        employee1.setSex("Female");
        employee1.setEmail("[email protected]");

        Employee employee2 = new Employee();
        employee2.setId(13L);
        employee2.setName("Joe");
        employee2.setSex("Female");
        employee2.setEmail("[email protected]");

        List<Employee> employeeList = new ArrayList<>();
        employeeList.add(employee1);
        employeeList.add(employee2);

        Assert.assertTrue(employeeService.updateBatchById(employeeList));
        System.out.println("Update successful"); }}Copy the code
  • The test results

  • Batch updated database

2.2 Update in batches

  • Method statement
// Batch update by ID
boolean updateBatchById(Collection<T> entityList, int batchSize);
Copy the code
  • Update the instance in batches
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(a) {
        Employee employee1 = new Employee();
        employee1.setId(7L);
        employee1.setName(Wu Zetian);
        employee1.setSex("Female");
        employee1.setEmail("[email protected]");

        Employee employee2 = new Employee();
        employee2.setId(3L);
        employee2.setName("Li Yuanfang");
        employee2.setSex("Male");
        employee2.setEmail("[email protected]");

        List<Employee> employeeList = new ArrayList<>();
        employeeList.add(employee1);
        employeeList.add(employee2);

        Assert.assertTrue(employeeService.updateBatchById(employeeList, 2));
        System.out.println("Update successful"); }}Copy the code
  • The test results

  • Batch updated database

Get

  1. Query by ID

Take the ID of the record to be queried as an argument, and return the queried entity.

  • Method statement
// Query by ID
T getById(Serializable id);
Copy the code
  • The query instance
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(a) {
        Employee employee = employeeService.getById(9); System.out.println(employee); }}Copy the code
  • The test results

List

  1. Query all

Query all records and return to a collection.

  • Method statement
// Query all
List<T> list(a);
Copy the code
  • The query instance
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(a) {
        List<Employee> employeeLists = new ArrayList<>();
        employeeLists = employeeService.list();
        Assert.assertEquals(6, employeeLists.size());
        System.out.println("Query successful"); }}Copy the code
  • The test results

  1. Batch query information by ID

The id of the record to be queried is passed into the collection, then the method parameter is entered, and finally the result of the query is returned into a collection.

  • Method statement
// query (by ID)
Collection<T> listByIds(Collection<? extends Serializable> idList);
Copy the code
  • Batch Query Instance
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(a) {
        List<Long> ids = new ArrayList<>();

        ids.add(6L);
        ids.add(7L);

        Assert.assertEquals(2, employeeService.listByIds(ids).size());
        System.out.println("Query successful"); }}Copy the code
  • The test results

  1. Query by condition

The condition is passed in to the Map set, the key for the field, the value for the value, and the set is returned.

  • Method statement
// Query (based on columnMap condition)
Collection<T> listByMap(Map<String, Object> columnMap);
Copy the code
  • Query instances based on conditions
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(a) {
        Map<String, Object> map = new HashMap<>();

        map.put("sex"."Female");

        Assert.assertEquals(3, employeeService.listByMap(map).size());
        System.out.println("Query successful"); }}Copy the code
  • The test results

  1. Query all lists
  • Method statement
// Query all lists
List<Map<String, Object>> listMaps();
Copy the code
  • The query instance
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(a) {
        Assert.assertEquals(6, employeeService.listMaps());
        System.out.println("Query successful"); }}Copy the code
  • The test results

  1. Querying all Records

Use to query all data records and return them to a collection.

  • Method statement
// Query all records
List<Object> listObjs(a);
Copy the code
  • The query instance
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(a) {
        Assert.assertEquals(6, employeeService.listObjs().size());
        System.out.println("Query successful"); }}Copy the code
  • The test results

Count

  1. Total number of Query records

Used to count the total number of records in the data control, method returns the number of records.

  • Method statement
// Query the total number of records
int count(a);
Copy the code
  • Query the total number of record instances
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(a) {
        Assert.assertEquals(6, employeeService.count());
        System.out.println("Query successful"); }}Copy the code
  • The test results

conclusion

Ok, the above is about Mybatis-Plus log configuration and how to carry out CRUD related content, here CRUD is mainly divided into Mapper layer and Service layer, we can choose according to their own needs. Of course, in our daily use, we often use two interfaces together, about more MP use skills, we will see you in the next article!