零基础搭建SpringBoot框架
在一般互联网公司,一些技术框架无论是前端还是后台,都是有相当牛技术经验,技术经理和架构师来搭建,一般的技术人员是无法接触到这一块的。因此,这边只是满足一些小型的开发,同时主要目的还是从搭建的角度去了解SpringBoot而已。话不多说开始搭建;
一、maven添加SpringBoot相关依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId><!-- 控制版本信息 -->
<version>2.1.9.RELEASE</version> <!-- SpringBoot的版本 -->
<relativePath />
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source><!-- 通过JDK为1.8编译 -->
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<!-- spring-boot-starter是SpringBoot的核心启动器,包含了自动配置、日志和YAML -->
<artifactId>spring-boot-starter-web</artifactId><!-- SpringBoot web模块支持,自动帮我们引入了web模块开发需要的相关jar包-->
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId><!-- SpringBoot程序测试依赖,如果是自动创建项目默认添加 -->
<scope>test</scope>
</dependency>
</dependencies>以上的依赖已经将SpringBoot部署到项目中了。
二、配置application.yml(或application.properties)文件
yml(或者yaml)文件相当于SSM框架的xml配置信息一样,因此,它会将你所用到的信息在此文件编辑,该文件放在src/main/resources路径下,下面以YML形式来配置:
quartz:
enabled: true #开启定时任务
server:
port: 8080 #指定启动端口号
servlet:
context-path: /**/**
spring:
output:
ansi:
enabled: always
#数据库相关配置
datasource:
name: ****
url: jdbc:mysql://r************:3306/***?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8
username: ****
password: ****
#配置初始化大小/最小/最大
initial-size: 1
min-idle: 1
max-active: 20
#获取连接等待超时时间
max-wait: 60000
#间隔多久进行一次检测,检测需要关闭的空闲连接
time-between-eviction-runs-millis: 60000
#一个连接在池中最小生存的时间
min-evictable-idle-time-millis: 300000
validation-query: SELECT ‘x‘
test-while-idle: true
test-on-borrow: false
test-on-return: false
#打开PSCache,并指定每个连接上PSCache的大小。oracle设为true,mysql设为false。分库分表较多推荐设置为false
pool-prepared-statements: false
max-pool-prepared-statement-per-connection-size: 20
## 该配置节点为独立的节点,有很多同学容易将这个配置放在spring的节点下,导致配置无法被识别
mybatis:
mapper-locations: classpath:mapper/*.xml #注意:一定要对应mapper映射xml文件的所在路径
#log日志
logging:
config: classpath:logback-spring.xml
path: D:/Log/logs三、pom添加数据库依赖(以MySQL为主)
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>这样对于一个一般小型的SpringBoot框架就搭建好了,下来就跟原来的项目一样,配置相关pom依赖,新建实体类、Service接口、Controller类,去继续完成一个项目。