SpringBoot整合说明
整合说明
jetty服务器
内嵌Tomcat是SpringBoot的辅助功能之一,将Tomcat服务器作为对象运行,并将该对象交给Spring容器管理
jetty服务器:更轻量级,负载性能低
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactedId>spring-boot-starter-web</artifactedId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactedId>spring-boot-starter-tomcat</artifactedId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactedId>spring-boot-starter-jetty</artifactedId>
</dependency>
SpringBoot整合Mybatis-Plus
idea创建boot项目时换一个网址:https://start.aliyun.com或maven工程导入对应依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3</version>
</dependency>
#配置mp相关配置,指定表名开头
mybatis-plus:
global-config:
db-config:
table-prefix: tb1_
SpringBoot整合Druid
druid-spring-boot-starter
# 方法1
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/your_database
username: your_username
password: your_password
# 方法2(推荐)
spring:
datasource:
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/your_database
username: your_username
password: your_password
配置具体含义可以参考Druid的官方文档:Druid配置属性官方文档。
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://localhost:3306/your_database
username: your_username
password: your_password
driver-class-name: com.mysql.cj.jdbc.Driver
druid:
initial-size: 5
min-idle: 5
max-active: 20
max-wait: 60000
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 300000
validation-query: SELECT 1
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: stat,wall,log4j
connection-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
SpringBoot整合JUnit
<dependency>
<groupId>org.springframewor.bootk</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
启动类就起到了配置类的作用,他会把他所在的包及子包扫描一遍;测试类会默认加载引导类,初始化spring的环境
测试类会加载同层包下的带有@SpringBootApplication里的@SpringBootConfiguration里的配置信息
如果测试类在SpringBoot启动类的包或子包中,可以省略启动类的设置,也就是省略classes的设定,如果不在一个包下就指定参数
@SpringBootTest(classes = 引导类名.class)
@SpringBootTest
class test{
@Autowired
private A a;
@Test
public void save(){
a.save();
}
}
SpringBoot整合mybatis
SpringBoot版本低于2.4.3(不含),Mysql驱动版本大于8.0时,需要在url连接串中配置时区或在MySQL数据库端配置时区
驱动类过时:
driver-class-name: com.mysql.cj.jdbc.Driver
加个@Mapper就可以
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url:jdbc:mysql: //localhost:3306/ssm_db?serverTimezone=UTC
username: root
password: root
type: com.alibaba.druid.pool.DruidDataSource