《从头开始学SpringBoot》系列------第二篇
内容包括:
1)SpringBoot实现ssmp整合
2)SpringBoot整合ssmp的案例
目录
1.整合SSMP
1.1整合JUnit
1.导入测试对应的starter
2.测试类使用@SpringBootTest修饰
3.使用自动装配的形式添加要测试的对象
4.测试类如果存在于引导类所在包或子包中无需指定引导类
5.测试类如果不存在于引导类所在的包或子包中需要通过classes属性指定引导类
java
@SpringBootTest
class Springboot04JunitApplicationTests {
//注入你要测试的对象
@Autowired
private BookDao bookDao;
@Test
void contextLoads() {
//执行要测试的对象对应的方法
bookDao.save();
System.out.println("two...");
}
}
1.2整合Mybatis
1.2.1导入对应的starter
XML
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
1.2.2配置相关信息
XML
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/ssm?serverTimezone=UTC
username: root
password: root
1.2.3dao(或是mapper)测试

1.3整合MybatisPlus
1.3.1导入对应的starter
XML
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3</version>
</dependency>
1.3.2配置相关信息
XML
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/ssm?serverTimezone=UTC
username: root
password: root

1.3.3定义数据层接口与映射配置
java
@Mapper
public interface BookDao extends BaseMapper<Book> {
}


1.4整合Druid
1.4.1导入坐标
XML
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.6</version>
</dependency>
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.6</version>
</dependency>
</dependencies>
1.4.2修改配置
修改前
XML
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/ssm?serverTimezone=UTC
username: root
password: root
修改后
XML
spring:
datasource:
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/ssm?serverTimezone=UTC
username: root
password: root
2.整合SSMP案例
2.1案例实现效果

2.2模块创建
2.2.1pom文件
XML
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--继承且指定springboot的版本-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.4</version>
</parent>
<artifactId>ssmp</artifactId>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.2.2启动类
java
@SpringBootApplication
public class SSMPApplication {
public static void main(String[] args) {
SpringApplication.run(SSMPApplication.class,args);
}
}
2.2.3配置文件
application.yml
XML
server:
port: 80
spring:
datasource:
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/ssm?serverTimezone=UTC
username: root
password: root
此时项目结构为

2.3实体类开发
2.3.1对应的表结构
sql
-- ----------------------------
-- Table structure for tbl_book
-- ----------------------------
DROP TABLE IF EXISTS `tbl_book`;
CREATE TABLE `tbl_book` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`type` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`description` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 51 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of tbl_book
-- ----------------------------
INSERT INTO `tbl_book` VALUES (1, '计算机理论', 'Spring实战 第5版', 'Spring入门经典教程,深入理解Spring原理技术内幕');
INSERT INTO `tbl_book` VALUES (2, '计算机理论', 'Spring 5核心原理与30个类手写实战', '十年沉淀之作,手写Spring精华思想');
INSERT INTO `tbl_book` VALUES (3, '计算机理论', 'Spring 5 设计模式', '深入Spring源码剖析Spring源码中蕴含的10大设计模式');
INSERT INTO `tbl_book` VALUES (4, '计算机理论', 'Spring MVC+MyBatis开发从入门到项目实战', '全方位解析面向Web应用的轻量级框架,带你成为Spring MVC开发高手');
INSERT INTO `tbl_book` VALUES (5, '计算机理论', '轻量级Java Web企业应用实战', '源码级剖析Spring框架,适合已掌握Java基础的读者');
INSERT INTO `tbl_book` VALUES (6, '计算机理论', 'Java核心技术 卷I 基础知识(原书第11版)', 'Core Java 第11版,Jolt大奖获奖作品,针对Java SE9、10、11全面更新');
INSERT INTO `tbl_book` VALUES (7, '计算机理论', '深入理解Java虚拟机', '5个维度全面剖析JVM,大厂面试知识点全覆盖');
INSERT INTO `tbl_book` VALUES (8, '计算机理论', 'Java编程思想(第4版)', 'Java学习必读经典,殿堂级著作!赢得了全球程序员的广泛赞誉');
INSERT INTO `tbl_book` VALUES (9, '计算机理论', '零基础学Java(全彩版)', '零基础自学编程的入门图书,由浅入深,详解Java语言的编程思想和核心技术');
INSERT INTO `tbl_book` VALUES (10, '市场营销', '直播就该这么做:主播高效沟通实战指南', '李子柒、李佳琦、薇娅成长为网红的秘密都在书中');
INSERT INTO `tbl_book` VALUES (11, '市场营销', '直播销讲实战一本通', '和秋叶一起学系列网络营销书籍');
INSERT INTO `tbl_book` VALUES (12, '市场营销', '直播带货:淘宝、天猫直播从新手到高手', '一本教你如何玩转直播的书,10堂课轻松实现带货月入3W+');
2.3.2实体类
1)使用lombok加快实体类开发
导入坐标
XML
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
2)domain

java
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("tbl_book")
public class Book {
private Integer id;
private String type;
private String name;
private String description;
}
2.4数据层开发
使用mybatisplus进行数据层的开发
2.4.1基础CRUD
①导入坐标
mybatisplus和druid的start以及mysql驱动(前面已经导入了)
XML
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
②配置信息
配置数据库连接相关的数据源配置(前面已经配置过了)
XML
server:
port: 80
spring:
datasource:
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/ssm?serverTimezone=UTC
username: root
password: root
③使用BashMapper加速开发

java
@Mapper
public interface BookDao extends BaseMapper<Book> {
@Select("select * from tbl_book where id = #{id}")
public Book getById(Integer id);
}
④测试类进行测试
java
@SpringBootTest
public class SSMPApplicationTest {
@Autowired
private BookDao bookDao;
//测试查询
@Test
public void test(){
System.out.println(bookDao.selectById(1));
//System.out.println(bookDao.getById(2));
}
//测试新增
@Test
public void test2(){
Book book = new Book();
book.setType("测试数据1");
book.setName("测试数据1");
book.setDescription("测试数据1");
bookDao.insert(book);
}
//测试删除
@Test
public void test3(){
System.out.println(bookDao.deleteById(53));
}
//测试修改
@Test
public void test4(){
Book book = new Book();
book.setId(54);
book.setType("测试数据11");
book.setName("测试数据11");
book.setDescription("测试数据11");
System.out.println("修改条数;" + bookDao.updateById(book));
}
//测试查询全部
@Test
public void test5(){
List<Book> list = bookDao.selectList(null);
for(Book book : list)
System.out.println(book);
}
}

XML
mybatis-plus:
global-config:
db-config:
id-type: auto #设置主键id字段的生成策略为参照数据库设定的策略,当前数据库设置id生成策略为自增
⑤运行结果

⑥查看mybatisplus运行日志

XML
mybatis-plus:
global-config:
db-config:
id-type: auto #设置主键id字段的生成策略为参照数据库设定的策略,当前数据库设置id生成策略为自增
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #开启mybatisplus运行日志

2.4.2分页功能制作
①拦截器开启

java
@Configuration
public class MPConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return interceptor;
}
}
上述代码第一行是创建MyBatisPlus的拦截器栈,这个时候拦截器栈中没有具体的拦截器,第二行是初始化了分页拦截器,并添加到拦截器栈中。如果后期开发其他功能,需要添加全新的拦截器,按照第二行的格式继续add进去新的拦截器就可以了。
②使用Page对象
java
//测试分页查询
@Test
public void test6(){
IPage page = new Page(2,5);
bookDao.selectPage(page,null);
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()); //详细数据
}

2.4.3条件查询功能制作
java
//测试条件查询
@Test
public void test7(){
String name = "1";
LambdaQueryWrapper<Book> lqw = new LambdaQueryWrapper<>();
lqw.like(Strings.isNotEmpty(name),Book::getName,name);
List<Book> books = bookDao.selectList(lqw);
for(Book book : books)
System.out.println(book);
}
2.5业务层开发
组织业务逻辑功能,并根据业务需求,对数据持久层发起调用
2.5.1业务层标准开发

①BookService
java
public interface BookService {
Boolean save(Book book);
Boolean update(Book book);
Boolean delevte(Integer id);
Book getById(Integer id);
List<Book> getAll();
IPage<Book> getPage(int currentPage, int pageSize);
}
②BookServiceImpl
java
@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 delevte(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<Book> page = new Page<>(currentPage,pageSize);
bookDao.selectPage(page,null);
return page;
}
}
③BookServiceTest
java
@SpringBootTest
public class BookServiceTest {
@Autowired
private BookService bookService;
//通过id获取一个数据
@Test
public void test1(){
System.out.println(bookService.getById(3));
}
//获取全部数据
@Test
public void test2(){
List<Book> all = bookService.getAll();
for(Book book : all)
System.out.println(book);
}
//根据id删除数据
@Test
public void test3(){
System.out.println("删除:" + bookService.delevte(55));
}
//根据id修改数据
@Test
public void test4(){
Book book = new Book(55,"修改2","修改2","修改2");
System.out.println("修改:" + bookService.update(book));
}
//新增一条数据
@Test
public void test5(){
Book book = new Book(null,"测试2","测试2","测试2");
System.out.println("保存:" + bookService.save(book));
}
//分页查询
@Test
public void test6(){
IPage<Book> page = bookService.getPage(2, 5);
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());
}
}
2.5.2业务层快速开发(不推荐)
①IBookService
java
public interface IBookService extends IService<Book> {
}
②IBookServiceImpl
java
@Service
public class IBookServiceImpl extends ServiceImpl<BookDao, Book> implements IBookService{
}
③IBookServiceTest
java
@SpringBootTest
public class IBookServiceTest {
@Autowired
private IBookService iBookService;
//测试根据id删除数据
@Test
public void test1(){
Book byId = iBookService.getById(4);
}
}
小结

2.6表现层开发
2.6.1标准版Service的Controller开发
①controller
java
@RestController
@RequestMapping("/books")
public class BookController {
@Autowired
private BookService bookService;
//获取全部数据
@GetMapping()
public List<Book> getAll(){
return bookService.getAll();
}
//根据id获取数据
@GetMapping("/{id}")
public Book getById(@PathVariable Integer id){
return bookService.getById(id);
}
//根据id删除数据
@DeleteMapping("/{id}")
public Boolean delete(@PathVariable Integer id){
return bookService.delete(id);
}
//更新数据
@PutMapping()
public Boolean update(@RequestBody Book book){
return bookService.update(book);
}
//保存数据
@PostMapping()
public Boolean save(@RequestBody Book book){
return bookService.save(book);
}
//分页查询
@GetMapping("/{currentPage}/{pageSize}")
public IPage<Book> getPage(@PathVariable int currentPage, @PathVariable int pageSize){
return bookService.getPage(currentPage,pageSize);
}
}
2.6.2快速版Service的Controller开发
①controller2
java
@RestController
@RequestMapping("/books")
public class BookController2 {
@Autowired
private IBookService iBookService;
//获取全部数据
@GetMapping()
public List<Book> getAll(){
return iBookService.list();
}
//根据id获取数据
@GetMapping("/{id}")
public Book getById(@PathVariable Integer id){
return iBookService.getById(id);
}
//根据id删除数据
@DeleteMapping("/{id}")
public Boolean delete(@PathVariable Integer id){
return iBookService.removeById(id);
}
//更新数据
@PutMapping()
public Boolean update(@RequestBody Book book){
LambdaQueryWrapper<Book> lqw = new LambdaQueryWrapper<>();
lqw.eq(book.getId()!=null,Book::getId,book.getId());
return iBookService.update(book,lqw);
}
//保存数据
@PostMapping()
public Boolean save(@RequestBody Book book){
return iBookService.save(book);
}
//分页查询
@GetMapping("/{currentPage}/{pageSize}")
public IPage<Book> getPage(@PathVariable int currentPage,@PathVariable int pageSize){
IPage<Book> page = new Page<>(currentPage,pageSize);
return iBookService.page(page);
}
}
2.6.3表现层消息一致性处理
前后端数据协议
①返回结果类

java
@Data
@AllArgsConstructor
@NoArgsConstructor
public class R {
private Boolean flag;
private Object data;
}
②修改后的表现层标准版开发
java
@RestController
@RequestMapping("/books")
public class BookController {
@Autowired
private BookService bookService;
//获取全部数据
@GetMapping()
public R getAll(){
List<Book> list = bookService.getAll();
return new R(true,list);
}
//根据id获取数据
@GetMapping("/{id}")
public R getById(@PathVariable Integer id){
Book book = bookService.getById(id);
return new R(true,book);
}
//根据id删除数据
@DeleteMapping("/{id}")
public R delete(@PathVariable Integer id){
Boolean flag = bookService.delete(id);
return new R(flag,null);
}
//更新数据
@PutMapping()
public R update(@RequestBody Book book){
Boolean flag = bookService.update(book);
return new R(flag,null);
}
//保存数据
@PostMapping()
public R save(@RequestBody Book book){
Boolean flag = bookService.save(book);
return new R(flag,null);
}
//分页查询
@GetMapping("/{currentPage}/{pageSize}")
public R getPage(@PathVariable int currentPage, @PathVariable int pageSize){
IPage<Book> page = bookService.getPage(currentPage, pageSize);
return new R(true,page);
}
}
2.7前后端连通性测试
2.7.1拷贝前端页面
将页面资源拷贝到resource里的static文件夹里

2.7.2启动资源
maven先clean一下,然后重启服务输入地址查看页面是否加载成功

2.7.3测试钩子函数
html
//钩子函数,VUE对象初始化完成后自动执行
created() {
this.getAll();
},
html
methods: {
//列表
getAll() {
axios.get("/books").then((res)=>{
console.log(res.data);
});
},

2.8页面基础功能开发
2.8.1列表功能(非分页版)
列表功能主要操作就是加载完数据,将数据展示到页面上,此处要利用VUE的数据模型绑定,发送请求得到数据,然后页面上读取指定数据即可。
只需修改getAll方法
html
//列表
getAll() {
axios.get("/books").then((res)=>{
this.dataList = res.data.data;
});
},
2.8.2添加功能
添加功能用于收集数据的表单是通过一个弹窗展示的,因此在添加操作前首先要进行弹窗的展示,添加后隐藏弹窗即可。因为这个弹窗一直存在,因此当页面加载时首先设置这个弹窗为不可显示状态,需要展示,切换状态即可。

①添加操作
html
//添加
handleAdd () {
//发送异步请求
axios.post("/books",this.formData).then((res)=>{
//如果操作成功,关闭弹层,显示数据
if(res.data.flag){
this.dialogFormVisible = false;
this.$message.success("添加成功");
}else {
this.$message.error("添加失败");
}
}).finally(()=>{
this.getAll();
});
},

②取消添加操作
html
//取消
cancel(){
this.dialogFormVisible = false;
this.$message.info("操作取消");
},
2.8.3删除功能
html
// 删除
handleDelete(row) {
//1.弹出提示框
this.$confirm("此操作永久删除当前数据,是否继续?","提示",{
type:'info'
}).then(()=>{
//2.做删除业务
axios.delete("/books/"+row.id).then((res)=>{
if(res.data.flag){
this.$message.success("删除成功");
}else{
this.$message.error("删除失败");
}
}).finally(()=>{
this.getAll();
});
}).catch(()=>{
//3.取消删除
this.$message.info("取消删除操作");
});
},
2.8.4修改功能
①显示具体数据
html
//弹出编辑窗口
handleUpdate(row) {
axios.get("/books/"+row.id).then((res)=>{
if(res.data.flag){
//展示弹层,加载数据
this.formData = res.data.data;
this.dialogFormVisible4Edit = true;
}else{
this.$message.error("数据同步失败,自动刷新");
}
});
},
②修改数据
修改操作
html
//修改
handleEdit() {
axios.put("/books",this.formData).then((res)=>{
//如果操作成功,关闭弹层并刷新页面
if(res.data.flag){
this.dialogFormVisible4Edit = false;
this.$message.success("修改成功");
}else {
this.$message.error("修改失败,请重试");
}
}).finally(()=>{
this.getAll();
});
},
取消编辑操作

③修改功能小结

2.9业务消息一致性处理
2.9.1修改返回结果类

2.9.2表现层做统一的异常处理

java
@RestControllerAdvice
public class ProjectExceptionAdvice {
@ExceptionHandler(Exception.class)
public R doOtherException(Exception ex){
//记录日志
//发送消息给运维
//发送邮件给开发人员,ex对象发送给开发人员
ex.printStackTrace();
return new R(false,null,"系统错误,请稍后再试!");
}
}
2.10页面功能开发
2.10.1分页功能
①getAll
修改getAll方法作为默认分页查询的方法
html
//列表
getAll() {
// axios.get("/books").then((res)=>{
// this.dataList = res.data.data;
// });
axios.get("/books/"+this.pagination.currentPage+"/"+this.pagination.pageSize).then((res) => {
this.pagination.total = res.data.data.total;
this.pagination.currentPage = res.data.data.current;
this.pagination.pagesize = res.data.data.size;
this.dataList = res.data.data.records;
});
},
②分页组件
html
<!--分页组件-->
<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>
③分页数据模型
html
data:{
pagination: {
//分页相关模型数据
currentPage: 1, //当前页码
pageSize:10, //每页显示的记录数
total:0, //总记录数
}
},
④页码切换
html
//切换页码
handleCurrentChange(currentPage) {
this.pagination.currentPage = currentPage;
this.getAll();
},
2.10.2删除功能维护
修改bug:最后一页删除数据后,分页查询页码超出范围
修改分页查询的controller
java
//分页查询
@GetMapping("/{currentPage}/{pageSize}")
public R 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 R(true,page);
}
2.10.3条件查询功能
①页面封装查询条件字段
html
pagination: {//分页相关模型数据
currentPage: 1,//当前页码
pageSize:4,//每页显示的记录数
total:0,//总记录数
name: "",
type: "",
description: ""
}
②页面添加字段对应的数据模型绑定名称
html
<div class="filter-container">
<el-input placeholder="图书类别" style="width: 200px;" v-model="pagination.type" class="filter-item"></el-input>
<el-input placeholder="图书名称" style="width: 200px;" v-model="pagination.name" class="filter-item"></el-input>
<el-input placeholder="图书描述" style="width: 200px;" v-model="pagination.description" class="filter-item"></el-input>
<el-button @click="getAll()" class="dalfBut">查询</el-button>
<el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
</div>
③将查询条件组织成url参数添加到请求地址中
html
getAll() {
// axios.get("/books").then((res)=>{
// this.dataList = res.data.data;
// });
//获取查询条件,拼接查询条件
param = "?name="+this.pagination.name;
param += "&type="+this.pagination.type;
param += "&description="+this.pagination.description;
console.log("-----------------"+ param);
axios.get("/books/"+this.pagination.currentPage+"/"+this.pagination.pageSize + param).then((res) => {
this.pagination.total = res.data.data.total;
this.pagination.currentPage = res.data.data.current;
this.pagination.pagesize = res.data.data.size;
this.dataList = res.data.data.records;
});
},
④修改controller中分页查询方法
java
//分页查询
@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);
}
⑤修改service与impl的方法
1)service
java
IPage<Book> getPage(int currentPage, int pageSize, Book queryBook);
2)serviceImpl
java
@Override
public IPage<Book> getPage(int currentPage, int pageSize, Book queryBook) {
IPage<Book> page = new Page<>(currentPage,pageSize);
LambdaQueryWrapper<Book> lqw = new LambdaQueryWrapper<>();
lqw.like(Strings.isNotEmpty(queryBook.getName()),Book::getName,queryBook.getName());
lqw.like(Strings.isNotEmpty(queryBook.getType()),Book::getType,queryBook.getType());
lqw.like(Strings.isNotEmpty(queryBook.getDescription()),Book::getDescription,queryBook.getDescription());
bookDao.selectPage(page,lqw);
return page;
}
⑥结果



内容来源于黑马、尚硅谷教程,仅作为学习笔记参考
