通用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);
}
}