MyBatis-Plus通用Service

通用Service

标准service:接口 + 实现

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bYDGo5xx-1640078756788)(assets/image-20201126163513099.png)]

  • service接口
  package com.czxy.service;
  
  import com.baomidou.mybatisplus.extension.service.IService;
  import com.czxy.domain.Customer;
  

  public interface CustomerService extends IService<Customer> {
  }
  
  • service实现类

    package com.czxy.service.impl;
    
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import com.czxy.domain.Customer;
    import com.czxy.mapper.CustomerMapper;
    import com.czxy.service.CustomerService;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;
    
    
    @Service
    @Transactional
    public class CustomerServiceImpl extends ServiceImpl<CustomerMapper,Customer> implements CustomerService {
    
    }
    
    
  • 测试类
    在这里插入图片描述

package com.czxy.test;

import com.czxy.mp.MybatisPlusApplication;
import com.czxy.mp.domain.Customer;
import com.czxy.mp.service.CustomerService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = MybatisPlusApplication.class)
public class TestMybatisPlusApplication {

    @Resource
    private CustomerService customerService;


    @Test
    public void testList() {
        List<Customer> list = customerService.list();
        list.forEach(System.out::println);
    }

    @Test
    public void testSave() {
        Customer customer = new Customer();
        customer.setCname("张三");
        customer.setPassword("9999");

        //添加
        customerService.save(customer);
    }

    @Test
    public void testUpdate() {
        Customer customer = new Customer();
        customer.setCid(1);
        customer.setCname("张三");

        customerService.updateById(customer);
    }

    @Test
    public void testSaveOrUpdate() {
        Customer customer = new Customer();
        customer.setCid(2);
        customer.setCname("李四");
        customer.setPassword("66666");

        customerService.saveOrUpdate(customer);

    }

    @Test
    public void testRemove() {
        customerService.removeById(2);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值