与BaseMapper类似,MP也封装了业务层的各种方法。简化了开发过程:
-
使用MyBatisPlus提供有业务层通用接口(ISerivce<T>)快速开发service接口,使用业务层通用实现类(ServiceImpl<M,T>)快速开发service实现类;
-
在通用类基础上做功能重载或功能追加;
-
可以编写额外方法,注意重载时不要覆盖原始操作,避免原始提供的功能丢失。
接口:
public interface IBookService extends IService<Book> {
boolean saveBook(Book book);
}
实现类:
@Service
public class QuickBookServiceImp extends ServiceImpl<BookMapper, Book> implements IBookService {
@Autowired
private BookMapper bookMapper;
@Override
public boolean saveBook(Book book) {
return bookMapper.insert(book) > 0;
}
}
测试:
@SpringBootTest
public class QuickServiceTestCase {
@Autowired
private IBookService bookService;
@Test
void testGetById() {
System.out.println(bookService.getById(1));
}
@Test
public void testSave() {
Book book = new Book();
book.setName("test");
book.setType("CH");
book.setDescription("service");
bookService.save(book);
}
@Test
void testUpdate(){
Book book = new Book();
book.setId(4);
book.setDescription("测试数据service");
bookService.updateById(book);
}
@Test
void testDelete(){
bookService.removeById(4);
}
@Test
void testGetAll(){
bookService.list();
}
@Test
public void testGetPage() {
IPage page = new Page(2,1);
bookService.page(page);
System.out.println(page.getCurrent());
System.out.println(page.getSize());
System.out.println(page.getTotal());
System.out.println(page.getPages());
System.out.println(page.getRecords());
}
}