SpringBoot环境配置 SpringBoot环境配置知识总结

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

SpringBoot环境配置 SpringBoot环境配置知识总结

gaoz666   2021-05-24 我要评论
想了解SpringBoot环境配置知识总结的相关内容吗,gaoz666在本文为您仔细讲解SpringBoot环境配置的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:SpringBoot环境配置,SpringBoot配置环境,下面大家一起来学习吧。

一、pom文件配置

<!-- SpringBoot的父级依赖。只有设置了parent标签,项目才是SpringBoot项目  -->
<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.2.2.RELEASE</version>
</parent>

<dependencies>
   <!-- web启动器,加载web环境所需要的依赖 -->
   <dependency>
   		<groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- mybatis 集成 -->
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.1.1</version>
    </dependency>
    <!-- springboot 分页插件 -->
    <dependency>
        <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper-spring-boot-starter</artifactId>
        <version>1.2.13</version>
    </dependency>
    <!-- mysql 驱动 -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <!-- c3p0 数据源 -->
    <dependency>
        <groupId>com.mchange</groupId>
        <artifactId>c3p0</artifactId>
        <version>0.9.5.5</version>
    </dependency>
    <!-- Freemarker Starter -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-freemarker</artifactId>
    </dependency>
    <!-- Thymeleaf Starter -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <!-- JavaMail -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-mail</artifactId>
    </dependency>
    <!-- AOP -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>
    <!-- Log -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-logging</artifactId>
    </dependency>
    <!-- tomcat -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <!-- 为使用StringUtils工具类 -->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
    </dependency>
    <!-- API文档 -->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.9.2</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.9.2</version>
    </dependency>
    <!-- DevTools 的坐标 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <!--当前这个项目被继承之后,这个不向下传递-->
        <optional>true</optional>	
    </dependency>
    <!-- SpringBoot单元测试 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
    </dependency>
    <!-- Ehcache -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-cache</artifactId>
    </dependency>
    <dependency>
        <groupId>net.sf.ehcache</groupId>
        <artifactId>ehcache</artifactId>
    </dependency>
    
</dependencies>

<!-- SpringBoot项目的打包插件 -->
<build>
    <finalName>springboot</finalName>
	<plugins>
  	<plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
            <fork>true</fork><!-- 如果没有该配置,热部署的devtools不生效 -->
        </configuration>
    </plugin>
  </plugins>
</build>

相关starter系列坐标参考:

名称 描述
spring-boot-starter 核心Spring Boot starter,包括自动配置支持,日志和YAML
spring-boot-starter-actuator 生产准备的特性,用于帮我们监控和管理应用
spring-boot-starter-amqp 对”高级消息队列协议”的支持,通过spring-rabbit实现
spring-boot-starter-aop 对面向切面编程的支持,包括spring-aop和AspectJ
spring-boot-starter-batch 对Spring Batch的支持,包括HSQLDB数据库
spring-boot-starter-cloud-connectors 对Spring Cloud Connectors的支持,简化在云平台下(例如,Cloud Foundry 和Heroku)服务的连接
spring-boot-starter-data-elasticsearch 对Elasticsearch搜索和分析引擎的支持,包括spring-data-elasticsearch
spring-boot-starter-data-gemfire 对GemFire分布式数据存储的支持,包括spring-data-gemfire
spring-boot-starter-data-jpa 对”Java持久化API”的支持,包括spring-data-jpa,spring-orm和Hibernate
spring-boot-starter-data-mongodb 对MongoDB NOSQL数据库的支持,包括spring-data-mongodb
spring-boot-starter-data-rest 对通过REST暴露Spring Data仓库的支持,通过spring-data-rest-webmvc实现
spring-boot-starter-data-solr 对Apache Solr搜索平台的支持,包括spring-data-solr
spring-boot-starter-freemarker 对FreeMarker模板引擎的支持
spring-boot-starter-groovy-templates 对Groovy模板引擎的支持
spring-boot-starter-hateoas 对基于HATEOAS的RESTful服务的支持,通过spring-hateoas实现
spring-boot-starter-hornetq 对”Java消息服务API”的支持,通过HornetQ实现
spring-boot-starter-integration 对普通spring-integration模块的支持
spring-boot-starter-jdbc 对JDBC数据库的支持
spring-boot-starter-jersey 对Jersey RESTful Web服务框架的支持
spring-boot-starter-jta-atomikos 对JTA分布式事务的支持,通过Atomikos实现
spring-boot-starter-jta-bitronix 对JTA分布式事务的支持,通过Bitronix实现
spring-boot-starter-mail 对javax.mail的支持
spring-boot-starter-mobile 对spring-mobile的支持
spring-boot-starter-mustache 对Mustache模板引擎的支持
spring-boot-starter-redis 对REDIS键值数据存储的支持,包括spring-redis
spring-boot-starter-security 对spring-security的支持
spring-boot-starter-social-facebook 对spring-social-facebook的支持
spring-boot-starter-social-linkedin 对spring-social-linkedin的支持
spring-boot-starter-social-twitter 对spring-social-twitter的支持
spring-boot-starter-test 对常用测试依赖的支持,包括JUnit, Hamcrest和Mockito,还有spring-test模块
spring-boot-starter-thymeleaf 对Thymeleaf模板引擎的支持,包括和Spring的集成
spring-boot-starter-velocity 对Velocity模板引擎的支持
spring-boot-starter-web 对全栈web开发的支持, 包括Tomcat和spring-webmvc
spring-boot-starter-websocket 对WebSocket开发的支持
spring-boot-starter-ws 对Spring Web服务的支持

二、yml文件配置

yml文件放在resources目录下

## 端口号  上下文路径
server:
  port: 8989
  servlet:
    context-path: /mvc

## 数据源配置
spring:
  datasource:
    type: com.mchange.v2.c3p0.ComboPooledDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/hr
    username: root
    password: root
    
  freemarker:
    suffix: .ftl
    content-type: text/html  #响应格式
    charset: UTF-8
    template-loader-path: classpath:/views/
    
  ## 热部署配置
  devtools:
    restart:
      enabled: true
      # 设置重启的目录,添加目录的文件需要restart
      additional-paths: src/main/java
      # 解决项目自动重新编译后接口报404的问题
      poll-interval: 3000
      quiet-period: 1000
      
  ## Ehcache缓存配置
  cache:
    ehcache:
      config: classpath:ehcache.xml
    
## mybatis 配置
mybatis:
  #映射文件的存放路径
  mapper-locations: classpath:/mappers/*.xml
  type-aliases-package: com.xxxx.springboot.po
  configuration:
    ## 下划线转驼峰配置
    map-underscore-to-camel-case: true

## pageHelper
pagehelper:
  helper-dialect: mysql

## 显示dao 执行sql语句
logging:
  level:
    com:
      xxxx:
        dao: debug

三、SQL映射文件配置

sql映射文件路径在"resources/mappers/"下

<?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.xxxx.dao.UserMapper">
    
</mapper>

四、ehcahe.xml 文件

配置在resources下

<ehcache name="mycache">
  	<!-- 
		如果不使用磁盘存储,只需要将diskStore注释掉即可;
		如果使用,需要在ehcache.xml文件中的ehcahce元素下的定义一个diskStore元素并指定其path属性。 
	-->
    <diskStore path="C:\java\cache"/>
    <!--
        name:缓存名称。
        maxElementsInMemory:缓存最大数目
        maxElementsOnDisk:硬盘最大缓存个数。
        eternal:对象是否永久有效,一但设置了,timeout将不起作用。
        overflowToDisk:是否保存到磁盘,当系统宕机时
        timeToIdleSeconds:设置对象在失效前的允许闲置时间(单位:秒)。
        	仅当eternal=false对象不是永久有效时使用,可选属性,默认值是0,表示可闲置时间无穷大。
        timeToLiveSeconds:设置对象在失效前允许存活时间(单位:秒)。
            最大时间介于创建时间和失效时间之间。
			仅当eternal=false对象不是永久有效时使用,默认是0,也就是对象存活时间无穷大。
        diskPersistent:是否缓存虚拟机重启期数据 
			Whether the disk store persists between restarts of the Virtual Machine. 			 The default value is false.
        diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。
			默认是30MB。每个Cache都应该有自己的一个缓冲区。
        diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒。
        memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,会根据指定的策略去清理内存
             默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)。
        clearOnFlush:内存数量最大时是否清除。
        memoryStoreEvictionPolicy:
			可选策略有:
				LRU(最近最少使用,默认策略)
					Less Frequently Used,就是例子中使用的策略,就是一直以来最少被使用的。
				FIFO(先进先出)
					first in first out,这个是大家最熟的,先进先出。
				LFU(最少访问次数)
					Least Recently Used,最近最少使用的。
					缓存的元素有一个时间戳,当缓存容量满了,而又需要腾出地方来缓存新的元素的时候,
					那么现有缓存元素中时间戳离当前时间最远的元素将被清出缓存。
        -->
    <defaultCache
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            maxElementsOnDisk="10000000"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU">
    </defaultCache>

    <cache
            name="users"
            eternal="false"
            maxElementsInMemory="100"
            overflowToDisk="false"
            diskPersistent="false"
            timeToIdleSeconds="0"
            timeToLiveSeconds="300"
            memoryStoreEvictionPolicy="LRU"/>
</ehcache>

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们