SpringBoot
什么是SpringBoot
快速启动一个生产级的项目,简化开发流程
回顾SSM
- 复杂、大量的配置文件
- 大量的依赖,没有人管理(容易造成依赖冲突,高低版本不兼容)
SpringBoot能解决上述问题
-
只需要一个配置文件,配置文件的名称必须是 application-.properties或者是 application-.yml这种形式命名
application-dev.properties
application-pro.properties
jdbc.driverClassName = com.mysql.jdbc.Driver jdbc.url = jdbc:mysql://localhost:3306/java2demo?useUnicode=true&characterEncoding=utf8 jdbc.username = root jdbc.password = root
但是SpringBoot更推荐使用.yml文件去作为配置文件
application-dev.yml
application-pro.yml
application-dev.yaml
application-pro.yaml
jdbc:
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/java2demo?useUnicode=true&characterEncoding=utf8
username: root
password: root
- SpringBoot项目有一个“依赖”的父工程,可以引入其他启动器依赖完成依赖管理
创建SpringBoot项目
- 1.创建maven项目有
-
2.在pom.xml中引入父工程
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.neuedu</groupId> <artifactId>springbootdemo1</artifactId> <version>1.0-SNAPSHOT</version> <!-- 引入父工程,当前最新版本2.1.5 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.5.RELEASE</version> </parent> </project>
-
3.web项目,需要引入web启动器依赖,版本由父工程管理
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
如何启动SpringBoot项目
package com.neuedu;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class BootApplication {
public static void main(String[] args) {
SpringApplication.run(BootApplication.class);
}
}
在application.yml中指定启动端口(默认8080)
# 指定启动端口
server:
port: 8888
启动启动类
这个启动类要想生效,它的路径必须在所有子包的父级路径中
SpringBoot中有自动扫描,是以启动类@SpringBootApplication
为起点向下扫描所有子包,所以直接就可以写web层,愉快的开发了
package com.neuedu.web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
@RequestMapping("hello")
@ResponseBody
public String hello(){
return "hello,StringBoot!";
}
}
简单测试,效果完美!
整合数据库连接池,mybatis,和事务
引入mybatis启动器和mysql依赖
<dependencies>
<!--web启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.4</version>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.39</version>
</dependency>
<!-- mybatis启动器 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
application.yml
# 数据库连接参数
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/java2demo?useUnicode=true&characterEncoding=utf8
username: root
password: root
# 数据库连接池
hikari:
maximum-pool-size: 30 # 最大连接数
minimum-idle: 10 # 最小连接数
idle-timeout: 60000 # 超时时间
# 指定mapper文件路径 , 配置包中类别名
mybatis:
mapper-locations: classpath:mappers/*.xml
type-aliases-package: com.neuedu.pojo
# 指定日志打印级别,输出SQL语句
logging:
level:
com.neuedu.mapper: debug
需要注意的是,mapper层要想被正确装配,需要在mapper层接口上添加@Mapper
注解或者在启动上添加@MapperScan
注解完成扫描
@SpringBootApplication
@MapperScan("com.neuedu.mapper") // 指定mapper包
public class BootApplication {
public static void main(String[] args) {
SpringApplication.run(BootApplication.class);
}
}