多环境示例
# 端口
server:
port: 8000
# 环境
spring:
# test-测试,prod-正式
profiles:
active: test
# 上传文件大小
servlet:
multipart:
max-file-size: 10MB
max-request-size: 10MB
# mybatis
mybatis:
configuration:
# 链接超时时间
default-statement-timeout: 30
# 开启驼峰命名规则映射(用于数据返回映射到实体类)
map-underscore-to-camel-case: true
---
# 测试环境
spring:
profiles: test
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/blog_fan?serverTimezone=GMT%2B8&characterEncoding=utf8
username: root
password: 123456
# 日志级别
logging:
level:
com.fan.blog: debug
# 文件保存路径
filesPath: 'D://P_files'
---
# 正式环境
spring:
profiles: prod
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/blog_fan?serverTimezone=GMT%2B8&characterEncoding=utf8
username: root
password: 123456
# 日志级别
logging:
level:
com.fan.blog: error
# 文件保存路径
filesPath: '/home/files'
---
高版本多环境改动
---
spring:
# 开发
config:
activate:
on-profile: dev
# 数据库
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.56.100:3306/fans?serverTimezone=GMT%2B8&characterEncoding=utf8
username: authUser
password: 123456
时间格式配置
spring:
# 时间格式化
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: Asia/Shanghai
SpringBoot2.x启动执行SQL脚本
# SQL脚本
spring:
sql:
init:
mode: always
data-locations: classpath:table.sql
sql的书写方式
-- 测试
create table if not exists `test` (
`id` bigint primary key,
`name` varchar(255)
);
-- 新增不存在字段暂时无法解决,存储过程运行报错
MySQL字段不存在则添加
MySQL需要利用存储过程实现
DROP PROCEDURE IF EXISTS pro_add_column;
DELIMITER $
CREATE PROCEDURE pro_add_column(
IN tablename VARCHAR(50),
IN colname VARCHAR(50),
IN coltype VARCHAR(20),
IN coldesc VARCHAR(50) )
BEGIN
if not exists(select 1 from information_schema.`COLUMNS` where table_name=tablename and column_name=colname) then
set @sqlStr = CONCAT('alter table ',tablename,' add ', colname, ' ',coltype,' ', ' COMMENT ', coldesc);
prepare sqlStr from @sqlStr;
execute sqlStr;
end if;
END $
DELIMITER;
使用方式
--表名, 字段名, 字段类型, 注释
CALL pro_add_column('test_table', 'count', 'int default 0', '''数量''');
自定义启动Banner
SpringBoot Banner生成:在resources
下新建banner.txt
nginx代理后真实IP获取
nginx配置
# 可以获取到客户端真实IP
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_redirect off;
tomcat配置
# 端口
server:
forward-headers-strategy: framework
tomcat:
remoteip:
host-header: X-Real-IP
protocol-header: X-Forwarded-Proto
获取配置文件中的配置项
在启动类里获取
@SpringBootApplication
public class MylslApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(MylslApplication.class, args);
System.err.println("local.ip=" + context.getEnvironment().getProperty("local.ip"));
}
}
在类中利用Environment获取
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
@Component
public class MyConfig {
@Autowired
private Environment env;
public void show(){
System.err.println("MyConfig---local.ip=" + env.getProperty("local.ip"));
}
}
在类中利用@Value注解获取,直接赋值给属性
缺点,如果没有配置会报错
package com.lsl.mylsl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
@Component
public class MyConfig {
@Autowired
private Environment env;
@Value("${local.port}")
private String localPort;
public void show(){
System.err.println("MyConfig---local.ip=" + env.getProperty("local.ip"));
System.err.println("MyConfig---local.port=" + env.getProperty("local.port"));
}
}