Spring Boot 配置与环境变量
application.properties 常用项、密码不写死
常用配置
server.port=8081
spring.thymeleaf.cache=false # 开发关缓存,改模板即时生效
spring.jpa.hibernate.ddl-auto=none # 不让 Hibernate 自动改表
spring.jpa.show-sql=false
spring.datasource.hikari.maximum-pool-size=5
spring.servlet.multipart.max-file-size=10MB
数据库连接(MySQL 8 必看)
spring.datasource.url=jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false&allowPublicKeyRetrieval=true
spring.datasource.username=${DB_USERNAME}
spring.datasource.password=${DB_PASSWORD}
两个坑:
characterEncoding写utf8不要写utf8mb4(驱动不认,会启动失败)- 非 SSL 连接必须加
allowPublicKeyRetrieval=true,否则报 Public Key Retrieval is not allowed
防中文乱码
server.servlet.encoding.charset=UTF-8
server.servlet.encoding.force=true
spring.thymeleaf.encoding=UTF-8
spring.messages.encoding=UTF-8
Windows 下还要给 JVM 加 -Dfile.encoding=UTF-8(IDEA 在 VM options 里加)。
初始化 SQL
spring.sql.init.mode=always
spring.sql.init.schema-locations=classpath:schema.sql
spring.sql.init.data-locations=classpath:data.sql
spring.sql.init.encoding=UTF-8