java1234开源博客系统
博客信息

mybatis-plus通用Service

0
发布时间:『 2020-09-14 14:31』  博客类别:mybatis-plus  阅读(150)

mybatis-plus通用Service


mp框架同样提供了service层的封装支持,让我们能够简化service层的开发;

 

具体使用:

service接口继承IService,service实现类继承ServiceImpl

 

IService提供了所有通用常用的方法,包括批处理添加更新支持,以及lambda支持;

QQ鎴浘20200914142257.jpg

 

具体案例:

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微信公众号
博主信息
Java1234_小锋
(知识改变命运,技术改变世界)
Powered by Java1234 V3.0 Copyright © 2012-2016 Java知识分享网 版权所有