SpringBoot 整合【Mybatis-Plus实现分页查询】

news/2024/12/29 4:12:39/

目录

1. 添加pom.xml依赖

2. 配置application.yml文件

3. mybatis-plus插件配置核心类

4. 启动类配置

5. 测试数据Sql脚本

6. mybatis-plus代码生成

7. 测试分页查询


1. 添加pom.xml依赖

 <!--mybatis-plus--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.4.0</version></dependency><!--mybatis-plus自动生成代码依赖--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-generator</artifactId><version>3.4.0</version></dependency><!--mybatis-plus模板--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-freemarker</artifactId><version>2.7.4</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.44</version><scope>runtime</scope></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency>

2. 配置application.yml文件

#配置tomcat
server:port: 8080spring:datasource:driver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql://localhost:3306/zmall?useUnicode=true&characterEncoding=UTF-8&useSSL=falseusername: rootpassword: 1234type: com.zaxxer.hikari.HikariDataSourcefreemarker:#设置编码格式charset: UTF-8#后缀suffix:#文档类型content-type: text/html#模板前缀template-loader-path: classpath:/templates/#启用模板enabled: true#配置mybatis
mybatis-plus:#配置SQL映射文件路径mapper-locations: classpath:mapper/*.xml#配置别名type-aliases-package: com.jmh.mp.model#开启驼峰命名configuration:map-underscore-to-camel-case: true
#配置日志输出
logging:level:com.jmh.mp.mapper: debug

3. mybatis-plus插件配置核心类

package com.jmh.mp.config;import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class MybatisPlusConfig {/*** 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题(该属性会在旧插件移除后一同移除)*/@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));return interceptor;}
}

4. 启动类配置

package com.jmh.mp;import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
@MapperScan("com.jmh.mp.mapper")
public class Mp01Application {public static void main(String[] args) {SpringApplication.run(Mp01Application.class, args);}}

5. 测试数据Sql脚本

/*Navicat Premium Data TransferSource Server         : localhostSource Server Type    : MySQLSource Server Version : 50736Source Host           : localhost:3306Source Schema         : zmallTarget Server Type    : MySQLTarget Server Version : 50736File Encoding         : 65001Date: 27/12/2022 15:38:36
*/SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;-- ----------------------------
-- Table structure for zmall_user
-- ----------------------------
DROP TABLE IF EXISTS `zmall_user`;
CREATE TABLE `zmall_user`  (`id` int(20) NOT NULL AUTO_INCREMENT COMMENT '主键',`loginName` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '登录名',`userName` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '用户名',`password` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '密码',`sex` int(2) NOT NULL DEFAULT 1 COMMENT '性别(1:男 0:女)',`identityCode` varchar(60) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '身份证号',`email` varchar(80) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '邮箱',`mobile` varchar(11) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '手机',`type` int(2) NULL DEFAULT 0 COMMENT '类型(1:后台 0:前台)',PRIMARY KEY (`id`) USING BTREE,UNIQUE INDEX `PK__zmall___C96109CC3A81B327`(`loginName`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 621 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = DYNAMIC;-- ----------------------------
-- Records of zmall_user
-- ----------------------------
INSERT INTO `zmall_user` VALUES (2, 'admin', '系统管理员', 'e10adc3949ba59abbe56e057f20f883e', 1, '130406198302141869', 'hello11@bdqn.com', '1583233515', 1);
INSERT INTO `zmall_user` VALUES (521, 'user0', '测试用户0', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120000', 'user0@139.com', '17700000000', 0);
INSERT INTO `zmall_user` VALUES (522, 'user1', '测试用户1', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120001', 'user1@139.com', '17700000001', 0);
INSERT INTO `zmall_user` VALUES (523, 'user2', '测试用户2', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120002', 'user2@139.com', '17700000002', 0);
INSERT INTO `zmall_user` VALUES (524, 'user3', '测试用户3', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120003', 'user3@139.com', '17700000003', 0);
INSERT INTO `zmall_user` VALUES (525, 'user4', '测试用户4', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120004', 'user4@139.com', '17700000004', 0);
INSERT INTO `zmall_user` VALUES (526, 'user5', '测试用户5', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120005', 'user5@139.com', '17700000005', 0);
INSERT INTO `zmall_user` VALUES (527, 'user6', '测试用户6', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120006', 'user6@139.com', '17700000006', 0);
INSERT INTO `zmall_user` VALUES (528, 'user7', '测试用户7', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120007', 'user7@139.com', '17700000007', 0);
INSERT INTO `zmall_user` VALUES (529, 'user8', '测试用户8', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120008', 'user8@139.com', '17700000008', 0);
INSERT INTO `zmall_user` VALUES (530, 'user9', '测试用户9', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120009', 'user9@139.com', '17700000009', 0);
INSERT INTO `zmall_user` VALUES (531, 'user10', '测试用户10', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120010', 'user10@139.com', '17700000010', 0);
INSERT INTO `zmall_user` VALUES (532, 'user11', '测试用户11', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120011', 'user11@139.com', '17700000011', 0);
INSERT INTO `zmall_user` VALUES (533, 'user12', '测试用户12', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120012', 'user12@139.com', '17700000012', 0);
INSERT INTO `zmall_user` VALUES (534, 'user13', '测试用户13', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120013', 'user13@139.com', '17700000013', 0);
INSERT INTO `zmall_user` VALUES (535, 'user14', '测试用户14', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120014', 'user14@139.com', '17700000014', 0);
INSERT INTO `zmall_user` VALUES (536, 'user15', '测试用户15', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120015', 'user15@139.com', '17700000015', 0);
INSERT INTO `zmall_user` VALUES (537, 'user16', '测试用户16', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120016', 'user16@139.com', '17700000016', 0);
INSERT INTO `zmall_user` VALUES (538, 'user17', '测试用户17', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120017', 'user17@139.com', '17700000017', 0);
INSERT INTO `zmall_user` VALUES (539, 'user18', '测试用户18', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120018', 'user18@139.com', '17700000018', 0);
INSERT INTO `zmall_user` VALUES (540, 'user19', '测试用户19', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120019', 'user19@139.com', '17700000019', 0);
INSERT INTO `zmall_user` VALUES (541, 'user20', '测试用户20', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120020', 'user20@139.com', '17700000020', 0);
INSERT INTO `zmall_user` VALUES (542, 'user21', '测试用户21', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120021', 'user21@139.com', '17700000021', 0);
INSERT INTO `zmall_user` VALUES (543, 'user22', '测试用户22', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120022', 'user22@139.com', '17700000022', 0);
INSERT INTO `zmall_user` VALUES (544, 'user23', '测试用户23', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120023', 'user23@139.com', '17700000023', 0);
INSERT INTO `zmall_user` VALUES (545, 'user24', '测试用户24', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120024', 'user24@139.com', '17700000024', 0);
INSERT INTO `zmall_user` VALUES (546, 'user25', '测试用户25', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120025', 'user25@139.com', '17700000025', 0);
INSERT INTO `zmall_user` VALUES (547, 'user26', '测试用户26', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120026', 'user26@139.com', '17700000026', 0);
INSERT INTO `zmall_user` VALUES (548, 'user27', '测试用户27', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120027', 'user27@139.com', '17700000027', 0);
INSERT INTO `zmall_user` VALUES (549, 'user28', '测试用户28', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120028', 'user28@139.com', '17700000028', 0);
INSERT INTO `zmall_user` VALUES (550, 'user29', '测试用户29', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120029', 'user29@139.com', '17700000029', 0);
INSERT INTO `zmall_user` VALUES (551, 'user30', '测试用户30', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120030', 'user30@139.com', '17700000030', 0);
INSERT INTO `zmall_user` VALUES (552, 'user31', '测试用户31', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120031', 'user31@139.com', '17700000031', 0);
INSERT INTO `zmall_user` VALUES (553, 'user32', '测试用户32', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120032', 'user32@139.com', '17700000032', 0);
INSERT INTO `zmall_user` VALUES (554, 'user33', '测试用户33', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120033', 'user33@139.com', '17700000033', 0);
INSERT INTO `zmall_user` VALUES (555, 'user34', '测试用户34', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120034', 'user34@139.com', '17700000034', 0);
INSERT INTO `zmall_user` VALUES (556, 'user35', '测试用户35', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120035', 'user35@139.com', '17700000035', 0);
INSERT INTO `zmall_user` VALUES (557, 'user36', '测试用户36', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120036', 'user36@139.com', '17700000036', 0);
INSERT INTO `zmall_user` VALUES (558, 'user37', '测试用户37', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120037', 'user37@139.com', '17700000037', 0);
INSERT INTO `zmall_user` VALUES (559, 'user38', '测试用户38', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120038', 'user38@139.com', '17700000038', 0);
INSERT INTO `zmall_user` VALUES (560, 'user39', '测试用户39', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120039', 'user39@139.com', '17700000039', 0);
INSERT INTO `zmall_user` VALUES (561, 'user40', '测试用户40', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120040', 'user40@139.com', '17700000040', 0);
INSERT INTO `zmall_user` VALUES (562, 'user41', '测试用户41', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120041', 'user41@139.com', '17700000041', 0);
INSERT INTO `zmall_user` VALUES (563, 'user42', '测试用户42', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120042', 'user42@139.com', '17700000042', 0);
INSERT INTO `zmall_user` VALUES (564, 'user43', '测试用户43', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120043', 'user43@139.com', '17700000043', 0);
INSERT INTO `zmall_user` VALUES (565, 'user44', '测试用户44', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120044', 'user44@139.com', '17700000044', 0);
INSERT INTO `zmall_user` VALUES (566, 'user45', '测试用户45', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120045', 'user45@139.com', '17700000045', 0);
INSERT INTO `zmall_user` VALUES (567, 'user46', '测试用户46', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120046', 'user46@139.com', '17700000046', 0);
INSERT INTO `zmall_user` VALUES (568, 'user47', '测试用户47', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120047', 'user47@139.com', '17700000047', 0);
INSERT INTO `zmall_user` VALUES (569, 'user48', '测试用户48', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120048', 'user48@139.com', '17700000048', 0);
INSERT INTO `zmall_user` VALUES (570, 'user49', '测试用户49', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120049', 'user49@139.com', '17700000049', 0);
INSERT INTO `zmall_user` VALUES (571, 'user50', '测试用户50', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120050', 'user50@139.com', '17700000050', 0);
INSERT INTO `zmall_user` VALUES (572, 'user51', '测试用户51', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120051', 'user51@139.com', '17700000051', 0);
INSERT INTO `zmall_user` VALUES (573, 'user52', '测试用户52', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120052', 'user52@139.com', '17700000052', 0);
INSERT INTO `zmall_user` VALUES (574, 'user53', '测试用户53', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120053', 'user53@139.com', '17700000053', 0);
INSERT INTO `zmall_user` VALUES (575, 'user54', '测试用户54', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120054', 'user54@139.com', '17700000054', 0);
INSERT INTO `zmall_user` VALUES (576, 'user55', '测试用户55', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120055', 'user55@139.com', '17700000055', 0);
INSERT INTO `zmall_user` VALUES (577, 'user56', '测试用户56', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120056', 'user56@139.com', '17700000056', 0);
INSERT INTO `zmall_user` VALUES (578, 'user57', '测试用户57', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120057', 'user57@139.com', '17700000057', 0);
INSERT INTO `zmall_user` VALUES (579, 'user58', '测试用户58', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120058', 'user58@139.com', '17700000058', 0);
INSERT INTO `zmall_user` VALUES (580, 'user59', '测试用户59', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120059', 'user59@139.com', '17700000059', 0);
INSERT INTO `zmall_user` VALUES (581, 'user60', '测试用户60', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120060', 'user60@139.com', '17700000060', 0);
INSERT INTO `zmall_user` VALUES (582, 'user61', '测试用户61', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120061', 'user61@139.com', '17700000061', 0);
INSERT INTO `zmall_user` VALUES (583, 'user62', '测试用户62', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120062', 'user62@139.com', '17700000062', 0);
INSERT INTO `zmall_user` VALUES (584, 'user63', '测试用户63', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120063', 'user63@139.com', '17700000063', 0);
INSERT INTO `zmall_user` VALUES (585, 'user64', '测试用户64', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120064', 'user64@139.com', '17700000064', 0);
INSERT INTO `zmall_user` VALUES (586, 'user65', '测试用户65', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120065', 'user65@139.com', '17700000065', 0);
INSERT INTO `zmall_user` VALUES (587, 'user66', '测试用户66', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120066', 'user66@139.com', '17700000066', 0);
INSERT INTO `zmall_user` VALUES (588, 'user67', '测试用户67', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120067', 'user67@139.com', '17700000067', 0);
INSERT INTO `zmall_user` VALUES (589, 'user68', '测试用户68', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120068', 'user68@139.com', '17700000068', 0);
INSERT INTO `zmall_user` VALUES (590, 'user69', '测试用户69', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120069', 'user69@139.com', '17700000069', 0);
INSERT INTO `zmall_user` VALUES (591, 'user70', '测试用户70', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120070', 'user70@139.com', '17700000070', 0);
INSERT INTO `zmall_user` VALUES (592, 'user71', '测试用户71', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120071', 'user71@139.com', '17700000071', 0);
INSERT INTO `zmall_user` VALUES (593, 'user72', '测试用户72', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120072', 'user72@139.com', '17700000072', 0);
INSERT INTO `zmall_user` VALUES (594, 'user73', '测试用户73', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120073', 'user73@139.com', '17700000073', 0);
INSERT INTO `zmall_user` VALUES (595, 'user74', '测试用户74', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120074', 'user74@139.com', '17700000074', 0);
INSERT INTO `zmall_user` VALUES (596, 'user75', '测试用户75', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120075', 'user75@139.com', '17700000075', 0);
INSERT INTO `zmall_user` VALUES (597, 'user76', '测试用户76', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120076', 'user76@139.com', '17700000076', 0);
INSERT INTO `zmall_user` VALUES (598, 'user77', '测试用户77', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120077', 'user77@139.com', '17700000077', 0);
INSERT INTO `zmall_user` VALUES (599, 'user78', '测试用户78', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120078', 'user78@139.com', '17700000078', 0);
INSERT INTO `zmall_user` VALUES (600, 'user79', '测试用户79', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120079', 'user79@139.com', '17700000079', 0);
INSERT INTO `zmall_user` VALUES (601, 'user80', '测试用户80', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120080', 'user80@139.com', '17700000080', 0);
INSERT INTO `zmall_user` VALUES (602, 'user81', '测试用户81', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120081', 'user81@139.com', '17700000081', 0);
INSERT INTO `zmall_user` VALUES (603, 'user82', '测试用户82', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120082', 'user82@139.com', '17700000082', 0);
INSERT INTO `zmall_user` VALUES (604, 'user83', '测试用户83', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120083', 'user83@139.com', '17700000083', 0);
INSERT INTO `zmall_user` VALUES (605, 'user84', '测试用户84', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120084', 'user84@139.com', '17700000084', 0);
INSERT INTO `zmall_user` VALUES (606, 'user85', '测试用户85', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120085', 'user85@139.com', '17700000085', 0);
INSERT INTO `zmall_user` VALUES (607, 'user86', '测试用户86', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120086', 'user86@139.com', '17700000086', 0);
INSERT INTO `zmall_user` VALUES (608, 'user87', '测试用户87', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120087', 'user87@139.com', '17700000087', 0);
INSERT INTO `zmall_user` VALUES (609, 'user88', '测试用户88', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120088', 'user88@139.com', '17700000088', 0);
INSERT INTO `zmall_user` VALUES (610, 'user89', '测试用户89', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120089', 'user89@139.com', '17700000089', 0);
INSERT INTO `zmall_user` VALUES (611, 'user90', '测试用户90', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120090', 'user90@139.com', '17700000090', 0);
INSERT INTO `zmall_user` VALUES (612, 'user91', '测试用户91', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120091', 'user91@139.com', '17700000091', 0);
INSERT INTO `zmall_user` VALUES (613, 'user92', '测试用户92', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120092', 'user92@139.com', '17700000092', 0);
INSERT INTO `zmall_user` VALUES (614, 'user93', '测试用户93', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120093', 'user93@139.com', '17700000093', 0);
INSERT INTO `zmall_user` VALUES (615, 'user94', '测试用户94', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120094', 'user94@139.com', '17700000094', 0);
INSERT INTO `zmall_user` VALUES (616, 'user95', '测试用户95', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120095', 'user95@139.com', '17700000095', 0);
INSERT INTO `zmall_user` VALUES (617, 'user96', '测试用户96', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120096', 'user96@139.com', '17700000096', 0);
INSERT INTO `zmall_user` VALUES (618, 'user97', '测试用户97', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120097', 'user97@139.com', '17700000097', 0);
INSERT INTO `zmall_user` VALUES (619, 'user98', '测试用户98', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120098', 'user98@139.com', '17700000098', 0);
INSERT INTO `zmall_user` VALUES (620, 'user99', '测试用户99', 'e10adc3949ba59abbe56e057f20f883e', 1, '430104199912120099', 'user99@139.com', '17700000099', 0);SET FOREIGN_KEY_CHECKS = 1;

6. mybatis-plus代码生成

  •  代码生成核心配置类
package com.jmh.mp.generator;import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;public class CodeGenerator {//数据库连接参数public static String driver = "com.mysql.jdbc.Driver";public static String url = "jdbc:mysql://localhost:3306/zmall?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true";public static String username="root";public static String password="1234";//父级别包名称public static String parentPackage = "com.jmh.mp";//项目名设置(如果是SpringCloud项目则需要设置,其他为""即可)public static String projectName="";//代码生成的目标路径public static String generateTo = "/src/main/java";//mapper.xml的生成路径public static String mapperXmlPath = "/src/main/resources/mapper";//控制器的公共基类,用于抽象控制器的公共方法,null值表示没有父类public static String baseControllerClassName ;//业务层的公共基类,用于抽象公共方法public static String baseServiceClassName ;//作者名public static String author = "jmh";//模块名称,用于组成包名public static String modelName = "model";/*** <p>* 读取控制台内容* </p>*/public static String scanner(String tip) {Scanner scanner = new Scanner(System.in);StringBuilder help = new StringBuilder();help.append("请输入" + tip + ":");System.out.println(help.toString());if (scanner.hasNext()) {String ipt = scanner.next();if (StringUtils.isNotBlank(ipt)) {return ipt;}}throw new MybatisPlusException("请输入正确的" + tip + "!");}public static void main(String[] args) {// 代码生成器AutoGenerator mpg = new AutoGenerator();// 全局配置GlobalConfig gc = new GlobalConfig();//设置代码输出目录String projectPath = System.getProperty("user.dir");gc.setOutputDir(projectPath + projectName + generateTo);//作者gc.setAuthor(author);//设置时间类型为Dategc.setDateType(DateType.TIME_PACK);gc.setOpen(false);//设置Mapper.xml的BaseColumnListgc.setBaseColumnList(true);//设置Mapper.xml的BaseResultMapgc.setBaseResultMap(true);// 设置实体属性 Swagger2 注解// gc.setSwagger2(true);mpg.setGlobalConfig(gc);// 数据源配置DataSourceConfig dsc = new DataSourceConfig();dsc.setUrl(url);dsc.setDriverName(driver);dsc.setUsername(username);dsc.setPassword(password);mpg.setDataSource(dsc);// 包配置PackageConfig pc = new PackageConfig();//pc.setModuleName(scanner("模块名"));pc.setParent(parentPackage);//设置包名pc.setEntity(modelName);mpg.setPackageInfo(pc);// 自定义配置InjectionConfig cfg = new InjectionConfig() {@Overridepublic void initMap() {// to do nothing}};// 如果模板引擎是 freemarkerString templatePath = "/templates/mybatis-generator/mapper2.xml.ftl";// 如果模板引擎是 velocity// String templatePath = "/templates/mapper.xml.vm";// 自定义输出配置List<FileOutConfig> focList = new ArrayList<>();// 自定义配置会被优先输出focList.add(new FileOutConfig(templatePath) {@Overridepublic String outputFile(TableInfo tableInfo) {// 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!return projectPath + projectName + mapperXmlPath + pc.getModuleName()+ "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;}});cfg.setFileOutConfigList(focList);mpg.setCfg(cfg);// 配置模板TemplateConfig templateConfig = new TemplateConfig();// 配置自定义输出模板//指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别templateConfig.setMapper("templates/mybatis-generator/mapper2.java");templateConfig.setEntity("templates/mybatis-generator/entity2.java");templateConfig.setService("templates/mybatis-generator/service2.java");templateConfig.setServiceImpl("templates/mybatis-generator/serviceImpl2.java");templateConfig.setController("templates/mybatis-generator/controller2.java");templateConfig.setXml(null);mpg.setTemplate(templateConfig);// 策略配置StrategyConfig strategy = new StrategyConfig();strategy.setNaming(NamingStrategy.underline_to_camel);strategy.setColumnNaming(NamingStrategy.underline_to_camel);strategy.setEntityLombokModel(true);strategy.setRestControllerStyle(true);strategy.setEntitySerialVersionUID(false);//设置controller的父类if (baseControllerClassName!=null) strategy.setSuperControllerClass(baseControllerClassName);//设置服务类的父类if (baseServiceClassName !=null ) strategy.setSuperServiceImplClass(baseServiceClassName);// 写于父类中的公共字段//strategy.setSuperEntityColumns("id");strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));strategy.setControllerMappingHyphenStyle(true);strategy.setTablePrefix("t_","zmall_");mpg.setStrategy(strategy);mpg.setTemplateEngine(new FreemarkerTemplateEngine());mpg.execute();}
}
  •  代码生成所需文件/添加到templates模块下即可(文件夹名称:mybatis-generator)
  • controller2.java.ftl
package ${package.Controller};import org.springframework.web.bind.annotation.RequestMapping;<#if restControllerStyle>
import org.springframework.web.bind.annotation.RestController;
<#else>
import org.springframework.stereotype.Controller;
</#if>
<#if superControllerClassPackage??>
import ${superControllerClassPackage};
</#if>/*** <p>* ${table.comment!} 前端控制器* </p>** @author ${author}* @since ${date}*/
<#if restControllerStyle>
@RestController
<#else>
@Controller
</#if>
@RequestMapping("<#if package.ModuleName?? && package.ModuleName != "">/${package.ModuleName}</#if>/<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>")
<#if kotlin>
class ${table.controllerName}<#if superControllerClass??> : ${superControllerClass}()</#if>
<#else>
<#if superControllerClass??>
public class ${table.controllerName} extends ${superControllerClass} {
<#else>
public class ${table.controllerName} {
</#if>}
</#if>
  • entity2.java.ftl
package ${package.Entity};<#list table.importPackages as pkg>
import ${pkg};
</#list>
<#if swagger2>
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
</#if>
<#if entityLombokModel>
import lombok.Data;
import lombok.EqualsAndHashCode;<#if chainModel>
import lombok.experimental.Accessors;</#if>
</#if>/*** <p>* ${table.comment!}* </p>** @author ${author}* @since ${date}*/
<#if entityLombokModel>
@Data<#if superEntityClass??>
@EqualsAndHashCode(callSuper = true)<#else>
@EqualsAndHashCode(callSuper = false)</#if><#if chainModel>
@Accessors(chain = true)</#if>
</#if>
<#if table.convert>
@TableName("${table.name}")
</#if>
<#if swagger2>
@ApiModel(value="${entity}对象", description="${table.comment!}")
</#if>
<#if superEntityClass??>
public class ${entity} extends ${superEntityClass}<#if activeRecord><${entity}></#if> {
<#elseif activeRecord>
public class ${entity} extends Model<${entity}> {
<#else>
public class ${entity} implements Serializable {
</#if><#if entitySerialVersionUID>private static final long serialVersionUID = 1L;
</#if>
<#-- ----------  BEGIN 字段循环遍历  ---------->
<#list table.fields as field><#if field.keyFlag><#assign keyPropertyName="${field.propertyName}"/></#if><#if field.comment!?length gt 0><#if swagger2>@ApiModelProperty(value = "${field.comment}")<#else>/*** ${field.comment}*/</#if></#if><#if field.keyFlag><#-- 主键 --><#if field.keyIdentityFlag>@TableId(value = "${field.annotationColumnName}", type = IdType.AUTO)<#elseif idType??>@TableId(value = "${field.annotationColumnName}", type = IdType.${idType})<#elseif field.convert>@TableId("${field.annotationColumnName}")</#if><#-- 普通字段 --><#elseif field.fill??><#-- -----   存在字段填充设置   -----><#if field.convert>@TableField(value = "${field.annotationColumnName}", fill = FieldFill.${field.fill})<#else>@TableField(fill = FieldFill.${field.fill})</#if><#elseif field.convert>@TableField("${field.annotationColumnName}")</#if><#-- 乐观锁注解 --><#if (versionFieldName!"") == field.name>@Version</#if><#-- 逻辑删除注解 --><#if (logicDeleteFieldName!"") == field.name>@TableLogic</#if>private ${field.propertyType} ${field.propertyName};
</#list>
<#------------  END 字段循环遍历  ----------><#if !entityLombokModel><#list table.fields as field><#if field.propertyType == "boolean"><#assign getprefix="is"/><#else><#assign getprefix="get"/></#if>public ${field.propertyType} ${getprefix}${field.capitalName}() {return ${field.propertyName};}<#if chainModel>public ${entity} set${field.capitalName}(${field.propertyType} ${field.propertyName}) {<#else>public void set${field.capitalName}(${field.propertyType} ${field.propertyName}) {</#if>this.${field.propertyName} = ${field.propertyName};<#if chainModel>return this;</#if>}</#list>
</#if><#if entityColumnConstant><#list table.fields as field>public static final String ${field.name?upper_case} = "${field.name}";</#list>
</#if>
<#if activeRecord>@Overrideprotected Serializable pkVal() {<#if keyPropertyName??>return this.${keyPropertyName};<#else>return null;</#if>}</#if>
<#if !entityLombokModel>@Overridepublic String toString() {return "${entity}{" +<#list table.fields as field><#if field_index==0>"${field.propertyName}=" + ${field.propertyName} +<#else>", ${field.propertyName}=" + ${field.propertyName} +</#if></#list>"}";}
</#if>
}
  • mapper2.java.ftl
package ${package.Mapper};import ${package.Entity}.${entity};
import ${superMapperClassPackage};/*** <p>* ${table.comment!} Mapper 接口* </p>** @author ${author}* @since ${date}*/
<#if kotlin>
interface ${table.mapperName} : ${superMapperClass}<${entity}>
<#else>
public interface ${table.mapperName} extends ${superMapperClass}<${entity}> {}
</#if>
  • mapper2.xml.ftl
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${package.Mapper}.${table.mapperName}"><#if enableCache><!-- 开启二级缓存 --><cache type="org.mybatis.caches.ehcache.LoggingEhcache"/></#if>
<#if baseResultMap><!-- 通用查询映射结果 --><resultMap id="BaseResultMap" type="${package.Entity}.${entity}">
<#list table.fields as field>
<#if field.keyFlag><#--生成主键排在第一位--><id column="${field.name}" property="${field.propertyName}" />
</#if>
</#list>
<#list table.commonFields as field><#--生成公共字段 --><result column="${field.name}" property="${field.propertyName}" />
</#list>
<#list table.fields as field>
<#if !field.keyFlag><#--生成普通字段 --><result column="${field.name}" property="${field.propertyName}" />
</#if>
</#list></resultMap></#if>
<#if baseColumnList><!-- 通用查询结果列 --><sql id="Base_Column_List">
<#list table.commonFields as field>${field.columnName},
</#list>${table.fieldNames}</sql></#if>
</mapper>
  • service2.java.ftl
package ${package.Service};import ${package.Entity}.${entity};
import ${superServiceClassPackage};/*** <p>* ${table.comment!} 服务类* </p>** @author ${author}* @since ${date}*/
<#if kotlin>
interface ${table.serviceName} : ${superServiceClass}<${entity}>
<#else>
public interface ${table.serviceName} extends ${superServiceClass}<${entity}> {}
</#if>
  • serviceImpl2.java.ftl
package ${package.ServiceImpl};import ${package.Entity}.${entity};
import ${package.Mapper}.${table.mapperName};
import ${package.Service}.${table.serviceName};
import ${superServiceImplClassPackage};
import org.springframework.stereotype.Service;/*** <p>* ${table.comment!} 服务实现类* </p>** @author ${author}* @since ${date}*/
@Service
<#if kotlin>
open class ${table.serviceImplName} : ${superServiceImplClass}<${table.mapperName}, ${entity}>(), ${table.serviceName} {}
<#else>
public class ${table.serviceImplName} extends ${superServiceImplClass}<${table.mapperName}, ${entity}> implements ${table.serviceName} {}
</#if>
  • 代码生成后项目结构

7. 测试分页查询

  • 采用junit测试 
package com.jmh.mp;import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jmh.mp.mapper.UserMapper;
import com.jmh.mp.model.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest
class Mp01ApplicationTests {@Autowiredprivate UserMapper userMapper;@Testvoid contextLoads() {Page<User> userPage = userMapper.selectPage(new Page<User>().setCurrent(1)//当前页.setSize(10)//当前页大小, new QueryWrapper<User>());System.out.println("总数据量:" + userPage.getTotal());System.out.println("当前页码:" + userPage.getCurrent());System.out.println("当前页大小:" + userPage.getSize());userPage.getRecords().forEach(System.out::println);}}
  •  结果 


http://www.ppmy.cn/news/7333.html

相关文章

【JavaScript】24_数组去重

10、数组去重 方法一、 改变原数组 <script>const arr [1,2,1,3,2,2,4,5,5,6,7]//分别获取数组中的元素for(let i 0; i < arr.length ; i){//获取当前值后边的所有值for(let j i 1; i < arr.length; j){//判断两个数是否相等if(arr[i] arr[j]){//出现了重复…

阳了在家没事干?教大家用python在家做一个万能看视频软件,绝对正经啦~

嗨害大家好鸭&#xff01;我是小熊猫~ 我前阵子不小心阳了&#xff0c;该说不说&#xff0c;真挺难受的 我在家真的就是纸巾热水不离手&#xff0c; 楼下水果店老板娘还说我年轻人身体怎么这么差… 哼我阳过之后我就锻炼去&#xff01;&#xff01;&#xff01; 效果展示 有…

maven 继承和聚合的区别

maven 继承和聚合的区别 参考 https://cloud.tencent.com/developer/article/1397748 继承 目的&#xff1a;统一管理version版本&#xff0c;少写冗余代码。使用&#xff1a; 父类pom不写业务&#xff0c;只写 pom的jar包版本等信息&#xff0c;子类中使用 parent 标签&…

RedisTemplate操作redis

目录 Redis Repositories方式 a、启用 Repository 功能 b、注解需要缓存的实体 c、创建一个 Repository 接口 d、测试类中测试 Redis Repositories方式 Spring Data Redis 从 1.7 开始提供 Redis Repositories &#xff0c;可以无缝的转换并存储 domain objects&#xff0…

Python采集某网站m3u8内容,美女我来了~

前言 嗨喽&#xff0c;大家好呀~这里是爱看美女的茜茜呐 又到了学Python时刻~ 环境使用: Python 3.8 Pycharm 模块使用: import requests >>> pip install requests import re 正则表达式 解析数据 import json 安装python第三方模块: win R 输入 cmd 点击确…

一文读懂机器学习常用算法的基本概念和适用场景

引用一句英国统计学家George E. P. Box的名言&#xff1a;All models are wrong, but some are useful. 没有哪一种算法能够适用所有情况&#xff0c;只有针对某一种问题更有用的算法。 机器学习算法不会要求一个问题被 100%求解&#xff0c;取而代之的是把问题转化为最优化的…

C# XML基本操作

一 XML的内容 二 XML的处理方式 1 DOM 文档对象模型&#xff08;Document Object Model&#xff09; 2 SAX XML解析的简单API&#xff08;Simple API for XML&#xff09; 3 .NET提供了XML支持&#xff1a;System.XML名称空间 三 常用的XML类 1 XmlDocument .LoadXml()…

【MySQL基础教程】函数的介绍与使用

前言 本文为 【MySQL基础教程】函数的介绍与使用 相关知识&#xff0c;下边具体将对字符串函数&#xff0c;数值函数&#xff0c;日期函数&#xff0c;流程函数等进行详尽介绍~ &#x1f4cc;博主主页&#xff1a;小新要变强 的主页 &#x1f449;Java全栈学习路线可参考&…