mybatis-plus通用Service

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<Department> {
}

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 ServiceImpl<DepartmentMapper,Department> implements 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)
@SpringBootTest
public class DepartmentServiceTest {
  
    @Autowired
    private DepartmentService departmentService;
  
    /**
     * 添加测试
     */
    @Test
    public void save(){
        Department department=new Department();
        department.setName("牛逼");
        department.setRemark("xxx");
        boolean save = departmentService.save(department);
        System.out.println(save);
    }
  
    /**
     * 修改测试
     */
    @Test
    public void updateById(){
        Department department=new Department();
        department.setId(9);
        department.setName("牛逼2");
        department.setRemark("xxx2");
        boolean save = departmentService.updateById(department);
        System.out.println(save);
    }
  
    /**
     * 删除
     */
    @Test
    public void removeById(){
        boolean b = departmentService.removeById(9);
        System.out.println(b);
    }
  
    /**
     * 查询
     */
    @Test
    public void list(){
        List<Department> list = departmentService.list();
        System.out.println(list);
    }
  
    /**
     * 批量添加
     */
    @Test
    public void saveBatch(){
        Department department1=new Department();
        department1.setName("牛逼1");
        department1.setRemark("xxx1");
  
        Department department2=new Department();
        department2.setName("牛逼2");
        department2.setRemark("xxx2");
  
        Department department3=new Department();
        department3.setName("牛逼3");
        department3.setRemark("xxx3");
  
        boolean b = departmentService.saveBatch(Arrays.asList(department1, department2, department3));
        System.out.println(b);
    }
  
    /**
     * 批量添加或者更新
     */
    @Test
    public void saveOrUpdateBatch(){
        Department department1=new Department();
        department1.setName("牛逼4");
        department1.setRemark("xxx4");
  
        Department department2=new Department();
        department2.setId(11);
        department2.setName("牛逼2x");
        department2.setRemark("xxx2x");
  
        boolean b = departmentService.saveOrUpdateBatch(Arrays.asList(department1, department2));
        System.out.println(b);
    }
  
  
  
  
  
}

 

 

------------------------------------------------------------------------------------------------------------------------------

作者: java1234_小锋

出处:https://www.cnblogs.com/java688/p/13672159.html

版权:本站使用「CC BY 4.0」创作共享协议,转载请在文章明显位置注明作者及出处。

------------------------------------------------------------------------------------------------------------------------------

 

posted @ 2020-09-15 15:23  java锋哥  阅读(7921)  评论(0编辑  收藏  举报