springboot
约定大于配置:
约定优于配置(convention over configuration),也称作按约定编程,是一种软件设计范式,旨在减少软件开发人员需做决定的数量,获得简单的好处,而又不失灵活性
在springboot中的约定;
Maven的目录结构。默认有resources文件夹,存放资源配置文件。src-main-resources,src-main-java。默认的编译生成的类都在targe文件夹下面
spring boot默认的配置文件必须是,也只能是application.命名的yml文件或者properties文件,且唯一
application.yml中默认属性。数据库连接信息必须是以spring: datasource: 为前缀;多环境配置。该属性可以根据运行环境自动读取不同的配置文件;端口号、请求路径等
SpringBoot其实不是什么新的框架,它默认配置了很多框架的使用方式,就像maven整合了所有的jar包,spring boot整合了所有的框架
下面是springboot中一些重要的配置文件:
appliction.yml:
# application.properties properties文件在进行配置的时候一定是一个key---value,对于这一条的解释就是一个可以对某电配置完整解释的一定是在同一级别
# application.yml yml文件格式是梯级呈现
# 1>在properties文件里面的 “ .” 连接在yml文件里面全部换成 ":" 进行连接,并且每一级之间必须换行,
# 在第二级开始应该进行一个Tab键的缩进,当然如果是同级的就不需要进行缩进
# 2>在yml文件里面如果是需要进行赋值那么必须是要在 ":" 后面进行一个空格键的缩进
# 3>在yml文件里面所有的配置,相同级别只能出现一次,比如我们使用了spring这个级别,
# 那么我们在后边进行spring级别的配置的时候就必须在这个地方进行,不能在写一个spring级别
server:
# 内置tomcat的信息
port: 8080
servlet:
# 项目访问路径
context-path: /app_springboot
spring:
mvc:
static-path-pattern: /static/**
datasource:
# 链接字符串
url: jdbc:mysql://localhost:3306/appinfodb?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=false
username: root # 用户名
password: root # 用户密码
driver-class-name: com.mysql.jdbc.Driver # 驱动类
type: com.alibaba.druid.pool.DruidDataSource # 数据库连接类型 数据库连接池 除此之外还有例如:C3P0 DBCP
# 初始化大小
initial-size: 5
# 最小值
min-idle: 5
# 最大值
max-active: 20
# 最长等待时间 ms
max-wait: 60000
# 间隔时间 没通过一段时间检测空闲连接,将其关闭
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 300000
validation-query: SELECT 1 FROM DUAL
test-while-idle: true
test-on-borrow: false
test-on-return: false
pool-prepared-statements: true
max-pool-prepared-statement-per-connection-size: 20
# 配置监控 统计拦截filters 去掉监控界面sql无法统计
filters: stat,wall,log4j2
use-global-data-source-stat: true
connect-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
web-stat-filter:
enabled: true
url-patten: "/*"
exclusions: "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*"
freemarker:
cache: false
logging:
level:
com.coffee.mapper: debug
jpa:
hibernate:
ddl-auto: update
show-sql: true
thymeleaf:
servlet:
content-type: text/html
cache: false
encoding: UTF-8
prefix: classpath:/templates/
suffix: .html
check-template-location: true
mode: LEGACYHTML5
redis:
#Redis数据库索引(默认为0)
database: 0
host: localhost
port: 6379
password: 123456
jedis:
pool:
#连接池的最大连接数(使用负值表示没有限制)
max-active: 100
#连接池的最大阻塞等待时间(使用负值表示没有限制)
max-wait: 3000
#连接池的最大空闲连接
max-idle: 100
#连接池总最小的空闲连接数
min-idle: 20
#连接超时时间(毫秒)
timeout: 3000
servlet:
multipart:
# 上传文件总 的最大值
max-request-size: 10MB
# 单个文件的最大值
max-file-size: 10MB
mybatis:
type-aliases-package: com.coffee.pojo #\u522B\u540D
mapper-locations: classpath:mapper/*.xml
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
springbootappliction
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 自动生成的文件
* XXXXXApplication :该类是程序的入口 类的内部有main方法 可以直接通过main方法 启动项目
* @SpringBootApplication 是Spring boot 的核心注解 是一个组合注解
* @Configuration 用于定义配置类,定义配置类可以替换xml文件,一般和@Bean注解联合使用
* @EnableAutoConfiguratin 让Spring Boor根据类路径的jar包依赖当前项目自动配置 比如:添加
* 了 spring-boot-starter-web依赖,会自动添加Tomcat和Spring Mvc 的依赖。那么SpringBoot会对
* Tomcat和Springmvc进行自动配置
* @ComponentScan 该注解默认会扫描该类所在的包下所有的配置
* 总结 如果你不想使用@SpringBootApplication 注解 可以使用这三个注解代替他
*/
@SpringBootApplication
@MapperScan("com.coffee.mapper")
public class AppSpringbootApplication{
public static void main(String[] args) {
//默认的启动方式
SpringApplication.run(AppSpringbootApplication.class, args);
}
}