SpringBoot基础入门笔记,源码可见下载链接
大家阅读时可善用目录功能,可以提高大家的阅读效率
下载地址:SpringBoot笔记+源码
注:开发Spring程序需要确保联网且能够加载到陈虚谷框架结构,不使用idea可以直接去官网(start.spring.io)创建项目
java -jar 名称.jar
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>3.2.0version>
parent>
<groupId>com.LonelysnowgroupId>
<artifactId>SpringBootartifactId>
<version>0.0.1-SNAPSHOTversion>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
dependencies>
project>
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>3.2.0version>
parent>
<groupId>com.LonelysnowgroupId>
<artifactId>SpringBootartifactId>
<version>0.0.1-SNAPSHOTversion>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-tomcatartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-jettyartifactId>
dependency>
dependencies>
project>
SpringBoot提供了多种属性的配置方式
server.port=80
server:
port: 80
server:
port: 80
如果yaml/yml文件没有自动补全,代码提示,可以在项目结构中将文件添加到项目的配置文件中
yaml(YAML Ain’t Markup Language),一种数据序列化格式
核心格式:冒号后加空格
likes:
- a
- b
- c
- d
BookController —— 调用
@RestController
@RequestMapping("/books")
public class BookController {
@Value("${testA}")
private int testA;
@Value("${enterprise.C[0]}")
private String C;
@Value("${enterprise.C[1]}")
private String A;
@Autowired
private Environment environment;
@Autowired
private Enterprise enterprise;
@GetMapping
public String get() {
System.out.println(environment);
System.out.println("________________________________________");
System.out.println(enterprise);
return "hello world";
}
}
domain/Enterprise —— 自定义对象
@Component
@ConfigurationProperties(prefix = "enterprise")
public class Enterprise {
private String A;
private Integer B;
private String D;
private String[] C;
// ......
// getter setter方法省略
}
application.yml
testA: 100
enterprise:
A: AAA
B: 200
D: BBB
C:
- 前端
- aa
- ba
- ca
- da
spring:
profiles:
active: dev
---
server:
port: 80
spring:
config:
activate:
on-profile: dev
---
server:
port: 81
spring:
config:
activate:
on-profile: pro
---
server:
port: 82
spring:
config:
activate:
on-profile: test
#设定需要的运行环境
spring.profiles.active=dev
server.port=82
java -jar SpringBoot-0.0.1-SNAPSHOT.jar --spring.profiles.active=XXX(环境名称)
java -jar SpringBoot-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev --server.port=8081
可运行
java -jar SpringBoot-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev --server.port=8081 --server.port=8082
会报错
注意事项:
<project>
<profiles>
<profile>
<id>devid>
<properties>
<profile.active>devprofile.active>
properties>
profile>
<profile>
<id>proid>
<properties>
<profile.active>proprofile.active>
properties>
<activation>
<activeByDefault>trueactiveByDefault>
activation>
profile>
<profile>
<id>testid>
<properties>
<profile.active>testprofile.active>
properties>
profile>
profiles>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins groupId>
<artifactId>maven-resources-pluginartifactId>
<version>3.3.1version>
<configuration>
<useDefaultDelimiters>trueuseDefaultDelimiters>
configuration>
plugin>
plugins>
build>
project>
spring:
profiles:
active: ${profile.active}
---
server:
port: 80
spring:
config:
activate:
on-profile: dev
---
server:
port: 81
spring:
config:
activate:
on-profile: pro
---
server:
port: 82
spring:
config:
activate:
on-profile: test
@SpringBootTest
class ApplicationTests {
@Autowired
private BookService bookService;
@Test
public void save(){
bookService.save();
}
}
<dependencies>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<scope>runtimescope>
<version>8.0.33version>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>3.0.3version>
dependency>
dependencies>
spring:
profiles:
active: ${profile.active}
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/ssm_db
username: root
password: 123456
@Mapper
public interface BookDao {
@Select("select * from tbl_book where id = ${id}")
Book getById(Integer id);
}
@SpringBootTest
class ApplicationTests {
@Autowired
private BookDao bookDao;
@Test
public void testById(){
Book book = bookDao.getById(3);
System.out.println(book);
}
}