Mybatis Plus+Druid环境搭建+基本使用

示例demo项目结构

mybatis plus官网地址:MyBatis-Plus,Druid官网Apache Druid | Apache® Druid

该示例demo可以直接使用,方便大家直接搭建应用

0.数据准备

DROP TABLE IF EXISTS `t_entranceguard_loginfo`;
CREATE TABLE `t_entranceguard_loginfo`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `ip_address` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT 'ip地址',
  `door_id` int(10) DEFAULT NULL COMMENT '门编号',
  `device_key` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '设备序列号',
  `req_seq_no` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '记录流水号,整型数据',
  `log_type` int(10) DEFAULT NULL COMMENT '记录类型(0:正常记录,1:非法用户记录 2:系统和报警记录)',
  `card_sn` varchar(10) CHARACTER SET latin1 COLLATE latin1_swedish_ci DEFAULT NULL COMMENT '卡号',
  `pass_time` datetime(0) DEFAULT NULL COMMENT '记录时间: 时间格式:yyyy-MM-dd HH:mm:ss',
  `operation` int(10) DEFAULT NULL COMMENT '通行方式,参考属性,记录通行方式',
  `alarm_code` int(10) DEFAULT NULL COMMENT '报警状态, ,参考属性,记录报警代码',
  `pass_status` int(10) DEFAULT NULL COMMENT '通行状态, 0通过 1,不通过',
  `read_id` int(10) DEFAULT NULL COMMENT '读头编号',
  `instance_id` bigint(20) DEFAULT NULL COMMENT '门禁设备实例id',
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `instance_id_index`(`instance_id`) USING BTREE
) 
-- ----------------------------
-- Records of t_entranceguard_loginfo
-- ----------------------------
INSERT INTO `t_entranceguard_loginfo` VALUES (1, '192.168.80.13', 1, 'DE6180388B486E24', '167777223467880100', 0, '485233809', '2023-03-02 15:50:34', 0, 0, 0, 2, 6295);
INSERT INTO `t_entranceguard_loginfo` VALUES (2, '192.168.80.13', 1, 'DE6180388B486E24', '167777223667880101', 0, '485233809', '2023-03-02 15:50:36', 0, 0, 0, 2, 6295);
INSERT INTO `t_entranceguard_loginfo` VALUES (3, '192.168.80.13', 1, 'DE6180388B486E24', '167777224067880102', 0, '485233809', '2023-03-02 15:50:40', 0, 0, 0, 2, 6295);
INSERT INTO `t_entranceguard_loginfo` VALUES (4, '192.168.80.13', 2, 'DE6180388B486E24', '167777225867880103', 0, '485233809', '2023-03-02 15:50:58', 0, 0, 0, 4, 6296);
INSERT INTO `t_entranceguard_loginfo` VALUES (5, '192.168.80.13', 2, 'DE6180388B486E24', '167777227367880104', 0, '485233809', '2023-03-02 15:51:13', 0, 0, 0, 4, 6296);
INSERT INTO `t_entranceguard_loginfo` VALUES (6, '192.168.80.13', 1, 'DE6180388B486E24', '167968599267880100', 0, '536635315', '2023-03-24 19:26:32', 0, 0, 0, 2, 6295);
INSERT INTO `t_entranceguard_loginfo` VALUES (7, '192.168.80.13', 2, 'DE6180388B486E24', '167968600967880101', 0, '536635315', '2023-03-24 19:26:49', 0, 0, 0, 4, 6296);
INSERT INTO `t_entranceguard_loginfo` VALUES (8, '192.168.80.13', 2, 'DE6180388B486E24', '167972949567880102', 0, '607938609', '2023-03-25 07:31:35', 0, 0, 0, 4, 6296);
INSERT INTO `t_entranceguard_loginfo` VALUES (9, '192.168.80.13', 2, 'DE6180388B486E24', '167992702167880103', 0, '0', '2023-03-27 14:23:41', 6, 80, 1, 4, 6296);
INSERT INTO `t_entranceguard_loginfo` VALUES (10, '192.168.80.13', 2, 'DE6180388B486E24', '168010101067880104', 0, '482665073', '2023-03-29 14:43:30', 0, 0, 0, 4, 6296);
INSERT INTO `t_entranceguard_loginfo` VALUES (11, '192.168.80.13', 1, 'DE6180388B486E24', '168010104867880105', 0, '482665073', '2023-03-29 14:44:08', 0, 0, 0, 2, 6295);
INSERT INTO `t_entranceguard_loginfo` VALUES (12, '192.168.80.13', 1, 'DE6180388B486E24', '168025517067880100', 1, '0', '2023-03-31 09:32:50', 32, 0, 0, 0, 6295);
INSERT INTO `t_entranceguard_loginfo` VALUES (13, '192.168.80.13', 2, 'DE6180388B486E24', '168025517067880101', 1, '0', '2023-03-31 09:32:50', 32, 0, 0, 0, 6296);
INSERT INTO `t_entranceguard_loginfo` VALUES (14, '192.168.80.13', 1, 'DE6180388B486E24', '168025518967880102', 1, '0', '2023-03-31 09:33:09', 32, 0, 0, 0, 6295);
INSERT INTO `t_entranceguard_loginfo` VALUES (15, '192.168.80.13', 2, 'DE6180388B486E24', '168025518967880103', 1, '0', '2023-03-31 09:33:09', 32, 0, 0, 0, 6296);
INSERT INTO `t_entranceguard_loginfo` VALUES (16, '192.168.80.13', 1, 'DE6180388B486E24', '168025520667880104', 1, '0', '2023-03-31 09:33:26', 32, 0, 0, 0, 6295);
INSERT INTO `t_entranceguard_loginfo` VALUES (17, '192.168.80.13', 2, 'DE6180388B486E24', '168025520667880105', 1, '0', '2023-03-31 09:33:26', 32, 0, 0, 0, 6296);
INSERT INTO `t_entranceguard_loginfo` VALUES (18, '192.168.80.13', 1, 'DE6180388B486E24', '168027523467880106', 1, '0', '2023-03-31 15:07:14', 32, 0, 0, 0, 6295);
INSERT INTO `t_entranceguard_loginfo` VALUES (19, '192.168.80.13', 2, 'DE6180388B486E24', '168027523467880107', 1, '0', '2023-03-31 15:07:14', 32, 0, 0, 0, 6296);
INSERT INTO `t_entranceguard_loginfo` VALUES (20, '192.168.80.13', 1, 'DE6180388B486E24', '168027624367880108', 1, '0', '2023-03-31 15:24:03', 32, 0, 0, 0, 6295);
INSERT INTO `t_entranceguard_loginfo` VALUES (21, '192.168.80.13', 2, 'DE6180388B486E24', '168027624367880109', 1, '0', '2023-03-31 15:24:03', 32, 0, 0, 0, 6296);
INSERT INTO `t_entranceguard_loginfo` VALUES (22, '192.168.80.13', 1, 'DE6180388B486E24', '168027627067880110', 1, '0', '2023-03-31 15:24:30', 32, 0, 0, 0, 6295);
INSERT INTO `t_entranceguard_loginfo` VALUES (23, '192.168.80.13', 2, 'DE6180388B486E24', '168027627067880111', 1, '0', '2023-03-31 15:24:30', 32, 0, 0, 0, 6296);
INSERT INTO `t_entranceguard_loginfo` VALUES (24, '192.168.80.13', 1, 'DE6180388B486E24', '168053795267880100', 0, '609863985', '2023-04-03 16:05:52', 0, 0, 0, 2, 6295);
INSERT INTO `t_entranceguard_loginfo` VALUES (25, '192.168.80.13', 2, 'DE6180388B486E24', '168053798767880101', 0, '609863985', '2023-04-03 16:06:27', 0, 0, 0, 4, 6296);
INSERT INTO `t_entranceguard_loginfo` VALUES (26, '192.168.80.13', 2, 'DE6180388B486E24', '168053798967880102', 0, '609863985', '2023-04-03 16:06:29', 0, 0, 0, 4, 6296);
INSERT INTO `t_entranceguard_loginfo` VALUES (27, '192.168.80.13', 2, 'DE6180388B486E24', '168054194267880103', 0, '609863985', '2023-04-03 17:12:22', 0, 0, 0, 4, 6296);
INSERT INTO `t_entranceguard_loginfo` VALUES (28, '192.168.80.13', 1, 'DE6180388B486E24', '168054195767880104', 0, '609863985', '2023-04-03 17:12:37', 0, 0, 0, 2, 6295);
INSERT INTO `t_entranceguard_loginfo` VALUES (29, '192.168.80.13', 2, 'DE6180388B486E24', '168062163167880105', 0, '485233809', '2023-04-04 15:20:31', 6, 0, 0, 4, 6296);
INSERT INTO `t_entranceguard_loginfo` VALUES (30, '192.168.80.13', 2, 'DE6180388B486E24', '168062163967880106', 0, '485233809', '2023-04-04 15:20:39', 6, 0, 0, 4, 6296);
INSERT INTO `t_entranceguard_loginfo` VALUES (31, '192.168.80.13', 1, 'DE6180388B486E24', '168062186367880107', 0, '485233809', '2023-04-04 15:24:23', 6, 0, 0, 2, 6295);
INSERT INTO `t_entranceguard_loginfo` VALUES (32, '192.168.80.13', 2, 'DE6180388B486E24', '168069042967880108', 0, '485233809', '2023-04-05 10:27:09', 6, 0, 0, 4, 6296);
INSERT INTO `t_entranceguard_loginfo` VALUES (33, '192.168.80.13', 1, 'DE6180388B486E24', '168069104467880109', 1, '0', '2023-04-05 10:37:24', 32, 0, 0, 0, 6295);
INSERT INTO `t_entranceguard_loginfo` VALUES (34, '192.168.80.13', 2, 'DE6180388B486E24', '168069104467880110', 1, '0', '2023-04-05 10:37:24', 32, 0, 0, 0, 6296);
INSERT INTO `t_entranceguard_loginfo` VALUES (35, '192.168.80.13', 1, 'DE6180388B486E24', '168219618967880111', 1, '0', '2023-04-22 20:43:09', 32, 0, 0, 0, 6295);
INSERT INTO `t_entranceguard_loginfo` VALUES (36, '192.168.80.13', 2, 'DE6180388B486E24', '168219618967880112', 1, '0', '2023-04-22 20:43:09', 32, 0, 0, 0, 6296);
INSERT INTO `t_entranceguard_loginfo` VALUES (37, '192.168.80.13', 1, 'DE6180388B486E24', '168284083867880113', 0, '3958833', '2023-04-30 07:47:18', 0, 80, 1, 2, 6295);
INSERT INTO `t_entranceguard_loginfo` VALUES (38, '192.168.80.13', 1, 'DE6180388B486E24', '168284084067880114', 0, '3958833', '2023-04-30 07:47:20', 0, 80, 1, 2, 6295);
INSERT INTO `t_entranceguard_loginfo` VALUES (39, '192.168.80.13', 1, 'DE6180388B486E24', '168284084467880115', 0, '3958833', '2023-04-30 07:47:24', 0, 80, 1, 2, 6295);
INSERT INTO `t_entranceguard_loginfo` VALUES (40, '192.168.80.13', 1, 'DE6180388B486E24', '168284085967880116', 0, '0', '2023-04-30 07:47:39', 6, 80, 1, 2, 6295);
INSERT INTO `t_entranceguard_loginfo` VALUES (41, '192.168.80.13', 1, 'DE6180388B486E24', '168284090067880117', 0, '3958833', '2023-04-30 07:48:20', 0, 80, 1, 2, 6295);
INSERT INTO `t_entranceguard_loginfo` VALUES (42, '192.168.80.13', 1, 'DE6180388B486E24', '168284092167880118', 0, '3958833', '2023-04-30 07:48:41', 0, 80, 1, 2, 6295);
INSERT INTO `t_entranceguard_loginfo` VALUES (43, '192.168.80.13', 2, 'DE6180388B486E24', '168284361667880119', 0, '485233809', '2023-04-30 08:33:36', 6, 0, 0, 4, 6296);
INSERT INTO `t_entranceguard_loginfo` VALUES (44, '192.168.80.13', 1, 'DE6180388B486E24', '168337064767880120', 0, '485233809', '2023-05-06 10:57:27', 6, 0, 0, 2, 6295);
INSERT INTO `t_entranceguard_loginfo` VALUES (45, '192.168.80.13', 2, 'DE6180388B486E24', '168434947967880100', 0, '485233809', '2023-05-17 18:51:19', 6, 0, 0, 4, 6296);
INSERT INTO `t_entranceguard_loginfo` VALUES (46, '192.168.80.13', 2, 'DE6180388B486E24', '168502663667880100', 0, '482665073', '2023-05-25 14:57:16', 0, 0, 0, 4, 6296);
INSERT INTO `t_entranceguard_loginfo` VALUES (47, '192.168.80.13', 2, 'DE6180388B486E24', '168563084167880101', 0, '485233809', '2023-06-01 14:47:21', 6, 0, 0, 4, 6296);
INSERT INTO `t_entranceguard_loginfo` VALUES (48, '192.168.80.13', 1, 'DE6180388B486E24', '168616077867880102', 0, '485233809', '2023-06-07 17:59:38', 6, 0, 0, 2, 6295);
INSERT INTO `t_entranceguard_loginfo` VALUES (49, '192.168.80.13', 2, 'DE6180388B486E24', '168622836967880103', 0, '485233809', '2023-06-08 12:46:09', 6, 0, 0, 4, 6296);
INSERT INTO `t_entranceguard_loginfo` VALUES (50, '192.168.80.13', 1, 'DE6180388B486E24', '168632810367880104', 0, '485233809', '2023-06-09 16:28:23', 6, 0, 0, 2, 6295);
INSERT INTO `t_entranceguard_loginfo` VALUES (51, '192.168.80.13', 1, 'DE6180388B486E24', '168796060267880100', 0, '0', '2023-06-28 13:56:42', 6, 80, 1, 2, 6295);
INSERT INTO `t_entranceguard_loginfo` VALUES (52, '192.168.80.13', 1, 'DE6180388B486E24', '168823113867880101', 0, '485233809', '2023-07-01 17:05:38', 6, 0, 0, 2, 6295);
INSERT INTO `t_entranceguard_loginfo` VALUES (53, '192.168.80.13', 2, 'DE6180388B486E24', '168823118767880102', 0, '485233809', '2023-07-01 17:06:27', 6, 0, 0, 4, 6296);
INSERT INTO `t_entranceguard_loginfo` VALUES (54, '192.168.80.13', 2, 'DE6180388B486E24', '168823120367880103', 0, '485233809', '2023-07-01 17:06:43', 6, 0, 0, 4, 6296);
INSERT INTO `t_entranceguard_loginfo` VALUES (55, '192.168.80.13', 1, 'DE6180388B486E24', '168889054067880104', 1, '0', '2023-07-09 08:15:40', 32, 0, 0, 0, 6295);
INSERT INTO `t_entranceguard_loginfo` VALUES (56, '192.168.80.13', 2, 'DE6180388B486E24', '168889054067880105', 1, '0', '2023-07-09 08:15:40', 32, 0, 0, 0, 6296);
INSERT INTO `t_entranceguard_loginfo` VALUES (57, '192.168.80.13', 1, 'DE6180388B486E24', '168889054767880106', 1, '0', '2023-07-09 08:15:47', 32, 0, 0, 0, 6295);
INSERT INTO `t_entranceguard_loginfo` VALUES (58, '192.168.80.13', 2, 'DE6180388B486E24', '168889054767880107', 1, '0', '2023-07-09 08:15:47', 32, 0, 0, 0, 6296);
INSERT INTO `t_entranceguard_loginfo` VALUES (59, '192.168.80.13', 1, 'DE6180388B486E24', '168889055467880108', 1, '0', '2023-07-09 08:15:54', 32, 0, 0, 0, 6295);

1.导入依赖

mybatis plus的主要依赖

        <!--mysql数据库驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!--mybatis-plus-boot-starter-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.1</version>
        </dependency>

测试示例所有依赖

<?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>

    <groupId>com.diswdata</groupId>
    <artifactId>Knife4j-demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.0</version>
    </parent>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <!-- 支持web环境,不用写版本号 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--mysql数据库驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!--mybatis-plus-boot-starter-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.1</version>
        </dependency>
        <!-- Lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <!--    lang3工具包    -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.1</version>
        </dependency>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.8.16</version>
        </dependency>
        <!-- form数据验证依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <!--knife4j-->
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>knife4j-spring-boot-starter</artifactId>
            <version>2.0.7</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
            <exclusions>
                <exclusion>
                    <groupId>io.swagger</groupId>
                    <artifactId>swagger-models</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- 防止进入swagger页面报类型转换错误,排除3.0.0中的引用,手动增加1.6.2版本 -->
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-models</artifactId>
            <version>1.6.2</version>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>29.0-jre</version>
        </dependency>
    </dependencies>
</project>

2.yaml文件配置

server:
  port: 9988
spring:
  datasource:
    type: com.zaxxer.hikari.HikariDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=Asia/Shanghai&characterEncoding=utf-8&useSSL=false
    username: root
    password: root

#mybatis plus 设置
mybatis-plus:
  type-aliases-package: com.diswdata.demo.domain #扫描实体类所在的包
  mapper-locations: classpath*:com/diswdata/**/mapper/xml/*Mapper.xml
  global-config:
    # 关闭MP3.0自带的banner
    banner: false
    db-config:
      #主键类型  0:"数据库ID自增",1:"该类型为未设置主键类型", 2:"用户输入ID",3:"全局唯一ID (数字类型唯一ID)", 4:"全局唯一ID UUID",5:"字符串全局唯一ID (idWorker 的字符串表示)";
      id-type: auto
      # 默认数据库表下划线命名
      table-underline: true
# Swagger配置
swagger:
  # 是否开启swagger
  enabled: true
  # 请求前缀
  pathMapping: /

3.启动类

@SpringBootApplication
@MapperScan("com.diswdata.demo.mapper")//扫描包路径
public class DiswMuseumApp {
    @Autowired
    public static void main(String[] args) {
        SpringApplication.run(DiswMuseumApp.class, args);
    }
}

4.mybatis plus分页插件配置类

package com.diswdata.basic.config;

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.pagination.optimize.JsqlParserCountOptimize;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * mybatis-plus分页插件
 */
@Configuration
public class MyBatisPlusPageInterceptor {
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        //优化处理类
        paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
        return paginationInterceptor;
    }
}

5.控制层

package com.diswdata.demo.controller;

import com.diswdata.basic.vo.PageVO;
import com.diswdata.basic.vo.ResponseVO;
import com.diswdata.demo.service.IEntranceguardLogInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


/**
 * 控制层:门禁记录
 */
@RestController
@RequestMapping("/entranceguardLogInfo")
@Api(tags = "门禁控制器")
@CrossOrigin
public class EntranceguardLogInfoController {

    @Autowired
    private IEntranceguardLogInfoService entranceguardLogInfoService;

    @CrossOrigin
    @GetMapping("/page")
    @ApiOperation(value = "根据设备实例id分页查询门禁信息", notes = "根据设备实例id分页查询门禁信息")
    public ResponseVO page(@Valid PageVO page) {
        return ResponseVO.success(entranceguardLogInfoService.getLogInfoByInstanceId(page.page()));
    }

}

6.service层

接口

package com.diswdata.demo.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.diswdata.demo.domain.EntranceguardLogInfo;

public interface IEntranceguardLogInfoService extends IService<EntranceguardLogInfo> {


    /**
     * 根据门禁实例id获取门禁记录(分页)
     * @param page
     * @return
     */
    Page getLogInfoByInstanceId(Page page);

}

实现类

package com.diswdata.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.diswdata.demo.domain.EntranceguardLogInfo;
import com.diswdata.demo.mapper.EntranceguardLogInfoMapper;
import com.diswdata.demo.service.IEntranceguardLogInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class EntranceguardLogInfoImpl extends ServiceImpl<EntranceguardLogInfoMapper, EntranceguardLogInfo> implements IEntranceguardLogInfoService {
    @Autowired
    private EntranceguardLogInfoMapper entranceguardLogInfoMapper;


    @Override
    public Page getLogInfoByInstanceId(Page page) {
        QueryWrapper<EntranceguardLogInfo> qw = new QueryWrapper<>();
        return entranceguardLogInfoMapper.selectPage(page,qw);
    }
}

7.mapper层

接口

package com.diswdata.demo.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.diswdata.demo.domain.EntranceguardLogInfo;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author XiaoYi
 * @since 2023-08-15
 */
public interface EntranceguardLogInfoMapper extends BaseMapper<EntranceguardLogInfo> {

}

xml文件

<?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="com.diswdata.demo.mapper.EntranceguardLogInfoMapper">

</mapper>

8.实体类

package com.diswdata.demo.domain;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;

/**
 * 门禁日志记录对象
 */
@Data
@TableName("t_entranceguard_loginfo")
public class EntranceguardLogInfo {
    @TableId(type = IdType.AUTO)
    @ApiModelProperty(value = "主键")
    private Long id;

    @ApiModelProperty(value = "ip地址")
    private String ipAddress;

    @ApiModelProperty(value = "设备序列号")
    private String deviceKey;//FFFF002A2A435E98;//,//设备序列号

    @ApiModelProperty(value = "记录流水号")
    private String reqSeqNo;//:;//160025214642688858;//,//记录流水号,整型数据

    @ApiModelProperty(value = "记录类型")
    private Integer logType;//:0,//记录类型(0:正常记录,1:非法用户记录 2:系统和报警记录)

    @TableField(value = "card_sn")
    @ApiModelProperty(value = "卡号")
    private String cardSN;//:;//0;//,//卡号

    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @ApiModelProperty(value = "记录时间")
    private Date passTime;//:”2020-09-18 17:50:00”,//记录时间: 时间格式:yyyy-MM-dd HH:mm:ss

    @ApiModelProperty(value = "通行方式")
    private Integer operation;//:2,// 通行方式,参考属性,记录通行方式

    @ApiModelProperty(value = "报警状态")
    private Integer alarmCode;//:0,//报警状态, ,参考属性,记录报警代码

    @ApiModelProperty(value = "通行状态")
    private Integer passStatus;//:0,//通行状态, 0通过 1,不通过

    @ApiModelProperty(value = "门编号")
    private Integer doorId;//:1,//门编号

    @ApiModelProperty(value = "读头编号")
    private Integer readId;//:1,//读头编号

    @ApiModelProperty(value = "门禁设备实例id")
    private Long instanceId;//门禁设备实例id

    @ApiModelProperty(value = "门禁设备实例名称")
    private String instanceName;//门禁设备实例id


}

9.公用类

package com.diswdata.basic.config;

import com.diswdata.basic.vo.ResultCode;
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseBuilder;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static org.springframework.http.HttpMethod.*;

/**
 * Swagger2的接口配置
 */
@EnableSwagger2
@EnableKnife4j
@Configuration
public class SwaggerConfig {

    /**
     * 是否开启swagger
     */
    @Value("${swagger.enabled}")
    private boolean enabled;

    /**
     * 设置请求的统一前缀
     */
    @Value("${swagger.pathMapping}")
    private String pathMapping;

    /**
     * 创建API
     */
    @Bean
    public Docket createRestApi() {
        List<Response> responseList = new ArrayList<>();
        Arrays.stream(ResultCode.values()).forEach(resultCode -> {
            responseList.add(new ResponseBuilder().code(resultCode.getCode().toString()).description(resultCode.getMsg()).build());
        });

        return new Docket(DocumentationType.SWAGGER_2)
                // 是否启用Swagger
                .enable(enabled)
                // 用来创建该API的基本信息,展示在文档的页面中(自定义展示的信息)
                .apiInfo(apiInfo())
                // 设置哪些接口暴露给Swagger展示
                .select()
                // 扫描所有有注解的api,用这种方式更灵活
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                // 扫描指定包中的swagger注解
                // .apis(RequestHandlerSelectors.basePackage("com.ruoyi.project.tool.swagger"))
                // 扫描所有
//                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build()
                /* 设置安全模式,swagger可以设置访问token */
//                .securitySchemes(securitySchemes())
//                .securityContexts(securityContexts())
                .pathMapping(pathMapping)
                .globalResponses(GET, responseList)
                .globalResponses(POST, responseList)
                .globalResponses(PUT, responseList)
                .globalResponses(DELETE, responseList);
    }

    /**
     * 安全模式,这里指定token通过Authorization头请求头传递
     */
    private List<SecurityScheme> securitySchemes() {
        List<SecurityScheme> apiKeyList = new ArrayList<SecurityScheme>();
        apiKeyList.add(new ApiKey("Authorization", "易启勇", In.HEADER.toValue()));
        return apiKeyList;
    }

    /**
     * 安全上下文
     */
    private List<SecurityContext> securityContexts() {
        List<SecurityContext> securityContexts = new ArrayList<>();
        securityContexts.add(
                SecurityContext.builder()
                        .securityReferences(defaultAuth())
                        .operationSelector(o -> o.requestMappingPattern().matches("/.*"))
                        .build());
        return securityContexts;
    }

    /**
     * 默认的安全上引用
     */
    private List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        List<SecurityReference> securityReferences = new ArrayList<>();
        securityReferences.add(new SecurityReference("Authorization", authorizationScopes));
        return securityReferences;
    }

    /**
     * 添加摘要信息
     */
    private ApiInfo apiInfo() {
        // 用ApiInfoBuilder进行定制
        return new ApiInfoBuilder()
                // 设置标题
                .title("标题:基础管理系统接口文档")
                // 描述
                .description("描述:基础管理系统接口文档")
                // 作者信息
                .contact(new Contact("", null, null))
                // 版本
                .version("版本号: ")
                .build();
    }
}

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package com.diswdata.basic.vo;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;

@ApiModel("排序字段")
public class OrderItemVO implements Serializable {
    @ApiModelProperty("排序字段")
    private String column;
    @ApiModelProperty("排序")
    private Boolean asc;

    public OrderItemVO(final String column, final Boolean asc) {
        this.column = column;
        this.asc = asc;
    }

    public OrderItemVO() {
    }

    public String getColumn() {
        return this.column;
    }

    public Boolean getAsc() {
        return this.asc;
    }

    public void setColumn(final String column) {
        this.column = column;
    }

    public void setAsc(final Boolean asc) {
        this.asc = asc;
    }

    public boolean equals(final Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof OrderItemVO)) {
            return false;
        } else {
            OrderItemVO other = (OrderItemVO)o;
            if (!other.canEqual(this)) {
                return false;
            } else {
                Object this$asc = this.getAsc();
                Object other$asc = other.getAsc();
                if (this$asc == null) {
                    if (other$asc != null) {
                        return false;
                    }
                } else if (!this$asc.equals(other$asc)) {
                    return false;
                }

                Object this$column = this.getColumn();
                Object other$column = other.getColumn();
                if (this$column == null) {
                    if (other$column != null) {
                        return false;
                    }
                } else if (!this$column.equals(other$column)) {
                    return false;
                }

                return true;
            }
        }
    }

    protected boolean canEqual(final Object other) {
        return other instanceof OrderItemVO;
    }

    public int hashCode() {
        int result = 1;
        Object $asc = this.getAsc();
        result = result * 59 + ($asc == null ? 43 : $asc.hashCode());
        Object $column = this.getColumn();
        result = result * 59 + ($column == null ? 43 : $column.hashCode());
        return result;
    }

    public String toString() {
        return "OrderItemVO(column=" + this.getColumn() + ", asc=" + this.getAsc() + ")";
    }
}
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.diswdata.basic.vo;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.lang3.StringUtils;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.List;

@ApiModel(
        value = "分页DTO",
        description = "分页DTO"
)
public class PageVO implements Serializable {
    @ApiModelProperty(
            value = "当前页数",
            required = true,
            example = "1"
    )
    private @NotNull(
            message = "请输入当前页数"
    ) Integer curPage;
    @ApiModelProperty(
            value = "每页显示数量",
            required = true,
            example = "10"
    )
    private @NotNull(
            message = "请输入每页显示数量"
    ) Integer pageSize;
    @ApiModelProperty("排序字段")
    private @Valid List<OrderItemVO> orders;

    public Page page() {
        Page page = new Page((long)this.getCurPage(), (long)this.getPageSize());
        if (this.orders != null && this.orders.size() > 0) {
            List<OrderItem> list = Lists.newArrayList();
            this.orders.forEach((item) -> {
                if (StringUtils.isNotBlank(item.getColumn()) && item.getAsc() != null) {
                    OrderItem orderItem = new OrderItem();
                    orderItem.setColumn(item.getColumn());
                    orderItem.setAsc(item.getAsc());
                    list.add(orderItem);
                }

            });
            page.setOrders(list);
        }

        page.setOptimizeCountSql(false);
        return page;
    }

    public PageVO() {
    }

    public PageVO(final Integer curPage, final Integer pageSize, final List<OrderItemVO> orders) {
        this.curPage = curPage;
        this.pageSize = pageSize;
        this.orders = orders;
    }

    public Integer getCurPage() {
        return this.curPage;
    }

    public Integer getPageSize() {
        return this.pageSize;
    }

    public List<OrderItemVO> getOrders() {
        return this.orders;
    }

    public void setCurPage(final Integer curPage) {
        this.curPage = curPage;
    }

    public void setPageSize(final Integer pageSize) {
        this.pageSize = pageSize;
    }

    public void setOrders(final List<OrderItemVO> orders) {
        this.orders = orders;
    }

    public boolean equals(final Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof PageVO)) {
            return false;
        } else {
            PageVO other = (PageVO)o;
            if (!other.canEqual(this)) {
                return false;
            } else {
                label47: {
                    Object this$curPage = this.getCurPage();
                    Object other$curPage = other.getCurPage();
                    if (this$curPage == null) {
                        if (other$curPage == null) {
                            break label47;
                        }
                    } else if (this$curPage.equals(other$curPage)) {
                        break label47;
                    }

                    return false;
                }

                Object this$pageSize = this.getPageSize();
                Object other$pageSize = other.getPageSize();
                if (this$pageSize == null) {
                    if (other$pageSize != null) {
                        return false;
                    }
                } else if (!this$pageSize.equals(other$pageSize)) {
                    return false;
                }

                Object this$orders = this.getOrders();
                Object other$orders = other.getOrders();
                if (this$orders == null) {
                    if (other$orders != null) {
                        return false;
                    }
                } else if (!this$orders.equals(other$orders)) {
                    return false;
                }

                return true;
            }
        }
    }

    protected boolean canEqual(final Object other) {
        return other instanceof PageVO;
    }

    public int hashCode() {
        int result = 1;
        Object $curPage = this.getCurPage();
        result = result * 59 + ($curPage == null ? 43 : $curPage.hashCode());
        Object $pageSize = this.getPageSize();
        result = result * 59 + ($pageSize == null ? 43 : $pageSize.hashCode());
        Object $orders = this.getOrders();
        result = result * 59 + ($orders == null ? 43 : $orders.hashCode());
        return result;
    }

    public String toString() {
        return "PageVO(curPage=" + this.getCurPage() + ", pageSize=" + this.getPageSize() + ", orders=" + this.getOrders() + ")";
    }
}
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.diswdata.basic.vo;

import io.swagger.annotations.ApiModel;
import org.springframework.http.HttpStatus;

import java.io.Serializable;

@ApiModel("返回信息封装类")
public class ResponseVO<T> implements Serializable {
    private Integer code;
    private String msg;
    private T data;

    public ResponseVO(Integer code) {
        this.code = code;
    }

    public ResponseVO(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public ResponseVO(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static ResponseVO success() {
        return new ResponseVO(HttpStatus.OK.value());
    }

    public static ResponseVO success(String msg) {
        return new ResponseVO(HttpStatus.OK.value(), msg);
    }

    public static ResponseVO success(Object data) {
        return new ResponseVO(HttpStatus.OK.value(), "成功", data);
    }

    public static ResponseVO success(String msg, Object data) {
        return new ResponseVO(HttpStatus.OK.value(), msg, data);
    }

    public static ResponseVO fail() {
        return new ResponseVO(HttpStatus.INTERNAL_SERVER_ERROR.value());
    }

    public static ResponseVO fail(Object data) {
        return new ResponseVO(HttpStatus.INTERNAL_SERVER_ERROR.value(), (String)null, data);
    }

    public static ResponseVO fail(String msg) {
        return new ResponseVO(HttpStatus.INTERNAL_SERVER_ERROR.value(), msg);
    }

    public static ResponseVO fail(String msg, Object data) {
        return new ResponseVO(HttpStatus.INTERNAL_SERVER_ERROR.value(), msg, data);
    }

    public static ResponseVO fail(Integer code, String msg) {
        return new ResponseVO(code, msg);
    }

    public static ResponseVO tips(String msg) {
        return new ResponseVO(HttpStatus.BAD_REQUEST.value(), msg);
    }

    public static <T> ResponseVOBuilder<T> builder() {
        return new ResponseVOBuilder();
    }

    public ResponseVO() {
    }

    public Integer getCode() {
        return this.code;
    }

    public String getMsg() {
        return this.msg;
    }

    public T getData() {
        return this.data;
    }

    public void setCode(final Integer code) {
        this.code = code;
    }

    public void setMsg(final String msg) {
        this.msg = msg;
    }

    public void setData(final T data) {
        this.data = data;
    }

    public boolean equals(final Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof ResponseVO)) {
            return false;
        } else {
            ResponseVO<?> other = (ResponseVO)o;
            if (!other.canEqual(this)) {
                return false;
            } else {
                label47: {
                    Object this$code = this.getCode();
                    Object other$code = other.getCode();
                    if (this$code == null) {
                        if (other$code == null) {
                            break label47;
                        }
                    } else if (this$code.equals(other$code)) {
                        break label47;
                    }

                    return false;
                }

                Object this$msg = this.getMsg();
                Object other$msg = other.getMsg();
                if (this$msg == null) {
                    if (other$msg != null) {
                        return false;
                    }
                } else if (!this$msg.equals(other$msg)) {
                    return false;
                }

                Object this$data = this.getData();
                Object other$data = other.getData();
                if (this$data == null) {
                    if (other$data != null) {
                        return false;
                    }
                } else if (!this$data.equals(other$data)) {
                    return false;
                }

                return true;
            }
        }
    }

    protected boolean canEqual(final Object other) {
        return other instanceof ResponseVO;
    }

    public int hashCode() {
        int result = 1;
        Object $code = this.getCode();
        result = result * 59 + ($code == null ? 43 : $code.hashCode());
        Object $msg = this.getMsg();
        result = result * 59 + ($msg == null ? 43 : $msg.hashCode());
        Object $data = this.getData();
        result = result * 59 + ($data == null ? 43 : $data.hashCode());
        return result;
    }

    public String toString() {
        return "ResponseVO(code=" + this.getCode() + ", msg=" + this.getMsg() + ", data=" + this.getData() + ")";
    }

    public static class ResponseVOBuilder<T> {
        private Integer code;
        private String msg;
        private T data;

        ResponseVOBuilder() {
        }

        public ResponseVOBuilder<T> code(final Integer code) {
            this.code = code;
            return this;
        }

        public ResponseVOBuilder<T> msg(final String msg) {
            this.msg = msg;
            return this;
        }

        public ResponseVOBuilder<T> data(final T data) {
            this.data = data;
            return this;
        }

        public ResponseVO<T> build() {
            return new ResponseVO(this.code, this.msg, this.data);
        }

        public String toString() {
            return "ResponseVO.ResponseVOBuilder(code=" + this.code + ", msg=" + this.msg + ", data=" + this.data + ")";
        }
    }
}
package com.diswdata.basic.vo;

import lombok.*;

/**
 * @className: ResultCode
 * @author: houqd
 * @date: 2022/11/1
 **/
@Getter
@AllArgsConstructor
@NoArgsConstructor
public enum ResultCode {
    SUCCESS(1,"成功"),
    ERROR(0,"失败"),
    SERVER_ERROR(500,"服务器错误"),
    UNLAWFUL(401,"无权限"),
    PARAM_ERROR(405,"参数错误"),
    NOT_LOGIN(406,"未登录");
    private Integer code;
    private String msg;
}

10.运行效果

11.搭配Druid连接池来使用

1.pom.xml依赖

<!-- druid连接池(德鲁伊-阿里巴巴的)-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>

2.yaml配置

server:
  port: 9988
spring:
  datasource:
    type: com.zaxxer.hikari.HikariDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=Asia/Shanghai&characterEncoding=utf-8&useSSL=false
    username: root
    password: root
#使用druid连接池只需要加入以下配置
  druid: #企业开发中可能会用到的配置
    initial-size: 5 # 初始化时建立物理连接的个数
    min-idle: 5 # 最小连接池连接数量,最小空闲数量
    max-active: 20 # 最大连接池连接数量,最大活跃连接数
    max-wait: 60000 # 配置获取连接等待超时的时间
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 1
    testWhileIdle: true
    testOnBorrow: true
    testOnReturn: false
    poolPreparedStatements: true
    maxPoolPreparedStatementPerConnectionSize: 20
    filters: stat,wall
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
    stat-view-servlet: # 德鲁伊连接池,内置提供一个web版的性能监控配置
      allow: 0.0.0.0 # 允许哪些IP访问druid监控界面,多个IP以逗号分隔
      login-username: admin # 设置登录帐号
      login-password: 123456 # 设置登录密码
      reset-enable: false # 是否允许重置数据
      # url-pattern: /database/* # 默认访问根路径是:/druid/;也可以自定义设置

#mybatis plus 设置
mybatis-plus:
  type-aliases-package: com.diswdata.demo.domain #扫描实体类所在的包
  mapper-locations: classpath*:com/diswdata/**/mapper/xml/*Mapper.xml
  global-config:
    # 关闭MP3.0自带的banner
    banner: false
    db-config:
      #主键类型  0:"数据库ID自增",1:"该类型为未设置主键类型", 2:"用户输入ID",3:"全局唯一ID (数字类型唯一ID)", 4:"全局唯一ID UUID",5:"字符串全局唯一ID (idWorker 的字符串表示)";
      id-type: auto
      # 默认数据库表下划线命名
      table-underline: true
# Swagger配置
swagger:
  # 是否开启swagger
  enabled: true
  # 请求前缀
  pathMapping: /

12.再次运行

knife4j展示效果

Druid监控端展示效果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值