把原有的application.properties删掉。而后 maven -X clean install,或者经过Maven Project双击clean和install
(1)端口服务配置css
#端口,项目上下文根 server: port: 8080 servlet: context-path: /hotel
其中context-path: /hotel能够不用配置
若是配置,访问路径就是http://ip:port/hotel/
没有配置,访问路径就是http://ip:port/html
(2)数据库配置java
spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=false username: root password: root jpa: hibernate: ddl-auto: update show-sql: true redis: database: 0 host: localhost port: 6379 password: jedis: pool: max-active: 8 max-wait: -1 max-idle: 8 min-idle: 0 timeout: 0
(3)配置多个不一样的profile,实如今不一样的环境(好比开发、测试和生产环境)使用不一样的配置变量。mysql
# 默认的profile为dev,其余环境经过指定启动参数使用不一样的profile,好比:
# 测试环境:java -jar my-spring-boot.jar --spring.profiles.active=test
# 生产环境:java -jar my-spring-boot.jar --spring.profiles.active=prod
spring:
profiles:
active: dev
---
# 开发环境配置
spring:
profiles: dev
mysql:
ipPort: localhost:3306
---
# 测试环境配置
spring:
profiles: test
mysql:
ipPort: ip:port
---
# 生产环境配置
spring:
profiles: prod
mysql:
ipPort: ip:port
使用方法:
经过指定启动参数使用不一样的profile
测试环境: java -jar my-spring-boot.jar --spring.profiles.active=test
生产环境: java -jar my-spring-boot.jar --spring.profiles.active=prodredis
(3)指定静态资源路径spring
spring:
resources:
#指定静态资源路径,默认为classpath:[/META-INF/resources/,/resources/, /static/, /public/]以及context:/
static-locations: classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/}
(4)热部署
在Springboot+Thymeleaf的开发过程当中,默认状况下修改到任何代码都须要从新启动项目才能生效。使用spring.thymeleaf.cache和devtools来解决html热启动的问题
首先在pom.xml中配置sql
<!--增长maven的devtools依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency>
注意:true只有设置为true时才会热启动,即当修改了html、css、js等这些静态资源后不用重启项目直接刷新便可。
而后修改application.yml数据库
#热部署--静态资源当即生效
spring:
#热部署--静态资源当即生效
thymeleaf:
cache: false
encoding: UTF-8
mode: LEGACYHTML5
prefix: classpath:/templates/
suffix: .html
check-template-location: true
#热部署生效
devtools:
restart:
enabled: true
若是须要在修改java文件后都能自动更新,则须要将原先的maven构建修改。
配置了true后在修改java文件后也就支持了热启动,不过这种方式是属于项目重启(速度比较快的项目重启),会清空session中的值,也就是若是有用户登录的话,项目重启后须要从新登录。apache
<!--maven构建--> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
<!--maven构建--> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <!--若是没有该项配置,devtools不会起做用,即应用不会restart --> <fork>true</fork> </configuration> </plugin> </plugins> </build>
(5)时间配置跨域
spring:
jackson:
#指定日期格式,好比yyyy-MM-dd HH:mm:ss
date-format: yyyy-MM-dd HH:mm:ss
#指定日期格式化时区
time-zone: GMT+8
(6)模板配置
springboot 中自带的页面渲染工具为thymeleaf 还有freemarker 这两种模板引擎 。
<!--freemarker--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>
spring:
freemarker:
suffix: .html #设定模板的后缀
request-context-attribute: request #request访问request
content-type: text/html
enabled: true
cache: false #缓存配置
template-loader-path: classpath:/templates/ #模板加载路径 按需配置
charset: UTF-8 #编码格式
settings:
number_format: '0.##' #数字格式化,无小数点
(7)redis和shiro配置
<!--redis和shiro--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <version>1.3.2</version> </dependency>
spring:
redis:
database: 0
host: localhost
port: 6379
password:
jedis:
pool:
max-active: 8
max-wait: -1
max-idle: 8
min-idle: 0
timeout: 0
shiro:
conf:
domain:
cookiePath: /
successUrl: /index
loginView: /login
openToken: false
sessionTimeout: 1800000
algorithmName: md5
hashIterations: 5
#不拦截的路径
sysanon:
- /login
- /regist
#跨域配置
allowedOrigins:
- /**