1.MyBatis-Plus通用mapper CRUD

1.MyBatis-Plus通用mapper CRUD

记录个人学习过程

使用到的类

package com.example.wx_test.entity;
/*部门*/
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;


@TableName("t_department")
@Data
public class Department {
//    @TableId(type = IdType.AUTO)
    private Integer id; // 编号

    private String name; // 名称

    private String remark; // 备注

}

部门对应的mapper

package com.example.wx_test.mapper;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.wx_test.entity.Department;

public interface DepartmentMapper extends BaseMapper<Department> {

}

部门对应数据表:t_department

字段名类型注释
idint部门id
namevarchar部门名称
remarkvarchar备注
package com.example.wx_test.entity;
/*员工*/
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

@TableName("t_employee")
@Data
public class Employee {
    private Integer id;

    private String name;

    private String gender;

    private String email;
    private String phoneNumber;
    @TableField(value = "departmentId")
    private Integer departmentId;
    private Integer salary;

}

员工对应的mapper

package com.example.wx_test.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.wx_test.entity.Employee;

public interface EmployeeMapper extends BaseMapper<Employee> {

}

员工对应数据表:t_employee

字段名类型注释
idint员工id
namevarchar员工姓名
birthdaydate出生日期
gendervarchar性别
emailvarchar邮箱
phone_numbervarchar电话
departmentIdint所属部门id
salaryint薪资

1.select

selectList

查找t_department表中全部数据

@Test
public void select(){
        List<Department> departmentList = departmentMapper.selectList(null);

    }

selectList()中的参数为queryWrapper,是条件构造器中的内容

selectById

通过id查找

@Test
    public void selectById(){
        Department department = departmentMapper.selectById(1);
        System.out.println(department);
    }

selectBatchIds

通过多个id进行查找

@Test
    public void selectBatchIds(){
        List<Integer> idList=new ArrayList<>();
        idList.add(1);
        idList.add(2);
        idList.add(3);
        List<Department> departmentList = departmentMapper.selectBatchIds(idList);
        System.out.println(departmentList);
    }

selectByMap

通过Map进行查找

@Test
    public void selectByMap(){
        Map<String,Object> columnMap=new HashMap<>();
        columnMap.put("name","测试");
        columnMap.put("remark","xx");
        List<Department> departmentList = departmentMapper.selectByMap(columnMap);
        System.out.println(departmentList);
    }

2.insert

@Test
    public void insert(){
        Department department=new Department();
        department.setName("测试名称6");
        department.setRemark("测试备注");
        int affectRow = departmentMapper.insert(department);
        if (affectRow>0){
            System.out.println("插入成功");
        }
        else{
            System.out.println("插入失败");
        }
    }

departmentMapper.insert()返回的值是int类型,表示执行此操作后数据库中受到影响的记录行数。此处插入一条数据,若插入成功则返回1。

3.update

@Test
    public void updateById() {
        Department department = new Department();
        department.setId(6);
        department.setName("总经理办公室2");
        department.setRemark("老大2");
        int affectRows = departmentMapper.updateById(department);
        if (affectRows > 0) {
            System.out.println("更新成功");
        } else {
            System.out.println("更新失败");
        }
    }

departmentMapper.updateById()返回的值是int类型,表示执行此操作后数据库中受到影响的记录行数。此处执行update操作,只更新了一条记录,返回值为1。

4.delete

deleteById

通过id删除

@Test
    public void deleteById(){
        int affectRows = departmentMapper.deleteById(9);
        if(affectRows>0){
            System.out.println("删除成功");
        }else{
            System.out.println("删除失败");
        }
    }

deleteByMap

通过map删除

@Test
public void deleteByMap(){
    Map<String,Object> map=new HashMap<>();
    map.put("name","测试名称6");
    map.put("remark","测试备注");
    int affectRows = departmentMapper.deleteByMap(map);
    if(affectRows>0){
        System.out.println("删除成功");
    }else{
        System.out.println("删除失败");
    }
}

deleteBatchIds

删除多个id

@Test
public void deleteBatchIds(){
    List<Integer> idList=new LinkedList<>();
    idList.add(11);
    idList.add(6);
    idList.add(10);
    int affectRows =departmentMapper.deleteBatchIds(idList);
    if(affectRows>0){
        System.out.println("删除成功");
    }else{
        System.out.println("删除失败");
    }

}

参考资料:

https://www.bilibili.com/video/BV1KV411U7pH?p=1&spm_id_from=pageDriver


版权声明:本文为Snakewood原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。