mybatis-plus通用Service
mp框架同样提供了service层的封装支持,让我们能够简化service层的开发;
具体使用:
service接口继承IService,service实现类继承ServiceImpl
IService提供了所有通用常用的方法,包括批处理添加更新支持,以及lambda支持;
具体案例:
DepartmentService 继承IService
package com.java1234.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.java1234.entity.Department;/**
* @author java1234_小锋
* @site www.java1234.com
* @company Java知识分享网
* @create 2020-09-14 13:45*/
public interface DepartmentService extends IService{
}
DepartmentServiceImpl 继承ServiceImpl类
package com.java1234.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.java1234.entity.Department;
import com.java1234.mapper.DepartmentMapper;
import com.java1234.service.DepartmentService;
import org.springframework.stereotype.Service;/**
* @author java1234_小锋
* @site www.java1234.com
* @company Java知识分享网
* @create 2020-09-14 13:49*/@Service("departmentService")public class DepartmentServiceImpl extends ServiceImplimplements DepartmentService {
}
单元测试:
package com.java1234;
import com.java1234.entity.Department;
import com.java1234.service.DepartmentService;
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;
import java.util.Arrays;
import java.util.List;/**
* 通用service测试
* @author java1234_小锋
* @site www.java1234.com
* @company Java知识分享网
* @create 2020-08-09 23:41*/@RunWith(SpringRunner.class)
@SpringBootTestpublic classDepartmentServiceTest {
@AutowiredprivateDepartmentService departmentService;/**
* 添加测试*/@Testpublic voidsave(){
Department department=newDepartment();
department.setName("牛逼");
department.setRemark("xxx");
boolean save=departmentService.save(department);
System.out.println(save);
}/**
* 修改测试*/@Testpublic voidupdateById(){
Department department=newDepartment();
department.setId(9);
department.setName("牛逼2");
department.setRemark("xxx2");
boolean save=departmentService.updateById(department);
System.out.println(save);
}/**
* 删除*/@Testpublic voidremoveById(){
boolean b= departmentService.removeById(9);
System.out.println(b);
}/**
* 查询*/@Testpublic voidlist(){
List list =departmentService.list();
System.out.println(list);
}/**
* 批量添加*/@Testpublic voidsaveBatch(){
Department department1=newDepartment();
department1.setName("牛逼1");
department1.setRemark("xxx1");
Department department2=newDepartment();
department2.setName("牛逼2");
department2.setRemark("xxx2");
Department department3=newDepartment();
department3.setName("牛逼3");
department3.setRemark("xxx3");
boolean b=departmentService.saveBatch(Arrays.asList(department1, department2, department3));
System.out.println(b);
}/**
* 批量添加或者更新*/@Testpublic voidsaveOrUpdateBatch(){
Department department1=newDepartment();
department1.setName("牛逼4");
department1.setRemark("xxx4");
Department department2=newDepartment();
department2.setId(11);
department2.setName("牛逼2x");
department2.setRemark("xxx2x");
boolean b=departmentService.saveOrUpdateBatch(Arrays.asList(department1, department2));
System.out.println(b);
}
}
------------------------------------------------------------------------------------------------------------------------------
作者:java1234_小锋
版权:本站使用「CC BY 4.0」创作共享协议,转载请在文章明显位置注明作者及出处。
------------------------------------------------------------------------------------------------------------------------------