欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

springboot+vue前后端分离项目(三)后端项目搭建及配置

程序员文章站 2022-06-05 09:19:04
...

springboot+vue前后端分离项目(三)后端项目搭建及配置

1. 项目创建

springboot+vue前后端分离项目(三)后端项目搭建及配置

2. 配置pom文件

添加依赖

 <dependency>
     <groupId>com.alibaba</groupId>
     <artifactId>druid-spring-boot-starter</artifactId>
     <version>1.1.10</version>
 </dependency>
 <dependency>
     <groupId>org.projectlombok</groupId>
     <artifactId>lombok</artifactId>
     <version>1.18.12</version>
 </dependency>

3. 配置application.yml文件

server:
  port: 8090
  servlet:
    context-path: /vhr
  address:

spring:
  profiles:
    active: dev
  application:
    name: vhr
  # 数据源配置
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/vhr?useUnicode=true&characterEncoding=utf-8&useSSL=false&autoReconnect=true&serverTimezone=UTC
    type: com.alibaba.druid.pool.DruidDataSource
    data-username: root
    data-password: root
    druid:
      # 初始化大小,最小,最大
      initial-size: 5
      min-idle: 5
      max-active: 20
      # 配置获取连接等待超时的时间
      max-wait: 60000
      # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
      time-between-eviction-runs-millis: 60000
      # 配置一个连接在池中最小生存的时间,单位是毫秒
      min-evictable-idle-time-millis: 300000
      validation-query: SELECT 1 FROM DUAL
      # 申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效。
      test-while-idle: true
      # 申请连接时会执行validationQuery检测连接是否有效,开启会降低性能,默认为true
      test-on-borrow: false
      # 归还连接时会执行validationQuery检测连接是否有效,开启会降低性能,默认为true
      test-on-return: false
      # 打开PSCache,并且指定每个连接上PSCache的大小. 是否缓存preparedStatement,mysql5.5+建议开启
      pool-prepared-statements: true
      # 当值大于0时poolPreparedStatements会自动修改为true
      max-pool-prepared-statement-per-connection-size: 20
      # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
      filters: stat,wall,slf4j
      # 合并多个DruidDataSource的监控数据
      use-global-data-source-stat: true
      # 通过connectProperties属性来打开mergeSql功能;慢SQL记录
      connect-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000

mybatis:
  # 注意:一定要对应mapper映射xml文件的所在路径
  mapper-locations: classpath:/mapper/*Dao.xml
  # 注意:对应实体类的路径
  type-aliases-package: com.javaboy.vhr.entity

# 日志配置
logging:
  file:
    path: logs
  level:
    org.springframework: WARN
    com.javaboy.vhr: DEBUG

4. 配置启动类

@Slf4j
@SpringBootApplication
@MapperScan(basePackages = "com.javaboy.vhr.dao")
public class VhrApplication {

    public static void main(String[] args) throws UnknownHostException {
        final HashMap<String, Object> props = new HashMap<>();
        final ConfigurableApplicationContext context = new SpringApplicationBuilder()
                .properties(props)
                .sources(VhrApplication.class)
                .run(args);

        final Environment env = context.getEnvironment();
        VhrApplication.log.info("\n----------------------------------------------------------\n\t" +
                        "Application '{}' is running! ActiveProfiles is '{}', Access URLs:\n\t" +
                        "Local: \t\thttp://127.0.0.1:{}\n\t" +
                        "External: \thttp://{}:{}\n----------------------------------------------------------",
                env.getProperty("spring.application.name"),
                env.getProperty("spring.profiles.active"),
                env.getProperty("server.port"),
                InetAddress.getLocalHost().getHostAddress(),
                env.getProperty("server.port"));
    }

}