分页插件:PageHelper+BootStrap+Vue+axios实现分页功能
创始人
2024-06-02 17:56:21
0

本教程需要有MyBatisPlus基础,学习MyBatisPlus请参考
SpringBoot第 14 讲:SpringBoot+MyBatisPlus

案例效果

在这里插入图片描述

技术栈

后端技术:SpringBoot2.7.9、MyBatisPlus3.5.1、MySQL8
前端技术:Vue2.5.16+axios、BootStrap3.3.7

一、后端开发

1.1、配置MyBatisPlus

1.1.1、在pom.xml中添加依赖

		org.springframework.bootspring-boot-starter-webcom.baomidoumybatis-plus-boot-starter3.5.1org.projectlomboklombok1.18.20mysqlmysql-connector-java8.0.32

1.1.2、在application.yml中配置数据源

server:port: 8070
spring:datasource:type: com.zaxxer.hikari.HikariDataSourcedriver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://127.0.01:3306/test_plus?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=trueusername: rootpassword: Aa123123.
mybatis-plus:type-aliases-package: demo.entityconfiguration:log-impl: org.apache.ibatis.logging.stdout.StdOutImplglobal-config:db-config:table-prefix: t_id-type: automapper-locations: classpath:mappers/*.xml

1.1.3、在启动类中配置分页插件

package demo;import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@SpringBootApplication
@Configuration
@MapperScan("demo.mapper")
public class Demo {/** 配置分页插件*/@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));return interceptor;}public static void main(String[] args) {SpringApplication.run(Demo.class);}
}

1.2、后台开发

1.2.1、SQL脚本

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;-- ----------------------------
-- Table structure for t_article
-- ----------------------------
DROP TABLE IF EXISTS `t_article`;
CREATE TABLE `t_article` (`id` bigint(20) NOT NULL AUTO_INCREMENT,`title` varchar(50) DEFAULT NULL,`logo` varchar(255) DEFAULT NULL,`descn` varchar(160) DEFAULT NULL,`create_time` datetime DEFAULT NULL,`cid` bigint(20) DEFAULT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1630090627736408069 DEFAULT CHARSET=utf8;-- ----------------------------
-- Records of t_article
-- ----------------------------
BEGIN;
INSERT INTO `t_article` (`id`, `title`, `logo`, `descn`, `create_time`, `cid`) VALUES (1, '扬子xx', NULL, 'eeeeeeee', '2023-01-02 00:00:00', 2);
INSERT INTO `t_article` (`id`, `title`, `logo`, `descn`, `create_time`, `cid`) VALUES (1630090618022400001, '扬子晚报1', NULL, 'dddsfjslfjalskd', '2023-01-03 00:00:00', 2);
INSERT INTO `t_article` (`id`, `title`, `logo`, `descn`, `create_time`, `cid`) VALUES (1630090625282740226, '扬子晚报2', NULL, 'dddsfjslfjalskd', '2023-01-04 00:00:00', 2);
INSERT INTO `t_article` (`id`, `title`, `logo`, `descn`, `create_time`, `cid`) VALUES (1630090625865748482, '扬子晚报3', NULL, 'dddsfjslfjalskd', '2023-01-05 00:00:00', 1);
INSERT INTO `t_article` (`id`, `title`, `logo`, `descn`, `create_time`, `cid`) VALUES (1630090626520059905, '扬子晚报4', NULL, 'dddsfjslfjalskd', '2023-01-06 00:00:00', 2);
INSERT INTO `t_article` (`id`, `title`, `logo`, `descn`, `create_time`, `cid`) VALUES (1630090627165982721, '扬子晚报5', NULL, 'dddsfjslfjalskd', '2023-01-07 00:00:00', 1);
INSERT INTO `t_article` (`id`, `title`, `logo`, `descn`, `create_time`, `cid`) VALUES (1630090627736408066, '扬子晚报6', NULL, 'dddsfjslfjalskd', '2023-01-08 00:00:00', 1);
INSERT INTO `t_article` (`id`, `title`, `logo`, `descn`, `create_time`, `cid`) VALUES (1630090627736408068, 'qqq1w', 'kdsjlsfjieosjfiosjdfo', 'eeeee', '2023-02-03 00:00:00', 2);
COMMIT;-- ----------------------------
-- Table structure for t_channel
-- ----------------------------
DROP TABLE IF EXISTS `t_channel`;
CREATE TABLE `t_channel` (`id` bigint(20) NOT NULL,`channel_name` varchar(50) DEFAULT NULL,`create_time` datetime DEFAULT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;-- ----------------------------
-- Records of t_channel
-- ----------------------------
BEGIN;
INSERT INTO `t_channel` (`id`, `channel_name`, `create_time`) VALUES (1, '头条', '2023-02-27 17:27:14');
INSERT INTO `t_channel` (`id`, `channel_name`, `create_time`) VALUES (2, '头七', '2023-02-27 17:27:14');
COMMIT;SET FOREIGN_KEY_CHECKS = 1;

1.2.2、实体Article

package demo.entity;import lombok.Data;@Data
public class Article {private Long id;private String title;private String logo;private String descn;private String createTime;private Long cid;
}

1.2.3、ArticleMapper.java

package demo.mapper;import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import demo.entity.Article;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;@Repository
public interface ArticleMapper extends BaseMapper
{Page
selectByPage(Page
page, @Param("title") String title); }

1.2.4、ArticleMapper.xml





1.2.5、ArticleController

package demo.controller;import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import demo.entity.Article;
import demo.mapper.ArticleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@CrossOrigin
public class ArticleController {@Autowiredprivate ArticleMapper articleMapper;@GetMapping("/list")public Page
findAll(Long pageIndex, Long pageSize){Page
page = articleMapper.selectPage(new Page(pageIndex, pageSize), null);return page;}@GetMapping("/list_custom")public Page
customFindAll(Long pageIndex, Long pageSize, String title){Page
page = articleMapper.selectByPage(new Page(pageIndex, pageSize), title);return page;} }

二、前端开发

2.1、html代码

	
文章列表
编号标题描述发布时间操作
{{art.id}}{{art.title}}{{art.descn}}{{art.createTime}}

2.2、Js代码

	

三、附录

3.1、完整po.m.xml文件


4.0.0org.springframework.bootspring-boot-starter-parent2.7.9 com.examplepage-helper-demo0.0.1-SNAPSHOTpage-helper-demoDemo project for Spring Boot1.8org.springframework.bootspring-boot-starter-webcom.baomidoumybatis-plus-boot-starter3.5.1org.projectlomboklombok1.18.20mysqlmysql-connector-java8.0.32org.springframework.bootspring-boot-maven-plugin

相关内容

热门资讯

中证A500ETF摩根(560... 8月22日,截止午间收盘,中证A500ETF摩根(560530)涨1.19%,报1.106元,成交额...
A500ETF易方达(1593... 8月22日,截止午间收盘,A500ETF易方达(159361)涨1.28%,报1.104元,成交额1...
何小鹏斥资约2.5亿港元增持小... 每经记者|孙磊    每经编辑|裴健如 8月21日晚间,小鹏汽车发布公告称,公司联...
中证500ETF基金(1593... 8月22日,截止午间收盘,中证500ETF基金(159337)涨0.94%,报1.509元,成交额2...
中证A500ETF华安(159... 8月22日,截止午间收盘,中证A500ETF华安(159359)涨1.15%,报1.139元,成交额...
科创AIETF(588790)... 8月22日,截止午间收盘,科创AIETF(588790)涨4.83%,报0.760元,成交额6.98...
创业板50ETF嘉实(1593... 8月22日,截止午间收盘,创业板50ETF嘉实(159373)涨2.61%,报1.296元,成交额1...
港股异动丨航空股大幅走低 中国... 港股航空股大幅下跌,其中,中国国航跌近7%表现最弱,中国东方航空跌近5%,中国南方航空跌超3%,美兰...
电网设备ETF(159326)... 8月22日,截止午间收盘,电网设备ETF(159326)跌0.25%,报1.198元,成交额409....
红利ETF国企(530880)... 8月22日,截止午间收盘,红利ETF国企(530880)跌0.67%,报1.034元,成交额29.0...