
🍁博客主页:👉不会压弯的小飞侠
✨欢迎关注:👉点赞👍收藏⭐留言✒
✨系列专栏:SSM框架整合专栏
✨如果觉得博主的文章还不错的话,请三连支持一下博主。
🔥欢迎大佬指正,一起 学习!一起加油!

(点击直接查看) 进行学习。(点击直接查看)(点击直接查看)🔥改进一下实体类
@Data 注解 <dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
🔥编写实体类Book
package com.study.domain;
import lombok.Data;
@Data
public class Book {
private Integer id;
private String name;
private String type;
private String description;
}
int insert(T t)int deleteById(Serializable id)int updateById(T t)T selectById(Serializable id)List selectList() IPage selectPage( IPage page) Page selectPage(Wrapper querywrapper) 可以点击直接查看进行学习)。package com.study.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.study.domain.Book;
import org.apache.ibatis.annotations.*;
@Mapper
public interface BookDao extends BaseMapper<Book> {
}
🔥测试dao功能
编写dao层测试类:
package com.study;
import com.study.dao.BookDao;
import com.study.domain.Book;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
class SssmApplicationTests {
@Autowired
private BookDao bookDao;
@Test
public void select(){
List<Book> books = bookDao.selectList(null);
System.out.println(books);
}
}
测试结果:

🔥报错:在测试使用Mybatis-plus查询数据库时报错,报错信息大概意思时在数据库中没有book这张表。
🔥解决办法:
🔥配置application.yml
server:
port: 80
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/springboot?serverTimezone=UTC
username: root
password: root
mybatis-plus:
global-config:
db-config:
table-prefix: tbl_
运行测试:查询全部数据成功!

🔥为方便调试可以开启MyBatisPlus的日志
🔥 使用配置方式开启日志,设置日志输出方式为标准输出
server:
port: 80
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/springboot?serverTimezone=UTC
username: root
password: root
mybatis-plus:
global-config:
db-config:
table-prefix: tbl_
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
测试:日志开启成功!

package com.study.config;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MpConfig {
@Bean
//设置分页拦截器
public MybatisPlusInterceptor pageInterceptor(){
//定义MP拦截器
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
//添加具体的拦截器
interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return interceptor;
}
}
🔥分页测试
@Test
void selectPage(){
IPage page = new Page(2, 4);
bookDao.selectPage(page, null);
System.out.println("当前页码"+page.getCurrent());
System.out.println("每页数据总量"+page.getSize());
System.out.println("总页数"+page.getPages());
System.out.println("数据总量"+page.getTotal());
System.out.println("当前页数据"+page.getRecords());
}
测试结果:

快速开发方案
🔥使用通用接口(ISerivce)快速开发Service
定义接口
package com.study.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.study.domain.Book;
import org.springframework.transaction.annotation.Transactional;
@Transactional
public interface BookService extends IService<Book> {
}
package com.study.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.study.dao.BookDao;
import com.study.domain.Book;
import com.study.service.BookService;
import org.springframework.stereotype.Service;
@Service
public class BookServiceImpl extends ServiceImpl<BookDao,Book> implements BookService {
}
🔥测试service功能
package com.study;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.study.domain.Book;
import com.study.service.BookService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class BookTest {
@Autowired
private BookService BookService;
@Test
void save(){
Book b=new Book();
b.setType("文学");
b.setName("白鹿原");
b.setDescription("好看");
BookService.save(b);
}
@Test
void update(){
Book b=new Book();
b.setId(15);
b.setType("文学");
b.setName("白鹿原");
b.setDescription("666");
BookService.updateById(b);
}
@Test
void delete(){
BookService.removeById(15);
}
@Test
void findById(){
System.out.println(BookService.getById(1));
}
@Test
void findAll(){
BookService.list();
}
@Test
void page(){
IPage<Book> page = new Page<Book>(2,5);
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());
}
}
测试查询全部数据的功能:

BookService
package com.study.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import com.study.domain.Book;
import org.springframework.transaction.annotation.Transactional;
@Transactional
public interface BookService extends IService<Book> {
boolean saveBook(Book book);
boolean modify(Book book);
boolean delete(Integer id);
IPage<Book> getPage(Integer currentPage, Integer pageSize);
IPage<Book> getPage(Integer currentPage, Integer pageSize, Book book);
}
BookServiceImpl
@Service
public class BookServiceImpl extends ServiceImpl<BookDao, Book> implements BookService {
@Autowired
private BookDao bookDao;
@Override
public boolean saveBook(Book book) {
return bookDao.insert(book) > 0;
}
@Override
public boolean modify(Book book) {
return bookDao.updateById(book) > 0;
}
@Override
public boolean delete(Integer id) {
return bookDao.deleteById(id) > 0;
}
}
BookController
@RestController
@RequestMapping("/books")
public class BookController {
@Autowired
private BookService bookService;
@GetMapping
public Result getAll(){
return new Result(true, bookService.list());
}
@PostMapping
public Result save(@RequestBody Book book) {
return new Result( bookService.saveBook(book));
}
@PutMapping
public Result update(@RequestBody Book book) {
return new Result(bookService.modify(book));
}
@DeleteMapping("{id}")
public Result delete(@PathVariable Integer id){
return new Result(bookService.delete(id));
}
@GetMapping("{id}")
public Result getById(@PathVariable Integer id){
return new Result(true, bookService.getById(id));
}
🔥Postman测试


🔥列表、新增、修改、删除,异常消息处理,功能就随便测试一个看能不能运行,修改为例:


<!--分页组件-->
<div class="pagination-container">
<el-pagination
class="pagiantion"
@current-change="handleCurrentChange"
:current-page="pagination.currentPage"
:page-size="pagination.pageSize"
layout="total, prev, pager, next, jumper"
:total="pagination.total">
</el-pagination>
</div>
data: {
pagination: { // 分页相关模型数据
currentPage: 1, // 当前页码
pageSize: 5, // 每页显示的记录数
total: 0, // 总记录数
}
},
getAll() {
axios.get("/books/" + this.pagination.currentPage + "/" + this.pagination.pageSize).then((res) => {
});
},
@GetMapping("/{currentPage}/{pageSize}")
public Result getPage(@PathVariable int currentPage, @PathVariable int pageSize) {
return new Result(true, bookService.getPage(currentPage, pageSize));
}
//分页查询
getAll() {
// 发送异步请求
axios.get("/books/" + this.pagination.currentPage + "/" + this.pagination.pageSize).then((res) => {
this.pagination.pageSize = res.data.data.size;
this.pagination.currentPage = res.data.data.current;
this.pagination.total = res.data.data.total;
this.dataList = res.data.data.records;
});
},
运行结果:

//切换页码
handleCurrentChange(currentPage) {
//修改页码值为当前选中的页码值
this.pagination.currentPage = currentPage;
//执行查询
this.getAll();
},
@GetMapping("/{currentPage}/{pageSize}")
public Result getPage(@PathVariable int currentPage, @PathVariable int pageSize) {
IPage<Book> page = bookService.getPage(currentPage, pageSize);
// 如果当前页码值大于总页码值,那么重新执行查询操作时,使用最大页码值作为当前页码值
if (currentPage > page.getPages()) {
page = bookService.getPage((int) page.getPages(), pageSize);
}
return new Result(true, page);
}
pagination: {//分页相关模型数据
currentPage: 1,//当前页码
pageSize: 5,//每页显示的记录数
total: 0,//总记录数
type: "",
name: "",
description: ""
}
<div class="filter-container">
<el-input placeholder="图书类别" v-model="pagination.type" style="width: 200px;"
class="filter-item"></el-input>
<el-input placeholder="图书名称" v-model="pagination.name" style="width: 200px;"
class="filter-item"></el-input>
<el-input placeholder="图书描述" v-model="pagination.description" style="width: 200px;"
class="filter-item"></el-input>
<el-button @click="getAll()" class="dalfBut">查询</el-button>
<el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
</div>
//分页查询
getAll() {
// 组织参数,拼接url请求地址
param = "?type=" + this.pagination.type;
param += "&name=" + this.pagination.name;
param += "&description=" + this.pagination.description;
// console.log(param);
// 发送异步请求
axios.get("/books/" + this.pagination.currentPage + "/" + this.pagination.pageSize + param).then((res) => {
this.pagination.pageSize = res.data.data.size;
this.pagination.currentPage = res.data.data.current;
this.pagination.total = res.data.data.total;
this.dataList = res.data.data.records;
});
},
@GetMapping("/{currentPage}/{pageSize}")
public Result getPage(@PathVariable int currentPage, @PathVariable int pageSize, Book book) {
System.out.println("参数====>" + book);
IPage<Book> page = bookService.getPage(currentPage, pageSize);
// 如果当前页码值大于总页码值,那么重新执行查询操作时,使用最大页码值作为当前页码值
if (currentPage > page.getPages()) {
page = bookService.getPage((int) page.getPages(), pageSize);
}
return new Result(true, page);
}
运行测试:

IPage<Book> getPage(int currentPage, int pageSize, Book book);
@Override
public IPage<Book> getPage(int currentPage, int pageSize, Book book) {
LambdaQueryWrapper<Book> lqw = new LambdaQueryWrapper();
lqw.like(Strings.isNotEmpty(book.getType()), Book::getType, book.getType());
lqw.like(Strings.isNotEmpty(book.getName()), Book::getName, book.getName());
lqw.like(Strings.isNotEmpty(book.getDescription()), Book::getDescription, book.getDescription());
IPage page = new Page(currentPage, pageSize);
bookDao.selectPage(page, lqw);
return page;
}
@GetMapping("/{currentPage}/{pageSize}")
public R getPage(@PathVariable int currentPage, @PathVariable int pageSize, Book book) {
// System.out.println("参数====>" + book);
IPage<Book> page = bookService.getPage(currentPage, pageSize, book);
// 如果当前页码值大于总页码值,那么重新执行查询操作时,使用最大页码值作为当前页码值
if (currentPage > page.getPages()) {
page = bookService.getPage((int) page.getPages(), pageSize, book);
}
return new R(true, page);
}
运行测试:
