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