【黑马程序员SpringBoot2全套视频教程,springboot零基础到项目实战(spring boot2完整版)】
【注意】
Service层接口定义与数据层接口定义具有较大区别,不要混用
【接口定义】
package com.dingjiaxiong.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dingjiaxiong.domain.Book;
import java.util.List;
/**
* ClassName: BookService
* date: 2022/10/17 19:14
*
* @author DingJiaxiong
*/
public interface BookService {
//新增
Boolean save(Book book);
//修改
Boolean update(Book book);
//删除
Boolean delete(Integer id);
//查询单个
Book getById(Integer id);
//查询全部
List<Book> getAll();
//分页查询
IPage<Book> getPage(int currentPage , int pageSize);
}
【实现类定义】
package com.dingjiaxiong.service.impl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dingjiaxiong.Dao.BookDao;
import com.dingjiaxiong.domain.Book;
import com.dingjiaxiong.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* ClassName: BookServiceImpl
* date: 2022/10/17 19:17
*
* @author DingJiaxiong
*/
@Service
public class BookServiceImpl implements BookService {
@Autowired
private BookDao bookDao;
@Override
public Boolean save(Book book) {
return bookDao.insert(book) > 0;
}
@Override
public Boolean update(Book book) {
return bookDao.updateById(book) > 0;
}
@Override
public Boolean delete(Integer id) {
return bookDao.deleteById(id) > 0;
}
@Override
public Book getById(Integer id) {
return bookDao.selectById(id);
}
@Override
public List<Book> getAll() {
return bookDao.selectList(null);
}
@Override
public IPage<Book> getPage(int currentPage, int pageSize) {
IPage page = new Page(currentPage,pageSize);
return bookDao.selectPage(page,null);
}
}
这样就算完成了,测试一下
package com.dingjiaxiong.service;
import com.dingjiaxiong.service.impl.BookServiceImpl;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
/**
* ClassName: BookServiceTestCase
* date: 2022/10/17 19:20
*
* @author DingJiaxiong
*/
@SpringBootTest
public class BookServiceTestCase {
@Autowired
private BookServiceImpl bookService;
@Test
void testGetById(){
System.out.println(bookService.getById(1));
}
}
测试结果

其他方法同理
新增:
@Test
void testSave(){
Book book = new Book();
book.setType("测试数据123");
book.setName("测试数据123");
book.setDescription("测试数据123");
bookService.save(book);
}

修改:
@Test
void testUpdate(){
Book book = new Book();
book.setId(15);
book.setType("测试数据123");
book.setName("测试数据123xxxxxxxxxxxxxxxxxxx");
book.setDescription("测试数据123");
bookService.update(book);
}

删除:
@Test
void testDelete(){
bookService.delete(15);
}

查询全部
@Test
void testGetAll(){
System.out.println(bookService.getAll());
}

分页查询
@Test
void testPage(){
IPage iPage = bookService.getPage(2, 5);
System.out.println(iPage.getCurrent());
System.out.println(iPage.getSize());
System.out.println(iPage.getTotal());
System.out.println(iPage.getPages());
System.out.println(iPage.getRecords());
}

ok, 没毛病
小结一下【这是标准开发】