1.Book实体类
package com.pro.domain;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
@TableName("book")
public class Book {
@TableId(type = IdType.AUTO)
@TableField("bookId")
private Long bookId;
private String bookName;
private Long categoryId;
private String subTitle;
private String author;
private String cover;
private String description;
private float evaluationScore;
private Integer evaluationQuantity;
@Override
public String toString() {
return "Book{" +
"bookId=" + bookId +
", bookName='" + bookName + '\'' +
", categoryId=" + categoryId +
", subTitle='" + subTitle + '\'' +
", author='" + author + '\'' +
", cover='" + cover + '\'' +
", description='" + description + '\'' +
", evaluationScore=" + evaluationScore +
", evaluationQuantity=" + evaluationQuantity +
'}';
}
public Long getBookId() {
return bookId;
}
public void setBookId(Long bookId) {
this.bookId = bookId;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public Long getCategoryId() {
return categoryId;
}
public void setCategoryId(Long categoryId) {
this.categoryId = categoryId;
}
public String getSubTitle() {
return subTitle;
}
public void setSubTitle(String subTitle) {
this.subTitle = subTitle;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getCover() {
return cover;
}
public void setCover(String cover) {
this.cover = cover;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public float getEvaluationScore() {
return evaluationScore;
}
public void setEvaluationScore(float evaluationScore) {
this.evaluationScore = evaluationScore;
}
public Integer getEvaluationQuantity() {
return evaluationQuantity;
}
public void setEvaluationQuantity(Integer evaluationQuantity) {
this.evaluationQuantity = evaluationQuantity;
}
}
@TableName("book")对照表名 @TableId(type = IdType.AUTO) @TableField("bookId")如果数据库属性名不一致,可以调整
2.BookDao
public interface BookDao extends BaseMapper<Book>{
}
3.BookService
public interface BookService {
/**
* @param categoryId 分类
* @param order 排序
* @param page 页码
* @param rows 页显示行数
* @return
*/
public IPage<Book> pageing(Long categoryId, String order, Integer page, Integer rows);
}
BookServiceImpl
@Service
public class BookServiceImpl implements BookService {
@Autowired
private BookDao bookDao;
@Override
public IPage<Book> pageing(Long categoryId, String order, Integer page, Integer rows) {
Page<Book> p = new Page<>(page, rows);
QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
//类别已选,可以加入sql中
if (categoryId != null && categoryId != -1) {
queryWrapper.eq("categoryId", categoryId);
}
if (order != null) {
if("quantity".equals(order)){
queryWrapper.orderByDesc("evaluationQuantity");
}else if("score".equals(order)){
queryWrapper.orderByDesc("evaluationScore");
}
}
IPage<Book> bookPage = bookDao.selectPage(p, queryWrapper);
return bookPage;
}
}
4.BookServiceTest
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class BookServiceTest {
@Autowired
private BookService bookService;
@Test
public void pageing() {
IPage<Book> bookIPage = bookService.pageing(1,2);
List<Book> bookList = bookIPage.getRecords();
for (Book book:bookList
) {
System.out.println(book);
}
System.out.println(bookIPage.getPages());
System.out.println(bookIPage.getTotal());
System.out.println(bookIPage.getCurrent());
System.out.println(bookIPage.getSize());
}
}
写完service ,立马建立test测试是否能取到数据
分别是总页数,总记录数,当前页,页显示行数