微借阅小程序开发实战(二)-Springboot+MybatisPlus提供后端api服务

application.yml配置

#配置数据源
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: abc123_
    url: jdbc:mysql://127.0.0.1:3306/book?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false&allowMultiQueries=true
    druid:
      initial-size: 5  # 初始化大小
      min-idle: 5  # 最小
      max-active: 100  # 最大
      max-wait: 60000  # 配置获取连接等待超时的时间
      time-between-eviction-runs-millis: 60000  # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
      min-evictable-idle-time-millis: 300000  # 指定一个空闲连接最少空闲多久后可被清除,单位是毫秒
      validationQuery: select 'x'
      test-while-idle: true  # 当连接空闲时,是否执行连接测试
      test-on-borrow: false  # 当从连接池借用连接时,是否测试该连接
      test-on-return: false  # 在连接归还到连接池时是否测试该连接
      filters: config,wall,stat  # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
      poolPreparedStatements: true # 打开PSCache,并且指定每个连接上PSCache的大小
      maxPoolPreparedStatementPerConnectionSize: 20
      maxOpenPreparedStatements: 20
      # 通过connectProperties属性来打开mergeSql功能;慢SQL记录
      connectionProperties: druid.stat.slowSqlMillis=200;druid.stat.logSlowSql=true;config.decrypt=false
#配置日志级别
logging:
  level:
    com.augmentum.book.mapper : debug
#mybatis plusxml路径配置
mybatis-plus:
  # MyBatis 配置文件位置,如果您有单独的 MyBatis 配置,请将其路径配置到 configLocation 中。
  # config-location: classpath:mybatis-config.xml
  # MyBatis Mapper 所对应的 XML 文件位置,如果您在 Mapper 中有自定义方法
  mapper-locations: classpath:mapper/**/*.xml
  # MyBaits 别名包扫描路径,通过该属性可以给包中的类注册别名 实体扫描,多个package用逗号或者分号分隔
  type-aliases-package: com.augmentum.book.entity
  # 启动时是否检查 MyBatis XML 文件的存在,默认不检查
  check-config-location: false
  #  ExecutorType.SIMPLE:该执行器类型不做特殊的事情,为每个语句的执行创建一个新的预处理语句(PreparedStatement)
  #  ExecutorType.REUSE:该执行器类型会复用预处理语句(PreparedStatement)
  #  ExecutorType.BATCH:该执行器类型会批量执行所有的更新语句
  executor-type: simple
  configuration:
    # 是否开启自动驼峰命名规则(camel case)映射
    map-underscore-to-camel-case: true
    #配置JdbcTypeForNull, oracle数据库必须配置
    jdbc-type-for-null: null
  global-config:
    #驼峰下划线转换
    column-underline: true
    db-config:
      #      数据库类型,默认值为未知的数据库类型
      #      db-type: mysql
      logic-delete-value: 1
      logic-not-delete-value: 0
    banner: false

  # 阿里云OOS上传
aliyun:
  oss:
    access-id:   LTAI4FxemFp5s5WYXoCDBAR4
    access-key:  mJQ3oF4nee9MTDUrjz11qsWAfgUQJJ
    bucket:      randyyang-book
    endpoint:    http://oss-cn-hangzhou.aliyuncs.com
    dir:         image/
wx:
  code:
    url: https://api.weixin.qq.com/sns/jscode2session
    appid: wxf4ff430e34a562c0
    secret: e7838750f18161c78928dc6fed509c1e
    grant_type: authorization_code

pom.xml

为了简易将展示全部依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.augmentum</groupId>
    <artifactId>book</artifactId>
    <version>1.0</version>
    <name>book</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!--进本启动-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <!--基本测试-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--web服务基本依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--freemarker引擎依赖(mybatis plus配置模板引擎,用themleaf也行)-->
        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.28</version>
        </dependency>
        <!--lombok,简化实体类,mybatis plus和它很搭配-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!--mysql数据库连接依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <!--驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>4.6.5</version>
        </dependency>
        <!--*****下面是mybatis plus重要依赖*****-->
        <!--mybatis-plus依赖-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.1.2</version>
        </dependency>
        <!--添加代码生成器依赖-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.1.2</version>
        </dependency>
        <!--热部署-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-core</artifactId>
            <version>3.2.12.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.auth0</groupId>
            <artifactId>java-jwt</artifactId>
            <version>3.4.0</version>
        </dependency>
        <dependency>
            <groupId>com.aliyun.oss</groupId>
            <artifactId>aliyun-sdk-oss</artifactId>
            <version>3.8.0</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.32</version>
        </dependency>
        <dependency>
            <groupId>com.xiaoleilu</groupId>
            <artifactId>hutool-all</artifactId>
            <version>3.0.5</version>
        </dependency>
        <!--腾讯云验证码票据验证依赖-->
        <!-- https://mvnrepository.com/artifact/com.tencentcloudapi/tencentcloud-sdk-java -->
        <!-- https://mvnrepository.com/artifact/com.tencentcloudapi/tencentcloud-sdk-java -->
        <dependency>
            <groupId>com.tencentcloudapi</groupId>
            <artifactId>tencentcloud-sdk-java</artifactId>
            <version>3.1.8</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

分页插件MybatisPlusConfigurer.java

package com.augmentum.book.config;

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@MapperScan("com.augmentum.book.mapper")
public class MybatisPlusConfigurer {
    /**
     * 分页插件
     *
     * @return PaginationInterceptor
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }

}

更多使用及api源码查看

https://gitee.com/JasonLee1286791087/book/tree/master/book-backend

你可能感兴趣的:(Java,mysql,java)