6.MyBatis-Plus 通用Service

6.MyBatis-Plus 通用Service

Service

package com.example.wx_test.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.example.wx_test.entity.Department;

public interface DepartmentService extends IService<Department> {
}

Impl

package com.example.wx_test.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.wx_test.service.DepartmentService;
import com.example.wx_test.entity.Department;
import com.example.wx_test.mapper.DepartmentMapper;
import org.springframework.stereotype.Service;

@Service("departmentService")
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService {

}

save

package com.example.wx_test;
import com.example.wx_test.service.DepartmentService;
import com.example.wx_test.entity.Department;
import org.junit.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;

@RunWith(SpringRunner.class)
@SpringBootTest
public class DepartmentServiceTest {
    @Autowired
    private DepartmentService departmentService;
    @Test
    public void save(){
        Department department = new Department();
        department.setName("测试部门");
        department.setRemark("测试部门remark");
        boolean result = departmentService.save(department);
        System.out.println(result);
    }
}

在这里插入图片描述

updateById

package com.example.wx_test;
import com.example.wx_test.service.DepartmentService;
import com.example.wx_test.entity.Department;
import org.junit.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;

@RunWith(SpringRunner.class)
@SpringBootTest
public class DepartmentServiceTest {
    @Autowired
    private DepartmentService departmentService;
    
    @Test
    public void updateById(){
        Department department = new Department();
        department.setId(17);
        department.setName("测试部门2");
        department.setRemark("测试部门remark2");
        boolean result = departmentService.updateById(department);
        System.out.println(result);
    }
}

在这里插入图片描述

removeById

@Test
    public void removeById(){
        boolean result = departmentService.removeById(17);
        System.out.println(result);
    }

在这里插入图片描述

list

@Test
public void list(){
    List<Department> list = departmentService.list();
    System.out.println(list);
}

在这里插入图片描述
接下来是批处理

saveBatch

@Test
public void saveBatch(){
    Department department1 = new Department();
    department1.setName("测试部门1");
    department1.setRemark("测试部门remark1");

    Department department2 = new Department();
    department2.setName("测试部门2");
    department2.setRemark("测试部门remark2");

    Department department3 = new Department();
    department3.setName("测试部门3");
    department3.setRemark("测试部门remark3");

    boolean result = departmentService.saveBatch(
            Arrays.asList(department1,department2,department3)
    );
    System.out.println(result);
}

在这里插入图片描述

updateBatchById

@Test
public void updateBatchById(){
    Department department1 = new Department();
    department1.setId(18);
    department1.setName("测试部门11");
    department1.setRemark("测试部门remark11");

    Department department2 = new Department();
    department2.setId(19);
    department2.setName("测试部门22");
    department2.setRemark("测试部门remark22");

    Department department3 = new Department();
    department3.setId(20);
    department3.setName("测试部门33");
    department3.setRemark("测试部门remark33");

    boolean result = departmentService.updateBatchById(
            Arrays.asList(department1,department2,department3)
    );
    System.out.println(result);
}

在这里插入图片描述
参考资料

  1. https://www.bilibili.com/video/BV1KV411U7pH?p=20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值