Mybatis-plus 入门案例 通用Service

通用Service分析

 标准service:接口 + 实现

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;

import com.czxy.domain.Customer;
import com.czxy.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 TestCustomerMapperDome08_service {

    @Resource
    private CustomerService customerService;

    //通用service
    //查询所有
    @Test
    public void testSelectList(){
        List<Customer> list = customerService.list();
        list.forEach(System.out::println);
    }


    //添加
    @Test
    public void testInsert(){
        Customer customer = new Customer();
        customer.setCname("张三");
        customer.setPassword("9999");
        //插入一条记录(选择字段,策略插入)
        customerService.save(customer);
    }


    // 根据 ID 选择修改
    @Test
    public void testUpdate(){
        Customer customer = new Customer();
        customer.setCid(9);
        customer.setCname("777");
        customer.setPassword("777");
        customerService.updateById(customer);

    }

    //TableId 注解存在更新记录,否插入一条记录
    @Test
    public void testSaveOrUpdate(){

        Customer customer = new Customer();
        customer.setCid(10);
        customer.setCname("999");
        customer.setPassword("9999");
        customerService.saveOrUpdate(customer);
    }

    @Test
    public void testDelete(){
        customerService.removeById(10);
    }

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值