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

目录

1. 添加pom.xml依赖

2. 配置application.yml文件

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

4. 启动类配置

5. 测试数据Sql脚本

6. mybatis-plus代码生成

7. 测试分页查询


1. 添加pom.xml依赖

 
        
            com.baomidou
            mybatis-plus-boot-starter
            3.4.0
        
        
        
            com.baomidou
            mybatis-plus-generator
            3.4.0
        
        
        
            org.springframework.boot
            spring-boot-starter-freemarker
            2.7.4
        

        
            org.springframework.boot
            spring-boot-starter-jdbc
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            mysql
            mysql-connector-java
            5.1.44
            runtime
        
        
            org.projectlombok
            lombok
            true
        

2. 配置application.yml文件

#配置tomcat
server:
  port: 8080

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/zmall?useUnicode=true&characterEncoding=UTF-8&useSSL=false
    username: root
    password: 1234
    type: com.zaxxer.hikari.HikariDataSource
  freemarker:
    #设置编码格式
    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 避免缓存出现问题(该属性会在旧插件移除后一同移除)
     */
    @Bean
    public 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 Transfer

 Source Server         : localhost
 Source Server Type    : MySQL
 Source Server Version : 50736
 Source Host           : localhost:3306
 Source Schema         : zmall

 Target Server Type    : MySQL
 Target Server Version : 50736
 File Encoding         : 65001

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

    /**
     * 

* 读取控制台内容 *

*/ 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); //设置时间类型为Date gc.setDateType(DateType.TIME_PACK); gc.setOpen(false); //设置Mapper.xml的BaseColumnList gc.setBaseColumnList(true); //设置Mapper.xml的BaseResultMap gc.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() { @Override public void initMap() { // to do nothing } }; // 如果模板引擎是 freemarker String templatePath = "/templates/mybatis-generator/mapper2.xml.ftl"; // 如果模板引擎是 velocity // String templatePath = "/templates/mapper.xml.vm"; // 自定义输出配置 List focList = new ArrayList<>(); // 自定义配置会被优先输出 focList.add(new FileOutConfig(templatePath) { @Override public 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 superControllerClassPackage??>
import ${superControllerClassPackage};


/**
 * 

* ${table.comment!} 前端控制器 *

* * @author ${author} * @since ${date} */ <#if restControllerStyle> @RestController <#else> @Controller @RequestMapping("<#if package.ModuleName?? && package.ModuleName != "">/${package.ModuleName}/<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}") <#if kotlin> class ${table.controllerName}<#if superControllerClass??> : ${superControllerClass}() <#else> <#if superControllerClass??> public class ${table.controllerName} extends ${superControllerClass} { <#else> public class ${table.controllerName} { }
  • entity2.java.ftl
package ${package.Entity};

<#list table.importPackages as pkg>
import ${pkg};

<#if swagger2>
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

<#if entityLombokModel>
import lombok.Data;
import lombok.EqualsAndHashCode;
    <#if chainModel>
import lombok.experimental.Accessors;
    


/**
 * 

* ${table.comment!} *

* * @author ${author} * @since ${date} */ <#if entityLombokModel> @Data <#if superEntityClass??> @EqualsAndHashCode(callSuper = true) <#else> @EqualsAndHashCode(callSuper = false) <#if chainModel> @Accessors(chain = true) <#if table.convert> @TableName("${table.name}") <#if swagger2> @ApiModel(value="${entity}对象", description="${table.comment!}") <#if superEntityClass??> public class ${entity} extends ${superEntityClass}<#if activeRecord><${entity}> { <#elseif activeRecord> public class ${entity} extends Model<${entity}> { <#else> public class ${entity} implements Serializable { <#if entitySerialVersionUID> private static final long serialVersionUID = 1L; <#-- ---------- BEGIN 字段循环遍历 ----------> <#list table.fields as field> <#if field.keyFlag> <#assign keyPropertyName="${field.propertyName}"/> <#if field.comment!?length gt 0> <#if swagger2> @ApiModelProperty(value = "${field.comment}") <#else> /** * ${field.comment} */ <#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}") <#-- 普通字段 --> <#elseif field.fill??> <#-- ----- 存在字段填充设置 -----> <#if field.convert> @TableField(value = "${field.annotationColumnName}", fill = FieldFill.${field.fill}) <#else> @TableField(fill = FieldFill.${field.fill}) <#elseif field.convert> @TableField("${field.annotationColumnName}") <#-- 乐观锁注解 --> <#if (versionFieldName!"") == field.name> @Version <#-- 逻辑删除注解 --> <#if (logicDeleteFieldName!"") == field.name> @TableLogic private ${field.propertyType} ${field.propertyName}; <#------------ END 字段循环遍历 ----------> <#if !entityLombokModel> <#list table.fields as field> <#if field.propertyType == "boolean"> <#assign getprefix="is"/> <#else> <#assign getprefix="get"/> 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}) { this.${field.propertyName} = ${field.propertyName}; <#if chainModel> return this; } <#if entityColumnConstant> <#list table.fields as field> public static final String ${field.name?upper_case} = "${field.name}"; <#if activeRecord> @Override protected Serializable pkVal() { <#if keyPropertyName??> return this.${keyPropertyName}; <#else> return null; } <#if !entityLombokModel> @Override public String toString() { return "${entity}{" + <#list table.fields as field> <#if field_index==0> "${field.propertyName}=" + ${field.propertyName} + <#else> ", ${field.propertyName}=" + ${field.propertyName} + "}"; } }
  • mapper2.java.ftl
package ${package.Mapper};

import ${package.Entity}.${entity};
import ${superMapperClassPackage};

/**
 * 

* ${table.comment!} Mapper 接口 *

* * @author ${author} * @since ${date} */ <#if kotlin> interface ${table.mapperName} : ${superMapperClass}<${entity}> <#else> public interface ${table.mapperName} extends ${superMapperClass}<${entity}> { }
  • mapper2.xml.ftl




<#if enableCache>
    
    


<#if baseResultMap>
    
    
<#list table.fields as field>
<#if field.keyFlag><#--生成主键排在第一位-->
        


<#list table.commonFields as field><#--生成公共字段 -->
        

<#list table.fields as field>
<#if !field.keyFlag><#--生成普通字段 -->
        


    


<#if baseColumnList>
    
    
<#list table.commonFields as field>
        ${field.columnName},

        ${table.fieldNames}
    



  • service2.java.ftl
package ${package.Service};

import ${package.Entity}.${entity};
import ${superServiceClassPackage};

/**
 * 

* ${table.comment!} 服务类 *

* * @author ${author} * @since ${date} */ <#if kotlin> interface ${table.serviceName} : ${superServiceClass}<${entity}> <#else> public interface ${table.serviceName} extends ${superServiceClass}<${entity}> { }
  • 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;

/**
 * 

* ${table.comment!} 服务实现类 *

* * @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} { }
  • 代码生成后项目结构

SpringBoot 整合【Mybatis-Plus实现分页查询】_第1张图片

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 {

    @Autowired
    private UserMapper userMapper;


    @Test
    void contextLoads() {

        Page userPage = userMapper.selectPage(new Page()
                        .setCurrent(1)//当前页
                        .setSize(10)//当前页大小
                , new QueryWrapper());
        System.out.println("总数据量:" + userPage.getTotal());
        System.out.println("当前页码:" + userPage.getCurrent());
        System.out.println("当前页大小:" + userPage.getSize());
        userPage.getRecords().forEach(System.out::println);


    }

}
  •  结果 

SpringBoot 整合【Mybatis-Plus实现分页查询】_第2张图片

你可能感兴趣的:(mybatis,spring,boot,java)